From a8af5de8f5128aadabd0d1124e9043f85617cf99 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 29 Apr 2024 18:54:35 +0800 Subject: [PATCH] refactor(syntax): move number related functions to number module (#3130) --- crates/oxc_ast/src/ast/literal.rs | 2 +- crates/oxc_ast/src/ast_builder.rs | 2 +- crates/oxc_codegen/src/gen.rs | 2 +- .../rules/unicorn/numeric_separators_style.rs | 6 ++-- crates/oxc_minifier/src/compressor/fold.rs | 2 +- crates/oxc_minifier/src/compressor/mod.rs | 2 +- crates/oxc_parser/src/js/expression.rs | 6 +++- crates/oxc_semantic/src/checker/javascript.rs | 2 +- crates/oxc_syntax/src/lib.rs | 30 +------------------ crates/oxc_syntax/src/number.rs | 28 +++++++++++++++++ .../src/react/jsx_source/mod.rs | 2 +- crates/oxc_transformer/src/typescript/enum.rs | 2 +- 12 files changed, 45 insertions(+), 41 deletions(-) create mode 100644 crates/oxc_syntax/src/number.rs diff --git a/crates/oxc_ast/src/ast/literal.rs b/crates/oxc_ast/src/ast/literal.rs index 8edf72289..fd3d9c5a2 100644 --- a/crates/oxc_ast/src/ast/literal.rs +++ b/crates/oxc_ast/src/ast/literal.rs @@ -10,7 +10,7 @@ use std::{ use bitflags::bitflags; use oxc_span::{Atom, Span}; -use oxc_syntax::{BigintBase, NumberBase}; +use oxc_syntax::number::{BigintBase, NumberBase}; #[cfg(feature = "serialize")] use serde::Serialize; #[cfg(feature = "serialize")] diff --git a/crates/oxc_ast/src/ast_builder.rs b/crates/oxc_ast/src/ast_builder.rs index 524aa6caa..c12ab5c29 100644 --- a/crates/oxc_ast/src/ast_builder.rs +++ b/crates/oxc_ast/src/ast_builder.rs @@ -10,10 +10,10 @@ use std::mem; use oxc_allocator::{Allocator, Box, String, Vec}; use oxc_span::{Atom, GetSpan, SourceType, Span, SPAN}; use oxc_syntax::{ + number::{BigintBase, NumberBase}, operator::{ AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator, }, - BigintBase, NumberBase, }; #[allow(clippy::wildcard_imports)] diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 6f0f65bb4..e91ae1af1 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -5,9 +5,9 @@ use oxc_span::GetSpan; use oxc_syntax::{ identifier::{LS, PS}, keyword::is_keyword, + number::NumberBase, operator::{BinaryOperator, UnaryOperator}, precedence::{GetPrecedence, Precedence}, - NumberBase, }; use super::{Codegen, Context, Operator, Separator}; diff --git a/crates/oxc_linter/src/rules/unicorn/numeric_separators_style.rs b/crates/oxc_linter/src/rules/unicorn/numeric_separators_style.rs index ab7f395e6..6eba1cc5b 100644 --- a/crates/oxc_linter/src/rules/unicorn/numeric_separators_style.rs +++ b/crates/oxc_linter/src/rules/unicorn/numeric_separators_style.rs @@ -152,18 +152,18 @@ impl Rule for NumericSeparatorsStyle { impl NumericSeparatorsStyle { fn format_number(&self, number: &NumericLiteral) -> String { - use oxc_syntax::NumberBase; + use oxc_syntax::number::NumberBase; match number.base { NumberBase::Binary => self.format_binary(number.raw), - NumberBase::Decimal | oxc_syntax::NumberBase::Float => self.format_decimal(number.raw), + NumberBase::Decimal | NumberBase::Float => self.format_decimal(number.raw), NumberBase::Hex => self.format_hex(number.raw), NumberBase::Octal => self.format_octal(number.raw), } } fn format_bigint(&self, number: &BigIntLiteral, raw: &str) -> String { - use oxc_syntax::BigintBase; + use oxc_syntax::number::BigintBase; let raw_without_bigint_n_suffix = &raw[..raw.len() - 1]; let mut formatted = match number.base { diff --git a/crates/oxc_minifier/src/compressor/fold.rs b/crates/oxc_minifier/src/compressor/fold.rs index 7f24e3772..e310a7a2b 100644 --- a/crates/oxc_minifier/src/compressor/fold.rs +++ b/crates/oxc_minifier/src/compressor/fold.rs @@ -9,8 +9,8 @@ use num_bigint::BigInt; use oxc_ast::ast::*; use oxc_span::{Atom, GetSpan, Span}; use oxc_syntax::{ + number::NumberBase, operator::{BinaryOperator, LogicalOperator, UnaryOperator}, - NumberBase, }; use super::ast_util::{ diff --git a/crates/oxc_minifier/src/compressor/mod.rs b/crates/oxc_minifier/src/compressor/mod.rs index 07cc899d1..86dc04cbf 100644 --- a/crates/oxc_minifier/src/compressor/mod.rs +++ b/crates/oxc_minifier/src/compressor/mod.rs @@ -15,9 +15,9 @@ use oxc_ast::visit::walk_mut::{ use oxc_ast::{ast::*, AstBuilder, VisitMut}; use oxc_span::Span; use oxc_syntax::{ + number::NumberBase, operator::{BinaryOperator, UnaryOperator}, precedence::GetPrecedence, - NumberBase, }; pub use self::options::CompressOptions; diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 096960ee6..7d90b6a26 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -4,7 +4,11 @@ use oxc_allocator::Box; use oxc_ast::ast::*; use oxc_diagnostics::Result; use oxc_span::{Atom, Span}; -use oxc_syntax::{operator::BinaryOperator, precedence::Precedence, BigintBase, NumberBase}; +use oxc_syntax::{ + number::{BigintBase, NumberBase}, + operator::BinaryOperator, + precedence::Precedence, +}; use super::{ function::IsParenthesizedArrowFunction, diff --git a/crates/oxc_semantic/src/checker/javascript.rs b/crates/oxc_semantic/src/checker/javascript.rs index 0ccb7554c..bd6a44b05 100644 --- a/crates/oxc_semantic/src/checker/javascript.rs +++ b/crates/oxc_semantic/src/checker/javascript.rs @@ -11,8 +11,8 @@ use oxc_diagnostics::{ use oxc_span::{Atom, CompactStr, GetSpan, ModuleKind, Span}; use oxc_syntax::{ module_record::ExportLocalName, + number::NumberBase, operator::{AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator}, - NumberBase, }; use phf::{phf_set, Set}; use rustc_hash::FxHashMap; diff --git a/crates/oxc_syntax/src/lib.rs b/crates/oxc_syntax/src/lib.rs index 91f0bb368..c495c78f1 100644 --- a/crates/oxc_syntax/src/lib.rs +++ b/crates/oxc_syntax/src/lib.rs @@ -6,38 +6,10 @@ pub mod keyword; pub mod module_graph_visitor; pub mod module_record; pub mod node; +pub mod number; pub mod operator; pub mod precedence; pub mod reference; pub mod scope; pub mod symbol; pub mod xml_entities; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum NumberBase { - Float, - Decimal, - Binary, - Octal, - Hex, -} - -impl NumberBase { - pub fn is_base_10(&self) -> bool { - matches!(self, Self::Float | Self::Decimal) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum BigintBase { - Decimal, - Binary, - Octal, - Hex, -} - -impl BigintBase { - pub fn is_base_10(&self) -> bool { - self == &Self::Decimal - } -} diff --git a/crates/oxc_syntax/src/number.rs b/crates/oxc_syntax/src/number.rs new file mode 100644 index 000000000..5603b1b59 --- /dev/null +++ b/crates/oxc_syntax/src/number.rs @@ -0,0 +1,28 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum NumberBase { + Float, + Decimal, + Binary, + Octal, + Hex, +} + +impl NumberBase { + pub fn is_base_10(&self) -> bool { + matches!(self, Self::Float | Self::Decimal) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum BigintBase { + Decimal, + Binary, + Octal, + Hex, +} + +impl BigintBase { + pub fn is_base_10(&self) -> bool { + self == &Self::Decimal + } +} diff --git a/crates/oxc_transformer/src/react/jsx_source/mod.rs b/crates/oxc_transformer/src/react/jsx_source/mod.rs index 656d39d66..64e838a26 100644 --- a/crates/oxc_transformer/src/react/jsx_source/mod.rs +++ b/crates/oxc_transformer/src/react/jsx_source/mod.rs @@ -4,7 +4,7 @@ use std::rc::Rc; use oxc_ast::ast::*; use oxc_span::{Span, SPAN}; -use oxc_syntax::NumberBase; +use oxc_syntax::number::NumberBase; use crate::context::Ctx; diff --git a/crates/oxc_transformer/src/typescript/enum.rs b/crates/oxc_transformer/src/typescript/enum.rs index ad649c657..d2700af21 100644 --- a/crates/oxc_transformer/src/typescript/enum.rs +++ b/crates/oxc_transformer/src/typescript/enum.rs @@ -4,8 +4,8 @@ use oxc_allocator::{Box, Vec}; use oxc_ast::{ast::*, visit::walk_mut, VisitMut}; use oxc_span::{Atom, SPAN}; use oxc_syntax::{ + number::NumberBase, operator::{AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator}, - NumberBase, }; use rustc_hash::FxHashMap; use ryu_js::Buffer;