mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor: move operator to oxc_syntax (#341)
This commit is contained in:
parent
52aa59bb38
commit
65a445f512
38 changed files with 97 additions and 487 deletions
15
Cargo.lock
generated
15
Cargo.lock
generated
|
|
@ -971,6 +971,7 @@ dependencies = [
|
|||
"ordered-float",
|
||||
"oxc_allocator",
|
||||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"rustc-hash",
|
||||
"ryu-js",
|
||||
"serde",
|
||||
|
|
@ -989,6 +990,7 @@ dependencies = [
|
|||
"oxc_hir",
|
||||
"oxc_parser",
|
||||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
|
@ -1078,6 +1080,7 @@ dependencies = [
|
|||
"oxc_ast",
|
||||
"oxc_parser",
|
||||
"oxc_semantic",
|
||||
"oxc_syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1089,6 +1092,7 @@ dependencies = [
|
|||
"ordered-float",
|
||||
"oxc_allocator",
|
||||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"ryu-js",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
|
@ -1112,6 +1116,7 @@ dependencies = [
|
|||
"oxc_parser",
|
||||
"oxc_semantic",
|
||||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"phf",
|
||||
"rust-lapper",
|
||||
"rustc-hash",
|
||||
|
|
@ -1140,6 +1145,7 @@ dependencies = [
|
|||
"oxc_parser",
|
||||
"oxc_semantic",
|
||||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
|
|
@ -1180,6 +1186,7 @@ dependencies = [
|
|||
"oxc_ast",
|
||||
"oxc_diagnostics",
|
||||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"rustc-hash",
|
||||
"serde_json",
|
||||
"unicode-id-start",
|
||||
|
|
@ -1198,6 +1205,7 @@ dependencies = [
|
|||
"oxc_diagnostics",
|
||||
"oxc_parser",
|
||||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"phf",
|
||||
"rustc-hash",
|
||||
]
|
||||
|
|
@ -1211,6 +1219,13 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "oxc_syntax"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "oxc_tasks_common"
|
||||
version = "0.0.0"
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ oxc_linter = { version = "0.0.0", path = "crates/oxc_linter" }
|
|||
oxc_minifier = { version = "0.0.0", path = "crates/oxc_minifier" }
|
||||
oxc_hir = { version = "0.0.0", path = "crates/oxc_hir" }
|
||||
oxc_ast_lower = { version = "0.0.0", path = "crates/oxc_ast_lower" }
|
||||
oxc_syntax = { version = "0.0.0", path = "crates/oxc_syntax" }
|
||||
|
||||
oxc_tasks_common = { path = "tasks/common" }
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ repository.workspace = true
|
|||
[dependencies]
|
||||
oxc_allocator = { workspace = true }
|
||||
oxc_span = { workspace = true }
|
||||
oxc_syntax = { workspace = true }
|
||||
|
||||
bitflags = { workspace = true }
|
||||
rustc-hash = { workspace = true }
|
||||
|
|
@ -24,4 +25,4 @@ ryu-js = { workspace = true, optional = true }
|
|||
|
||||
[features]
|
||||
default = []
|
||||
serde = ["dep:serde", "dep:serde_json", "dep:ryu-js", "ordered-float/serde", "oxc_span/serde"]
|
||||
serde = ["dep:serde", "dep:serde_json", "dep:ryu-js", "ordered-float/serde", "oxc_span/serde", "oxc_syntax/serde"]
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@ use std::fmt;
|
|||
use num_bigint::BigUint;
|
||||
use oxc_allocator::{Box, Vec};
|
||||
use oxc_span::{Atom, Span};
|
||||
use oxc_syntax::operator::{
|
||||
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
|
||||
};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::Serialize;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,12 +4,10 @@ mod js;
|
|||
mod jsdoc;
|
||||
mod jsx;
|
||||
mod literal;
|
||||
mod operator;
|
||||
mod ts;
|
||||
|
||||
pub use self::js::*;
|
||||
pub use self::jsdoc::*;
|
||||
pub use self::jsx::*;
|
||||
pub use self::literal::*;
|
||||
pub use self::operator::*;
|
||||
pub use self::ts::*;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@
|
|||
|
||||
use oxc_allocator::{Allocator, Box, String, Vec};
|
||||
use oxc_span::{Atom, Span};
|
||||
use oxc_syntax::operator::{
|
||||
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
|
||||
};
|
||||
|
||||
#[allow(clippy::wildcard_imports)]
|
||||
use crate::{ast::*, SourceType};
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ oxc_allocator = { workspace = true }
|
|||
oxc_ast = { workspace = true }
|
||||
oxc_hir = { workspace = true }
|
||||
oxc_span = { workspace = true }
|
||||
oxc_syntax = { workspace = true }
|
||||
|
||||
bitflags = { workspace = true }
|
||||
num-bigint = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -384,29 +384,9 @@ impl<'a> AstLower<'a> {
|
|||
&mut self,
|
||||
expr: &ast::AssignmentExpression<'a>,
|
||||
) -> hir::Expression<'a> {
|
||||
let operator = match expr.operator {
|
||||
ast::AssignmentOperator::Assign => hir::AssignmentOperator::Assign,
|
||||
ast::AssignmentOperator::Addition => hir::AssignmentOperator::Addition,
|
||||
ast::AssignmentOperator::Subtraction => hir::AssignmentOperator::Subtraction,
|
||||
ast::AssignmentOperator::Multiplication => hir::AssignmentOperator::Multiplication,
|
||||
ast::AssignmentOperator::Division => hir::AssignmentOperator::Division,
|
||||
ast::AssignmentOperator::Remainder => hir::AssignmentOperator::Remainder,
|
||||
ast::AssignmentOperator::ShiftLeft => hir::AssignmentOperator::ShiftLeft,
|
||||
ast::AssignmentOperator::ShiftRight => hir::AssignmentOperator::ShiftRight,
|
||||
ast::AssignmentOperator::ShiftRightZeroFill => {
|
||||
hir::AssignmentOperator::ShiftRightZeroFill
|
||||
}
|
||||
ast::AssignmentOperator::BitwiseOR => hir::AssignmentOperator::BitwiseOR,
|
||||
ast::AssignmentOperator::BitwiseXOR => hir::AssignmentOperator::BitwiseXOR,
|
||||
ast::AssignmentOperator::BitwiseAnd => hir::AssignmentOperator::BitwiseAnd,
|
||||
ast::AssignmentOperator::LogicalAnd => hir::AssignmentOperator::LogicalAnd,
|
||||
ast::AssignmentOperator::LogicalOr => hir::AssignmentOperator::LogicalOr,
|
||||
ast::AssignmentOperator::LogicalNullish => hir::AssignmentOperator::LogicalNullish,
|
||||
ast::AssignmentOperator::Exponential => hir::AssignmentOperator::Exponential,
|
||||
};
|
||||
let left = self.lower_assignment_target(&expr.left);
|
||||
let right = self.lower_expression(&expr.right);
|
||||
self.hir.assignment_expression(expr.span, operator, left, right)
|
||||
self.hir.assignment_expression(expr.span, expr.operator, left, right)
|
||||
}
|
||||
|
||||
fn lower_arrow_expression(&mut self, expr: &ast::ArrowExpression<'a>) -> hir::Expression<'a> {
|
||||
|
|
@ -429,32 +409,8 @@ impl<'a> AstLower<'a> {
|
|||
|
||||
fn lower_binary_expression(&mut self, expr: &ast::BinaryExpression<'a>) -> hir::Expression<'a> {
|
||||
let left = self.lower_expression(&expr.left);
|
||||
let operator = match expr.operator {
|
||||
ast::BinaryOperator::Equality => hir::BinaryOperator::Equality,
|
||||
ast::BinaryOperator::Inequality => hir::BinaryOperator::Inequality,
|
||||
ast::BinaryOperator::StrictEquality => hir::BinaryOperator::StrictEquality,
|
||||
ast::BinaryOperator::StrictInequality => hir::BinaryOperator::StrictInequality,
|
||||
ast::BinaryOperator::LessThan => hir::BinaryOperator::LessThan,
|
||||
ast::BinaryOperator::LessEqualThan => hir::BinaryOperator::LessEqualThan,
|
||||
ast::BinaryOperator::GreaterThan => hir::BinaryOperator::GreaterThan,
|
||||
ast::BinaryOperator::GreaterEqualThan => hir::BinaryOperator::GreaterEqualThan,
|
||||
ast::BinaryOperator::ShiftLeft => hir::BinaryOperator::ShiftLeft,
|
||||
ast::BinaryOperator::ShiftRight => hir::BinaryOperator::ShiftRight,
|
||||
ast::BinaryOperator::ShiftRightZeroFill => hir::BinaryOperator::ShiftRightZeroFill,
|
||||
ast::BinaryOperator::Addition => hir::BinaryOperator::Addition,
|
||||
ast::BinaryOperator::Subtraction => hir::BinaryOperator::Subtraction,
|
||||
ast::BinaryOperator::Multiplication => hir::BinaryOperator::Multiplication,
|
||||
ast::BinaryOperator::Division => hir::BinaryOperator::Division,
|
||||
ast::BinaryOperator::Remainder => hir::BinaryOperator::Remainder,
|
||||
ast::BinaryOperator::BitwiseOR => hir::BinaryOperator::BitwiseOR,
|
||||
ast::BinaryOperator::BitwiseXOR => hir::BinaryOperator::BitwiseXOR,
|
||||
ast::BinaryOperator::BitwiseAnd => hir::BinaryOperator::BitwiseAnd,
|
||||
ast::BinaryOperator::In => hir::BinaryOperator::In,
|
||||
ast::BinaryOperator::Instanceof => hir::BinaryOperator::Instanceof,
|
||||
ast::BinaryOperator::Exponential => hir::BinaryOperator::Exponential,
|
||||
};
|
||||
let right = self.lower_expression(&expr.right);
|
||||
self.hir.binary_expression(expr.span, left, operator, right)
|
||||
self.hir.binary_expression(expr.span, left, expr.operator, right)
|
||||
}
|
||||
|
||||
fn lower_call_expression(&mut self, expr: &ast::CallExpression<'a>) -> hir::Expression<'a> {
|
||||
|
|
@ -512,13 +468,8 @@ impl<'a> AstLower<'a> {
|
|||
expr: &ast::LogicalExpression<'a>,
|
||||
) -> hir::Expression<'a> {
|
||||
let left = self.lower_expression(&expr.left);
|
||||
let operator = match expr.operator {
|
||||
ast::LogicalOperator::Or => hir::LogicalOperator::Or,
|
||||
ast::LogicalOperator::And => hir::LogicalOperator::And,
|
||||
ast::LogicalOperator::Coalesce => hir::LogicalOperator::Coalesce,
|
||||
};
|
||||
let right = self.lower_expression(&expr.right);
|
||||
self.hir.logical_expression(expr.span, left, operator, right)
|
||||
self.hir.logical_expression(expr.span, left, expr.operator, right)
|
||||
}
|
||||
|
||||
fn lower_member_expr(&mut self, expr: &ast::MemberExpression<'a>) -> hir::MemberExpression<'a> {
|
||||
|
|
@ -660,26 +611,13 @@ impl<'a> AstLower<'a> {
|
|||
}
|
||||
|
||||
fn lower_unary_expression(&mut self, expr: &ast::UnaryExpression<'a>) -> hir::Expression<'a> {
|
||||
let operator = match expr.operator {
|
||||
ast::UnaryOperator::UnaryNegation => hir::UnaryOperator::UnaryNegation,
|
||||
ast::UnaryOperator::UnaryPlus => hir::UnaryOperator::UnaryPlus,
|
||||
ast::UnaryOperator::LogicalNot => hir::UnaryOperator::LogicalNot,
|
||||
ast::UnaryOperator::BitwiseNot => hir::UnaryOperator::BitwiseNot,
|
||||
ast::UnaryOperator::Typeof => hir::UnaryOperator::Typeof,
|
||||
ast::UnaryOperator::Void => hir::UnaryOperator::Void,
|
||||
ast::UnaryOperator::Delete => hir::UnaryOperator::Delete,
|
||||
};
|
||||
let argument = self.lower_expression(&expr.argument);
|
||||
self.hir.unary_expression(expr.span, operator, expr.prefix, argument)
|
||||
self.hir.unary_expression(expr.span, expr.operator, expr.prefix, argument)
|
||||
}
|
||||
|
||||
fn lower_update_expression(&mut self, expr: &ast::UpdateExpression<'a>) -> hir::Expression<'a> {
|
||||
let operator = match expr.operator {
|
||||
ast::UpdateOperator::Increment => hir::UpdateOperator::Increment,
|
||||
ast::UpdateOperator::Decrement => hir::UpdateOperator::Decrement,
|
||||
};
|
||||
let argument = self.lower_simple_assignment_target(&expr.argument);
|
||||
self.hir.update_expression(expr.span, operator, expr.prefix, argument)
|
||||
self.hir.update_expression(expr.span, expr.operator, expr.prefix, argument)
|
||||
}
|
||||
|
||||
fn lower_yield_expression(&mut self, expr: &ast::YieldExpression<'a>) -> hir::Expression<'a> {
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ repository.workspace = true
|
|||
oxc_allocator = { workspace = true }
|
||||
oxc_ast = { workspace = true }
|
||||
oxc_semantic = { workspace = true }
|
||||
oxc_syntax = { workspace = true }
|
||||
|
||||
[dev_dependencies]
|
||||
oxc_parser = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use oxc_allocator::{Box, Vec};
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
use oxc_semantic::Symbol;
|
||||
use oxc_syntax::operator::{BinaryOperator, Operator};
|
||||
|
||||
use crate::{Formatter, Separator};
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use std::rc::Rc;
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
use oxc_semantic::SymbolTable;
|
||||
use oxc_syntax::operator::{BinaryOperator, Operator, UnaryOperator, UpdateOperator};
|
||||
|
||||
pub use crate::gen::Gen;
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ repository.workspace = true
|
|||
[dependencies]
|
||||
oxc_allocator = { workspace = true }
|
||||
oxc_span = { workspace = true }
|
||||
oxc_syntax = { workspace = true }
|
||||
|
||||
bitflags = { workspace = true }
|
||||
num-bigint = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -8,6 +8,9 @@ use num_bigint::BigUint;
|
|||
use ordered_float::NotNan;
|
||||
use oxc_allocator::{Box, Vec};
|
||||
use oxc_span::{Atom, Span};
|
||||
use oxc_syntax::operator::{
|
||||
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
|
||||
};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::Serialize;
|
||||
|
||||
|
|
@ -2118,404 +2121,6 @@ pub struct JSXText {
|
|||
pub value: Atom,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize))]
|
||||
pub enum Operator {
|
||||
AssignmentOperator(AssignmentOperator),
|
||||
BinaryOperator(BinaryOperator),
|
||||
LogicalOperator(LogicalOperator),
|
||||
UnaryOperator(UnaryOperator),
|
||||
UpdateOperator(UpdateOperator),
|
||||
}
|
||||
|
||||
impl From<AssignmentOperator> for Operator {
|
||||
fn from(op: AssignmentOperator) -> Self {
|
||||
Self::AssignmentOperator(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BinaryOperator> for Operator {
|
||||
fn from(op: BinaryOperator) -> Self {
|
||||
Self::BinaryOperator(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LogicalOperator> for Operator {
|
||||
fn from(op: LogicalOperator) -> Self {
|
||||
Self::LogicalOperator(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UnaryOperator> for Operator {
|
||||
fn from(op: UnaryOperator) -> Self {
|
||||
Self::UnaryOperator(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UpdateOperator> for Operator {
|
||||
fn from(op: UpdateOperator) -> Self {
|
||||
Self::UpdateOperator(op)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize))]
|
||||
pub enum AssignmentOperator {
|
||||
#[cfg_attr(feature = "serde", serde(rename = "="))]
|
||||
Assign,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "+="))]
|
||||
Addition,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "-="))]
|
||||
Subtraction,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "*="))]
|
||||
Multiplication,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "/="))]
|
||||
Division,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "%="))]
|
||||
Remainder,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "<<="))]
|
||||
ShiftLeft,
|
||||
#[cfg_attr(feature = "serde", serde(rename = ">>="))]
|
||||
ShiftRight,
|
||||
#[cfg_attr(feature = "serde", serde(rename = ">>>="))]
|
||||
ShiftRightZeroFill,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "|="))]
|
||||
BitwiseOR,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "^="))]
|
||||
BitwiseXOR,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "&="))]
|
||||
BitwiseAnd,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "&&="))]
|
||||
LogicalAnd,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "||="))]
|
||||
LogicalOr,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "??="))]
|
||||
LogicalNullish,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "**="))]
|
||||
Exponential,
|
||||
}
|
||||
|
||||
impl AssignmentOperator {
|
||||
#[must_use]
|
||||
pub fn is_logical_operator(self) -> bool {
|
||||
matches!(self, Self::LogicalAnd | Self::LogicalOr | Self::LogicalNullish)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_arithmetic(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Addition
|
||||
| Self::Subtraction
|
||||
| Self::Multiplication
|
||||
| Self::Division
|
||||
| Self::Remainder
|
||||
| Self::Exponential
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_bitwise(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::BitwiseOR
|
||||
| Self::BitwiseXOR
|
||||
| Self::BitwiseAnd
|
||||
| Self::ShiftLeft
|
||||
| Self::ShiftRight
|
||||
| Self::ShiftRightZeroFill
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Assign => "=",
|
||||
Self::Addition => "+=",
|
||||
Self::Subtraction => "-=",
|
||||
Self::Multiplication => "*=",
|
||||
Self::Division => "/=",
|
||||
Self::Remainder => "%=",
|
||||
Self::ShiftLeft => "<<=",
|
||||
Self::ShiftRight => ">>=",
|
||||
Self::ShiftRightZeroFill => ">>>=",
|
||||
Self::BitwiseOR => "|=",
|
||||
Self::BitwiseXOR => "^=",
|
||||
Self::BitwiseAnd => "&=",
|
||||
Self::LogicalAnd => "&&=",
|
||||
Self::LogicalOr => "||=",
|
||||
Self::LogicalNullish => "??=",
|
||||
Self::Exponential => "**=",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for AssignmentOperator {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let operator = self.as_str();
|
||||
write!(f, "{operator}")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize))]
|
||||
pub enum BinaryOperator {
|
||||
#[cfg_attr(feature = "serde", serde(rename = "=="))]
|
||||
Equality,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "!="))]
|
||||
Inequality,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "==="))]
|
||||
StrictEquality,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "!=="))]
|
||||
StrictInequality,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "<"))]
|
||||
LessThan,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "<="))]
|
||||
LessEqualThan,
|
||||
#[cfg_attr(feature = "serde", serde(rename = ">"))]
|
||||
GreaterThan,
|
||||
#[cfg_attr(feature = "serde", serde(rename = ">="))]
|
||||
GreaterEqualThan,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "<<"))]
|
||||
ShiftLeft,
|
||||
#[cfg_attr(feature = "serde", serde(rename = ">>"))]
|
||||
ShiftRight,
|
||||
#[cfg_attr(feature = "serde", serde(rename = ">>>"))]
|
||||
ShiftRightZeroFill,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "+"))]
|
||||
Addition,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "-"))]
|
||||
Subtraction,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "*"))]
|
||||
Multiplication,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "/"))]
|
||||
Division,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "%"))]
|
||||
Remainder,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "|"))]
|
||||
BitwiseOR,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "^"))]
|
||||
BitwiseXOR,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "&"))]
|
||||
BitwiseAnd,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "in"))]
|
||||
In,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "instanceof"))]
|
||||
Instanceof,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "**"))]
|
||||
Exponential,
|
||||
}
|
||||
|
||||
impl BinaryOperator {
|
||||
#[must_use]
|
||||
pub fn is_equality(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Equality | Self::Inequality | Self::StrictEquality | Self::StrictInequality
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_compare(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::LessThan | Self::LessEqualThan | Self::GreaterThan | Self::GreaterEqualThan
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_arithmetic(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Addition
|
||||
| Self::Subtraction
|
||||
| Self::Multiplication
|
||||
| Self::Division
|
||||
| Self::Remainder
|
||||
| Self::Exponential
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_relational(self) -> bool {
|
||||
matches!(self, Self::In | Self::Instanceof)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_bitwise(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::BitwiseOR
|
||||
| Self::BitwiseXOR
|
||||
| Self::BitwiseAnd
|
||||
| Self::ShiftLeft
|
||||
| Self::ShiftRight
|
||||
| Self::ShiftRightZeroFill,
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_numeric_or_string_binary_operator(self) -> bool {
|
||||
self.is_arithmetic() || self.is_bitwise()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_keyword(self) -> bool {
|
||||
matches!(self, Self::In | Self::Instanceof)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Equality => "==",
|
||||
Self::Inequality => "!=",
|
||||
Self::StrictEquality => "===",
|
||||
Self::StrictInequality => "!==",
|
||||
Self::LessThan => "<",
|
||||
Self::LessEqualThan => "<=",
|
||||
Self::GreaterThan => ">",
|
||||
Self::GreaterEqualThan => ">=",
|
||||
Self::ShiftLeft => "<<",
|
||||
Self::ShiftRight => ">>",
|
||||
Self::ShiftRightZeroFill => ">>>",
|
||||
Self::Addition => "+",
|
||||
Self::Subtraction => "-",
|
||||
Self::Multiplication => "*",
|
||||
Self::Division => "/",
|
||||
Self::Remainder => "%",
|
||||
Self::BitwiseOR => "|",
|
||||
Self::BitwiseXOR => "^",
|
||||
Self::BitwiseAnd => "&",
|
||||
Self::In => "in",
|
||||
Self::Instanceof => "instanceof",
|
||||
Self::Exponential => "**",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for BinaryOperator {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let operator = self.as_str();
|
||||
write!(f, "{operator}")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize))]
|
||||
pub enum LogicalOperator {
|
||||
#[cfg_attr(feature = "serde", serde(rename = "||"))]
|
||||
Or,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "&&"))]
|
||||
And,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "??"))]
|
||||
Coalesce,
|
||||
}
|
||||
|
||||
impl LogicalOperator {
|
||||
#[must_use]
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Or => "||",
|
||||
Self::And => "&&",
|
||||
Self::Coalesce => "??",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LogicalOperator {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let operator = self.as_str();
|
||||
write!(f, "{operator}")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize))]
|
||||
pub enum UnaryOperator {
|
||||
#[cfg_attr(feature = "serde", serde(rename = "-"))]
|
||||
UnaryNegation,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "+"))]
|
||||
UnaryPlus,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "!"))]
|
||||
LogicalNot,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "~"))]
|
||||
BitwiseNot,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "typeof"))]
|
||||
Typeof,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "void"))]
|
||||
Void,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "delete"))]
|
||||
Delete,
|
||||
}
|
||||
|
||||
impl UnaryOperator {
|
||||
#[must_use]
|
||||
pub fn operator(&self) -> Operator {
|
||||
Operator::UnaryOperator(*self)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_arithmetic(self) -> bool {
|
||||
matches!(self, Self::UnaryNegation | Self::UnaryPlus)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_bitwise(self) -> bool {
|
||||
matches!(self, Self::BitwiseNot)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_keyword(self) -> bool {
|
||||
matches!(self, Self::Typeof | Self::Void | Self::Delete)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::UnaryNegation => "-",
|
||||
Self::UnaryPlus => "+",
|
||||
Self::LogicalNot => "!",
|
||||
Self::BitwiseNot => "~",
|
||||
Self::Typeof => "typeof",
|
||||
Self::Void => "void",
|
||||
Self::Delete => "delete",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for UnaryOperator {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let operator = self.as_str();
|
||||
write!(f, "{operator}")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize))]
|
||||
pub enum UpdateOperator {
|
||||
#[cfg_attr(feature = "serde", serde(rename = "++"))]
|
||||
Increment,
|
||||
#[cfg_attr(feature = "serde", serde(rename = "--"))]
|
||||
Decrement,
|
||||
}
|
||||
|
||||
impl UpdateOperator {
|
||||
#[must_use]
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Increment => "++",
|
||||
Self::Decrement => "--",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for UpdateOperator {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let operator = self.as_str();
|
||||
write!(f, "{operator}")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type", rename_all = "camelCase"))]
|
||||
pub struct Decorator<'a> {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ use num_bigint::BigUint;
|
|||
use ordered_float::NotNan;
|
||||
use oxc_allocator::{Allocator, Box, String, Vec};
|
||||
use oxc_span::{Atom, Span};
|
||||
use oxc_syntax::operator::{
|
||||
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
|
||||
};
|
||||
|
||||
#[allow(clippy::wildcard_imports)]
|
||||
use crate::hir::*;
|
||||
|
|
|
|||
|
|
@ -16,7 +16,8 @@ oxc_ast = { workspace = true }
|
|||
oxc_diagnostics = { workspace = true }
|
||||
oxc_macros = { workspace = true }
|
||||
oxc_semantic = { workspace = true }
|
||||
oxc_formatter = { workspace = true }
|
||||
oxc_syntax = { workspace = true }
|
||||
oxc_formatter = { workspace = true }
|
||||
|
||||
lazy_static = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ use std::hash::{Hash, Hasher};
|
|||
use oxc_ast::AstKind;
|
||||
use oxc_semantic::AstNode;
|
||||
use oxc_span::GetSpan;
|
||||
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator};
|
||||
use rustc_hash::FxHasher;
|
||||
|
||||
pub fn calculate_hash<T: Hash>(t: &T) -> u64 {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use oxc_ast::{
|
||||
ast::{AssignmentOperator, BinaryExpression, BinaryOperator, Expression, UnaryOperator},
|
||||
ast::{BinaryExpression, Expression},
|
||||
AstKind,
|
||||
};
|
||||
use oxc_diagnostics::{
|
||||
|
|
@ -8,6 +8,7 @@ use oxc_diagnostics::{
|
|||
};
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};
|
||||
|
||||
use crate::{context::LintContext, rule::Rule, AstNode};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
use oxc_ast::{
|
||||
ast::{BinaryOperator, Expression, UnaryOperator},
|
||||
AstKind,
|
||||
};
|
||||
use oxc_ast::{ast::Expression, AstKind};
|
||||
use oxc_diagnostics::{
|
||||
miette::{self, Diagnostic},
|
||||
thiserror::{self, Error},
|
||||
};
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};
|
||||
|
||||
use crate::{context::LintContext, fixer::Fix, rule::Rule, AstNode};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use oxc_ast::{
|
||||
ast::{
|
||||
AssignmentExpression, AssignmentOperator, AssignmentTarget, BinaryOperator, Expression,
|
||||
IdentifierReference, SimpleAssignmentTarget, UnaryOperator, UpdateOperator,
|
||||
AssignmentExpression, AssignmentTarget, Expression, IdentifierReference,
|
||||
SimpleAssignmentTarget,
|
||||
},
|
||||
AstKind,
|
||||
};
|
||||
|
|
@ -11,6 +11,7 @@ use oxc_diagnostics::{
|
|||
};
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator, UpdateOperator};
|
||||
|
||||
use crate::{context::LintContext, rule::Rule, AstNode};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
use oxc_ast::{ast::BinaryOperator, AstKind};
|
||||
use oxc_ast::AstKind;
|
||||
use oxc_diagnostics::{
|
||||
miette::{self, Diagnostic},
|
||||
thiserror::Error,
|
||||
};
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
use oxc_syntax::operator::BinaryOperator;
|
||||
|
||||
use crate::{context::LintContext, rule::Rule, AstNode};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
use oxc_ast::{
|
||||
ast::{BinaryOperator, Expression, UnaryOperator},
|
||||
AstKind,
|
||||
};
|
||||
use oxc_ast::{ast::Expression, AstKind};
|
||||
use oxc_diagnostics::{
|
||||
miette::{self, Diagnostic},
|
||||
thiserror::{self, Error},
|
||||
};
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};
|
||||
|
||||
use crate::{context::LintContext, rule::Rule, AstNode};
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use oxc_diagnostics::{
|
|||
};
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator};
|
||||
|
||||
use crate::{
|
||||
ast_util::{self, IsConstant},
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use oxc_ast::{
|
||||
ast::{BinaryExpression, BinaryOperator, Expression, UnaryOperator},
|
||||
ast::{BinaryExpression, Expression},
|
||||
AstKind,
|
||||
};
|
||||
use oxc_diagnostics::{
|
||||
|
|
@ -9,6 +9,7 @@ use oxc_diagnostics::{
|
|||
use oxc_formatter::Gen;
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::{GetSpan, Span};
|
||||
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};
|
||||
|
||||
use crate::{context::LintContext, fixer::Fix, rule::Rule, AstNode};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
use oxc_ast::{
|
||||
ast::{Expression, UnaryOperator},
|
||||
AstKind,
|
||||
};
|
||||
use oxc_ast::{ast::Expression, AstKind};
|
||||
use oxc_diagnostics::{
|
||||
miette::{self, Diagnostic},
|
||||
thiserror::Error,
|
||||
};
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::{GetSpan, Span};
|
||||
use oxc_syntax::operator::UnaryOperator;
|
||||
use phf::{phf_map, Map};
|
||||
|
||||
use crate::{ast_util::calculate_hash, context::LintContext, fixer::Fix, rule::Rule, AstNode};
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ oxc_ast = { workspace = true }
|
|||
oxc_ast_lower = { workspace = true }
|
||||
oxc_hir = { workspace = true }
|
||||
oxc_semantic = { workspace = true }
|
||||
oxc_syntax = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
walkdir = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use oxc_allocator::{Allocator, Vec};
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_hir::{hir::*, HirBuilder, VisitMut};
|
||||
use oxc_span::Span;
|
||||
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};
|
||||
|
||||
#[allow(clippy::struct_excessive_bools)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
use oxc_allocator::{Box, Vec};
|
||||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_hir::hir::*;
|
||||
use oxc_syntax::operator::{
|
||||
AssignmentOperator, BinaryOperator, LogicalOperator, Operator, UnaryOperator, UpdateOperator,
|
||||
};
|
||||
|
||||
use super::{Printer, Separator};
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@ use std::rc::Rc;
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_hir::hir::*;
|
||||
use oxc_semantic::SymbolTable;
|
||||
use oxc_syntax::operator::{
|
||||
AssignmentOperator, BinaryOperator, LogicalOperator, Operator, UnaryOperator, UpdateOperator,
|
||||
};
|
||||
|
||||
use self::gen::Gen;
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ doctest = false
|
|||
oxc_allocator = { workspace = true }
|
||||
oxc_span = { workspace = true }
|
||||
oxc_ast = { workspace = true }
|
||||
oxc_syntax = { workspace = true }
|
||||
oxc_diagnostics = { workspace = true }
|
||||
|
||||
bitflags = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use oxc_allocator::Box;
|
|||
use oxc_ast::ast::*;
|
||||
use oxc_diagnostics::Result;
|
||||
use oxc_span::{Atom, Span};
|
||||
use oxc_syntax::operator::BinaryOperator;
|
||||
|
||||
use super::function::IsParenthesizedArrowFunction;
|
||||
use super::grammar::CoverGrammar;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use oxc_allocator::Box;
|
|||
use oxc_ast::ast::*;
|
||||
use oxc_diagnostics::Result;
|
||||
use oxc_span::Span;
|
||||
use oxc_syntax::operator::AssignmentOperator;
|
||||
|
||||
use super::list::ObjectExpressionProperties;
|
||||
use crate::{diagnostics, lexer::Kind, list::SeparatedList, Parser};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
use oxc_ast::ast::*;
|
||||
use oxc_syntax::operator::{
|
||||
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
|
||||
};
|
||||
|
||||
use crate::lexer::Kind;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use bitflags::bitflags;
|
|||
use oxc_allocator::{Box, Vec};
|
||||
use oxc_ast::ast::*;
|
||||
use oxc_diagnostics::Result;
|
||||
use oxc_syntax::operator::UnaryOperator;
|
||||
|
||||
use super::list::{
|
||||
TSInterfaceOrObjectBodyList, TSTupleElementList, TSTypeArgumentList, TSTypeParameterList,
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ repository.workspace = true
|
|||
[dependencies]
|
||||
oxc_span = { workspace = true }
|
||||
oxc_ast = { workspace = true }
|
||||
oxc_syntax = { workspace = true }
|
||||
oxc_diagnostics = { workspace = true }
|
||||
|
||||
indextree = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use oxc_diagnostics::{
|
|||
thiserror::{self, Error},
|
||||
};
|
||||
use oxc_span::{Atom, GetSpan, Span};
|
||||
use oxc_syntax::operator::{BinaryOperator, LogicalOperator, UnaryOperator};
|
||||
use phf::{phf_set, Set};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
|
|
|
|||
18
crates/oxc_syntax/Cargo.toml
Normal file
18
crates/oxc_syntax/Cargo.toml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
[package]
|
||||
name = "oxc_syntax"
|
||||
version = "0.0.0"
|
||||
publish = false
|
||||
authors.workspace = true
|
||||
description.workspace = true
|
||||
edition.workspace = true
|
||||
homepage.workspace = true
|
||||
keywords.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde = { workspace = true, features = ["derive"], optional = true }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
serde = ["dep:serde"]
|
||||
3
crates/oxc_syntax/src/lib.rs
Normal file
3
crates/oxc_syntax/src/lib.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
//! Common code for JavaScript Syntax
|
||||
|
||||
pub mod operator;
|
||||
Loading…
Reference in a new issue