diff --git a/Cargo.lock b/Cargo.lock index e38049378..29dc02019 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 61e187955..d30c09d5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/crates/oxc_ast/Cargo.toml b/crates/oxc_ast/Cargo.toml index d925c7f87..179673fc5 100644 --- a/crates/oxc_ast/Cargo.toml +++ b/crates/oxc_ast/Cargo.toml @@ -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"] diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 4f5a93f55..cd050adf3 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -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; diff --git a/crates/oxc_ast/src/ast/mod.rs b/crates/oxc_ast/src/ast/mod.rs index 2b54f9da4..355228bf8 100644 --- a/crates/oxc_ast/src/ast/mod.rs +++ b/crates/oxc_ast/src/ast/mod.rs @@ -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::*; diff --git a/crates/oxc_ast/src/ast_builder.rs b/crates/oxc_ast/src/ast_builder.rs index 2aee29617..3be1001f6 100644 --- a/crates/oxc_ast/src/ast_builder.rs +++ b/crates/oxc_ast/src/ast_builder.rs @@ -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}; diff --git a/crates/oxc_ast_lower/Cargo.toml b/crates/oxc_ast_lower/Cargo.toml index c70709d8f..0c2ce91be 100644 --- a/crates/oxc_ast_lower/Cargo.toml +++ b/crates/oxc_ast_lower/Cargo.toml @@ -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 } diff --git a/crates/oxc_ast_lower/src/lib.rs b/crates/oxc_ast_lower/src/lib.rs index e60615127..808cbe913 100644 --- a/crates/oxc_ast_lower/src/lib.rs +++ b/crates/oxc_ast_lower/src/lib.rs @@ -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> { diff --git a/crates/oxc_formatter/Cargo.toml b/crates/oxc_formatter/Cargo.toml index 858822fb1..165da0357 100644 --- a/crates/oxc_formatter/Cargo.toml +++ b/crates/oxc_formatter/Cargo.toml @@ -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 } diff --git a/crates/oxc_formatter/src/gen.rs b/crates/oxc_formatter/src/gen.rs index 3f1f3997e..3983b0233 100644 --- a/crates/oxc_formatter/src/gen.rs +++ b/crates/oxc_formatter/src/gen.rs @@ -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}; diff --git a/crates/oxc_formatter/src/lib.rs b/crates/oxc_formatter/src/lib.rs index ce954c807..8c391c07b 100644 --- a/crates/oxc_formatter/src/lib.rs +++ b/crates/oxc_formatter/src/lib.rs @@ -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; diff --git a/crates/oxc_hir/Cargo.toml b/crates/oxc_hir/Cargo.toml index e4d871565..c846e16b8 100644 --- a/crates/oxc_hir/Cargo.toml +++ b/crates/oxc_hir/Cargo.toml @@ -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 } diff --git a/crates/oxc_hir/src/hir.rs b/crates/oxc_hir/src/hir.rs index 038c21950..7e8f1f9cc 100644 --- a/crates/oxc_hir/src/hir.rs +++ b/crates/oxc_hir/src/hir.rs @@ -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 for Operator { - fn from(op: AssignmentOperator) -> Self { - Self::AssignmentOperator(op) - } -} - -impl From for Operator { - fn from(op: BinaryOperator) -> Self { - Self::BinaryOperator(op) - } -} - -impl From for Operator { - fn from(op: LogicalOperator) -> Self { - Self::LogicalOperator(op) - } -} - -impl From for Operator { - fn from(op: UnaryOperator) -> Self { - Self::UnaryOperator(op) - } -} - -impl From 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> { diff --git a/crates/oxc_hir/src/hir_builder.rs b/crates/oxc_hir/src/hir_builder.rs index b8dd72334..ba161ec4c 100644 --- a/crates/oxc_hir/src/hir_builder.rs +++ b/crates/oxc_hir/src/hir_builder.rs @@ -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::*; diff --git a/crates/oxc_linter/Cargo.toml b/crates/oxc_linter/Cargo.toml index f5cf1fc13..b56c1d7e3 100644 --- a/crates/oxc_linter/Cargo.toml +++ b/crates/oxc_linter/Cargo.toml @@ -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 } diff --git a/crates/oxc_linter/src/ast_util.rs b/crates/oxc_linter/src/ast_util.rs index 74757774c..41a8ce835 100644 --- a/crates/oxc_linter/src/ast_util.rs +++ b/crates/oxc_linter/src/ast_util.rs @@ -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: &T) -> u64 { diff --git a/crates/oxc_linter/src/rules/deepscan/bad_bitwise_operator.rs b/crates/oxc_linter/src/rules/deepscan/bad_bitwise_operator.rs index f176f2ac6..6b0732383 100644 --- a/crates/oxc_linter/src/rules/deepscan/bad_bitwise_operator.rs +++ b/crates/oxc_linter/src/rules/deepscan/bad_bitwise_operator.rs @@ -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}; diff --git a/crates/oxc_linter/src/rules/eq_eq_eq.rs b/crates/oxc_linter/src/rules/eq_eq_eq.rs index f1e09c2a7..3368e1216 100644 --- a/crates/oxc_linter/src/rules/eq_eq_eq.rs +++ b/crates/oxc_linter/src/rules/eq_eq_eq.rs @@ -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}; diff --git a/crates/oxc_linter/src/rules/for_direction.rs b/crates/oxc_linter/src/rules/for_direction.rs index 0b7979ad6..01094c4c0 100644 --- a/crates/oxc_linter/src/rules/for_direction.rs +++ b/crates/oxc_linter/src/rules/for_direction.rs @@ -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}; diff --git a/crates/oxc_linter/src/rules/no_bitwise.rs b/crates/oxc_linter/src/rules/no_bitwise.rs index bba66db4e..695dd69fc 100644 --- a/crates/oxc_linter/src/rules/no_bitwise.rs +++ b/crates/oxc_linter/src/rules/no_bitwise.rs @@ -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}; diff --git a/crates/oxc_linter/src/rules/no_compare_neg_zero.rs b/crates/oxc_linter/src/rules/no_compare_neg_zero.rs index a7f61ed1d..80cbe36e6 100644 --- a/crates/oxc_linter/src/rules/no_compare_neg_zero.rs +++ b/crates/oxc_linter/src/rules/no_compare_neg_zero.rs @@ -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}; diff --git a/crates/oxc_linter/src/rules/no_constant_binary_expression.rs b/crates/oxc_linter/src/rules/no_constant_binary_expression.rs index 9838615c1..76a7f6391 100644 --- a/crates/oxc_linter/src/rules/no_constant_binary_expression.rs +++ b/crates/oxc_linter/src/rules/no_constant_binary_expression.rs @@ -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}, diff --git a/crates/oxc_linter/src/rules/no_unsafe_negation.rs b/crates/oxc_linter/src/rules/no_unsafe_negation.rs index 506132e14..109dd8453 100644 --- a/crates/oxc_linter/src/rules/no_unsafe_negation.rs +++ b/crates/oxc_linter/src/rules/no_unsafe_negation.rs @@ -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}; diff --git a/crates/oxc_linter/src/rules/valid_typeof.rs b/crates/oxc_linter/src/rules/valid_typeof.rs index c0aa98f69..74641577b 100644 --- a/crates/oxc_linter/src/rules/valid_typeof.rs +++ b/crates/oxc_linter/src/rules/valid_typeof.rs @@ -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}; diff --git a/crates/oxc_minifier/Cargo.toml b/crates/oxc_minifier/Cargo.toml index 75b8341eb..0bcff50c6 100644 --- a/crates/oxc_minifier/Cargo.toml +++ b/crates/oxc_minifier/Cargo.toml @@ -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 } diff --git a/crates/oxc_minifier/src/compressor/mod.rs b/crates/oxc_minifier/src/compressor/mod.rs index dba01624b..f98131f27 100644 --- a/crates/oxc_minifier/src/compressor/mod.rs +++ b/crates/oxc_minifier/src/compressor/mod.rs @@ -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)] diff --git a/crates/oxc_minifier/src/printer/gen.rs b/crates/oxc_minifier/src/printer/gen.rs index a145f315e..fb938a71a 100644 --- a/crates/oxc_minifier/src/printer/gen.rs +++ b/crates/oxc_minifier/src/printer/gen.rs @@ -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}; diff --git a/crates/oxc_minifier/src/printer/mod.rs b/crates/oxc_minifier/src/printer/mod.rs index 584f73b7d..599c5d2f9 100644 --- a/crates/oxc_minifier/src/printer/mod.rs +++ b/crates/oxc_minifier/src/printer/mod.rs @@ -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; diff --git a/crates/oxc_parser/Cargo.toml b/crates/oxc_parser/Cargo.toml index 92cfbd169..a6aa4dde0 100644 --- a/crates/oxc_parser/Cargo.toml +++ b/crates/oxc_parser/Cargo.toml @@ -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 } diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 98b0fe2f4..464f03f43 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -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; diff --git a/crates/oxc_parser/src/js/object.rs b/crates/oxc_parser/src/js/object.rs index d350bb3a9..9f9a21feb 100644 --- a/crates/oxc_parser/src/js/object.rs +++ b/crates/oxc_parser/src/js/object.rs @@ -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}; diff --git a/crates/oxc_parser/src/js/operator.rs b/crates/oxc_parser/src/js/operator.rs index b5d16b792..1bf27c709 100644 --- a/crates/oxc_parser/src/js/operator.rs +++ b/crates/oxc_parser/src/js/operator.rs @@ -1,4 +1,6 @@ -use oxc_ast::ast::*; +use oxc_syntax::operator::{ + AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator, +}; use crate::lexer::Kind; diff --git a/crates/oxc_parser/src/ts/types.rs b/crates/oxc_parser/src/ts/types.rs index 66d2eed65..0588308c5 100644 --- a/crates/oxc_parser/src/ts/types.rs +++ b/crates/oxc_parser/src/ts/types.rs @@ -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, diff --git a/crates/oxc_semantic/Cargo.toml b/crates/oxc_semantic/Cargo.toml index 7df20978c..b712d5a68 100644 --- a/crates/oxc_semantic/Cargo.toml +++ b/crates/oxc_semantic/Cargo.toml @@ -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 } diff --git a/crates/oxc_semantic/src/checker/javascript.rs b/crates/oxc_semantic/src/checker/javascript.rs index 7894eb6cb..80ab04d79 100644 --- a/crates/oxc_semantic/src/checker/javascript.rs +++ b/crates/oxc_semantic/src/checker/javascript.rs @@ -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; diff --git a/crates/oxc_syntax/Cargo.toml b/crates/oxc_syntax/Cargo.toml new file mode 100644 index 000000000..98523abc2 --- /dev/null +++ b/crates/oxc_syntax/Cargo.toml @@ -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"] diff --git a/crates/oxc_syntax/src/lib.rs b/crates/oxc_syntax/src/lib.rs new file mode 100644 index 000000000..0b5bcec3c --- /dev/null +++ b/crates/oxc_syntax/src/lib.rs @@ -0,0 +1,3 @@ +//! Common code for JavaScript Syntax + +pub mod operator; diff --git a/crates/oxc_ast/src/ast/operator.rs b/crates/oxc_syntax/src/operator.rs similarity index 100% rename from crates/oxc_ast/src/ast/operator.rs rename to crates/oxc_syntax/src/operator.rs