From ced651f68061c24d62c705a1266616609b166b16 Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Tue, 18 Mar 2025 18:38:17 -0700 Subject: [PATCH 1/6] Add regex feature to completely remove regex --- crates/oxc/Cargo.toml | 5 +++- crates/oxc/src/lib.rs | 1 + crates/oxc_linter/Cargo.toml | 2 +- crates/oxc_parser/Cargo.toml | 4 ++- crates/oxc_parser/src/js/expression.rs | 37 ++++++++++++++++++-------- crates/oxc_parser/src/lib.rs | 2 ++ napi/playground/Cargo.toml | 2 +- tasks/benchmark/Cargo.toml | 2 +- 8 files changed, 39 insertions(+), 16 deletions(-) diff --git a/crates/oxc/Cargo.toml b/crates/oxc/Cargo.toml index 8e6fc08e2292f..9e4e98abc5792 100644 --- a/crates/oxc/Cargo.toml +++ b/crates/oxc/Cargo.toml @@ -29,7 +29,7 @@ required-features = ["full"] oxc_allocator = { workspace = true } oxc_ast = { workspace = true } oxc_parser = { workspace = true } -oxc_regular_expression = { workspace = true } +oxc_regular_expression = { workspace = true, optional = true } oxc_ast_visit = { workspace = true, optional = true } oxc_cfg = { workspace = true, optional = true } @@ -53,8 +53,11 @@ full = [ "isolated_declarations", "ast_visit", "cfg", + "regex", ] +regex = ["oxc_regular_expression", "oxc_parser/regex"] + semantic = ["oxc_semantic"] transformer = ["oxc_transformer"] minifier = ["oxc_mangler", "oxc_minifier"] diff --git a/crates/oxc/src/lib.rs b/crates/oxc/src/lib.rs index e2a54bd6b4990..0bb18718275de 100644 --- a/crates/oxc/src/lib.rs +++ b/crates/oxc/src/lib.rs @@ -39,6 +39,7 @@ pub mod parser { pub use oxc_parser::*; } +#[cfg(feature = "regex")] pub mod regular_expression { #[doc(inline)] pub use oxc_regular_expression::*; diff --git a/crates/oxc_linter/Cargo.toml b/crates/oxc_linter/Cargo.toml index 3136a20719607..3d66bf768604c 100644 --- a/crates/oxc_linter/Cargo.toml +++ b/crates/oxc_linter/Cargo.toml @@ -29,7 +29,7 @@ oxc_diagnostics = { workspace = true } oxc_ecmascript = { workspace = true } oxc_index = { workspace = true, features = ["serde"] } oxc_macros = { workspace = true } -oxc_parser = { workspace = true } +oxc_parser = { workspace = true, features = ["regex"] } oxc_regular_expression = { workspace = true } oxc_resolver = { workspace = true } oxc_semantic = { workspace = true } diff --git a/crates/oxc_parser/Cargo.toml b/crates/oxc_parser/Cargo.toml index bb37450cd9844..3af28b337c75b 100644 --- a/crates/oxc_parser/Cargo.toml +++ b/crates/oxc_parser/Cargo.toml @@ -23,7 +23,7 @@ oxc_allocator = { workspace = true } oxc_ast = { workspace = true } oxc_diagnostics = { workspace = true } oxc_ecmascript = { workspace = true } -oxc_regular_expression = { workspace = true } +oxc_regular_expression = { workspace = true, optional = true } oxc_span = { workspace = true } oxc_syntax = { workspace = true } @@ -45,3 +45,5 @@ pico-args = { workspace = true } [features] # Expose Lexer for benchmarks benchmarking = [] +# Parse regex +regex = ["oxc_regular_expression"] diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 55403028e5008..0bbac3173db8a 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -2,6 +2,7 @@ use cow_utils::CowUtils; use oxc_allocator::Box; use oxc_ast::ast::*; use oxc_diagnostics::Result; +#[cfg(feature = "regex")] use oxc_regular_expression::ast::Pattern; use oxc_span::{Atom, GetSpan, Span}; use oxc_syntax::{ @@ -342,18 +343,31 @@ impl<'a> ParserImpl<'a> { let flags_text = &self.source_text[flags_start as usize..self.cur_token().end as usize]; let raw = self.cur_src(); self.bump_any(); + // Parse pattern if options is enabled and also flags are valid - let pattern = (self.options.parse_regular_expression && !flags_error) - .then_some(()) - .map(|()| { - self.parse_regex_pattern(pattern_start, pattern_text, flags_start, flags_text) - }) - .map_or_else( - || RegExpPattern::Raw(pattern_text), - |pat| { - pat.map_or_else(|| RegExpPattern::Invalid(pattern_text), RegExpPattern::Pattern) - }, - ); + #[cfg(feature = "regex")] + let pattern = { + (self.options.parse_regular_expression && !flags_error) + .then_some(()) + .map(|()| { + self.parse_regex_pattern(pattern_start, pattern_text, flags_start, flags_text) + }) + .map_or_else( + || RegExpPattern::Raw(pattern_text), + |pat| { + pat.map_or_else( + || RegExpPattern::Invalid(pattern_text), + RegExpPattern::Pattern, + ) + }, + ) + }; + #[cfg(not(feature = "regex"))] + let pattern = { + let _ = (flags_start, flags_text, flags_error); + RegExpPattern::Raw(pattern_text) + }; + Ok(self.ast.reg_exp_literal( self.end_span(span), RegExp { pattern, flags }, @@ -361,6 +375,7 @@ impl<'a> ParserImpl<'a> { )) } + #[cfg(feature = "regex")] fn parse_regex_pattern( &mut self, pattern_span_offset: u32, diff --git a/crates/oxc_parser/src/lib.rs b/crates/oxc_parser/src/lib.rs index 48121dc68d0b7..a2fe462de15ea 100644 --- a/crates/oxc_parser/src/lib.rs +++ b/crates/oxc_parser/src/lib.rs @@ -188,6 +188,7 @@ pub struct ParseOptions { /// Whether to parse regular expressions or not. /// /// Default: `false` + #[cfg(feature = "regex")] pub parse_regular_expression: bool, /// Allow [`return`] statements outside of functions. @@ -223,6 +224,7 @@ pub struct ParseOptions { impl Default for ParseOptions { fn default() -> Self { Self { + #[cfg(feature = "regex")] parse_regular_expression: false, allow_return_outside_function: false, preserve_parens: true, diff --git a/napi/playground/Cargo.toml b/napi/playground/Cargo.toml index 7cebc8c8076e6..a313fc2cc0cf2 100644 --- a/napi/playground/Cargo.toml +++ b/napi/playground/Cargo.toml @@ -22,7 +22,7 @@ test = false doctest = false [dependencies] -oxc = { workspace = true, features = ["ast_visit", "codegen", "minifier", "semantic", "serialize", "transformer", "isolated_declarations"] } +oxc = { workspace = true, features = ["ast_visit", "codegen", "minifier", "semantic", "serialize", "transformer", "isolated_declarations", "regex"] } oxc_index = { workspace = true } oxc_linter = { workspace = true } oxc_napi = { workspace = true } diff --git a/tasks/benchmark/Cargo.toml b/tasks/benchmark/Cargo.toml index f51baceec1777..7cf948614dd3c 100644 --- a/tasks/benchmark/Cargo.toml +++ b/tasks/benchmark/Cargo.toml @@ -72,7 +72,7 @@ oxc_isolated_declarations = { workspace = true, optional = true } oxc_linter = { workspace = true, optional = true } oxc_mangler = { workspace = true, optional = true } oxc_minifier = { workspace = true, optional = true } -oxc_parser = { workspace = true, features = ["benchmarking"], optional = true } +oxc_parser = { workspace = true, features = ["benchmarking", "regex"], optional = true } oxc_prettier = { workspace = true, optional = true } oxc_semantic = { workspace = true, optional = true } oxc_span = { workspace = true, optional = true, features = ["schemars", "serialize"] } From 75329f6cd9f91c73e1fc92f1b240b50878668c0e Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 19 Mar 2025 20:40:12 +0800 Subject: [PATCH 2/6] u --- Cargo.toml | 2 +- crates/oxc/Cargo.toml | 6 +++--- crates/oxc/src/lib.rs | 2 +- crates/oxc_linter/Cargo.toml | 2 +- crates/oxc_parser/Cargo.toml | 5 +++-- crates/oxc_parser/src/js/expression.rs | 8 ++++---- crates/oxc_parser/src/lib.rs | 4 ++-- napi/playground/Cargo.toml | 2 +- tasks/benchmark/Cargo.toml | 2 +- 9 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index de7201e6e6179..aac100bf2e544 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,7 +118,7 @@ oxc_isolated_declarations = { version = "0.60.0", path = "crates/oxc_isolated_de oxc_mangler = { version = "0.60.0", path = "crates/oxc_mangler" } oxc_minifier = { version = "0.60.0", path = "crates/oxc_minifier" } oxc_napi = { version = "0.60.0", path = "crates/oxc_napi" } -oxc_parser = { version = "0.60.0", path = "crates/oxc_parser" } +oxc_parser = { version = "0.60.0", path = "crates/oxc_parser", features = ["regular_expression"] } oxc_parser_napi = { version = "0.60.0", path = "napi/parser" } oxc_regular_expression = { version = "0.60.0", path = "crates/oxc_regular_expression" } oxc_semantic = { version = "0.60.0", path = "crates/oxc_semantic" } diff --git a/crates/oxc/Cargo.toml b/crates/oxc/Cargo.toml index 9e4e98abc5792..70c1381c16af0 100644 --- a/crates/oxc/Cargo.toml +++ b/crates/oxc/Cargo.toml @@ -28,7 +28,7 @@ required-features = ["full"] [dependencies] oxc_allocator = { workspace = true } oxc_ast = { workspace = true } -oxc_parser = { workspace = true } +oxc_parser = { workspace = true, features = [] } oxc_regular_expression = { workspace = true, optional = true } oxc_ast_visit = { workspace = true, optional = true } @@ -53,10 +53,10 @@ full = [ "isolated_declarations", "ast_visit", "cfg", - "regex", + "regular_expression", ] -regex = ["oxc_regular_expression", "oxc_parser/regex"] +regular_expression = ["oxc_regular_expression", "oxc_parser/regular_expression"] semantic = ["oxc_semantic"] transformer = ["oxc_transformer"] diff --git a/crates/oxc/src/lib.rs b/crates/oxc/src/lib.rs index 0bb18718275de..8f4c600daee0a 100644 --- a/crates/oxc/src/lib.rs +++ b/crates/oxc/src/lib.rs @@ -39,7 +39,7 @@ pub mod parser { pub use oxc_parser::*; } -#[cfg(feature = "regex")] +#[cfg(feature = "regular_expression")] pub mod regular_expression { #[doc(inline)] pub use oxc_regular_expression::*; diff --git a/crates/oxc_linter/Cargo.toml b/crates/oxc_linter/Cargo.toml index 3d66bf768604c..3136a20719607 100644 --- a/crates/oxc_linter/Cargo.toml +++ b/crates/oxc_linter/Cargo.toml @@ -29,7 +29,7 @@ oxc_diagnostics = { workspace = true } oxc_ecmascript = { workspace = true } oxc_index = { workspace = true, features = ["serde"] } oxc_macros = { workspace = true } -oxc_parser = { workspace = true, features = ["regex"] } +oxc_parser = { workspace = true } oxc_regular_expression = { workspace = true } oxc_resolver = { workspace = true } oxc_semantic = { workspace = true } diff --git a/crates/oxc_parser/Cargo.toml b/crates/oxc_parser/Cargo.toml index 3af28b337c75b..267e87d1c457d 100644 --- a/crates/oxc_parser/Cargo.toml +++ b/crates/oxc_parser/Cargo.toml @@ -43,7 +43,8 @@ oxc_ast_visit = { workspace = true, features = ["serialize"] } pico-args = { workspace = true } [features] +default = ["regular_expression"] +# Parse regex +regular_expression = ["oxc_regular_expression"] # Expose Lexer for benchmarks benchmarking = [] -# Parse regex -regex = ["oxc_regular_expression"] diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 0bbac3173db8a..61a1e1794b544 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -2,7 +2,7 @@ use cow_utils::CowUtils; use oxc_allocator::Box; use oxc_ast::ast::*; use oxc_diagnostics::Result; -#[cfg(feature = "regex")] +#[cfg(feature = "regular_expression")] use oxc_regular_expression::ast::Pattern; use oxc_span::{Atom, GetSpan, Span}; use oxc_syntax::{ @@ -345,7 +345,7 @@ impl<'a> ParserImpl<'a> { self.bump_any(); // Parse pattern if options is enabled and also flags are valid - #[cfg(feature = "regex")] + #[cfg(feature = "regular_expression")] let pattern = { (self.options.parse_regular_expression && !flags_error) .then_some(()) @@ -362,7 +362,7 @@ impl<'a> ParserImpl<'a> { }, ) }; - #[cfg(not(feature = "regex"))] + #[cfg(not(feature = "regular_expression"))] let pattern = { let _ = (flags_start, flags_text, flags_error); RegExpPattern::Raw(pattern_text) @@ -375,7 +375,7 @@ impl<'a> ParserImpl<'a> { )) } - #[cfg(feature = "regex")] + #[cfg(feature = "regular_expression")] fn parse_regex_pattern( &mut self, pattern_span_offset: u32, diff --git a/crates/oxc_parser/src/lib.rs b/crates/oxc_parser/src/lib.rs index a2fe462de15ea..930231efd391d 100644 --- a/crates/oxc_parser/src/lib.rs +++ b/crates/oxc_parser/src/lib.rs @@ -188,7 +188,7 @@ pub struct ParseOptions { /// Whether to parse regular expressions or not. /// /// Default: `false` - #[cfg(feature = "regex")] + #[cfg(feature = "regular_expression")] pub parse_regular_expression: bool, /// Allow [`return`] statements outside of functions. @@ -224,7 +224,7 @@ pub struct ParseOptions { impl Default for ParseOptions { fn default() -> Self { Self { - #[cfg(feature = "regex")] + #[cfg(feature = "regular_expression")] parse_regular_expression: false, allow_return_outside_function: false, preserve_parens: true, diff --git a/napi/playground/Cargo.toml b/napi/playground/Cargo.toml index a313fc2cc0cf2..3a0d5084e409a 100644 --- a/napi/playground/Cargo.toml +++ b/napi/playground/Cargo.toml @@ -22,7 +22,7 @@ test = false doctest = false [dependencies] -oxc = { workspace = true, features = ["ast_visit", "codegen", "minifier", "semantic", "serialize", "transformer", "isolated_declarations", "regex"] } +oxc = { workspace = true, features = ["ast_visit", "codegen", "minifier", "semantic", "serialize", "transformer", "isolated_declarations", "regular_expression"] } oxc_index = { workspace = true } oxc_linter = { workspace = true } oxc_napi = { workspace = true } diff --git a/tasks/benchmark/Cargo.toml b/tasks/benchmark/Cargo.toml index 7cf948614dd3c..821606bdf30ee 100644 --- a/tasks/benchmark/Cargo.toml +++ b/tasks/benchmark/Cargo.toml @@ -72,7 +72,7 @@ oxc_isolated_declarations = { workspace = true, optional = true } oxc_linter = { workspace = true, optional = true } oxc_mangler = { workspace = true, optional = true } oxc_minifier = { workspace = true, optional = true } -oxc_parser = { workspace = true, features = ["benchmarking", "regex"], optional = true } +oxc_parser = { workspace = true, features = ["benchmarking", "regular_expression"], optional = true } oxc_prettier = { workspace = true, optional = true } oxc_semantic = { workspace = true, optional = true } oxc_span = { workspace = true, optional = true, features = ["schemars", "serialize"] } From c1ccb29276c291cbda9855b89ab39997f381ccce Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 19 Mar 2025 20:41:19 +0800 Subject: [PATCH 3/6] u --- crates/oxc/Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/oxc/Cargo.toml b/crates/oxc/Cargo.toml index 70c1381c16af0..617df0635cfff 100644 --- a/crates/oxc/Cargo.toml +++ b/crates/oxc/Cargo.toml @@ -56,8 +56,6 @@ full = [ "regular_expression", ] -regular_expression = ["oxc_regular_expression", "oxc_parser/regular_expression"] - semantic = ["oxc_semantic"] transformer = ["oxc_transformer"] minifier = ["oxc_mangler", "oxc_minifier"] @@ -66,6 +64,7 @@ mangler = ["oxc_mangler"] cfg = ["oxc_cfg"] isolated_declarations = ["oxc_isolated_declarations"] ast_visit = ["oxc_ast_visit"] +regular_expression = ["oxc_regular_expression", "oxc_parser/regular_expression"] serialize = [ "oxc_allocator/from_raw_parts", From 4182fe8029de5678a7a1b6fb41b3e1549888383a Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 19 Mar 2025 20:42:40 +0800 Subject: [PATCH 4/6] u --- crates/oxc/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/oxc/Cargo.toml b/crates/oxc/Cargo.toml index 617df0635cfff..d89efa704f813 100644 --- a/crates/oxc/Cargo.toml +++ b/crates/oxc/Cargo.toml @@ -44,6 +44,7 @@ oxc_syntax = { workspace = true } oxc_transformer = { workspace = true, optional = true } [features] +default = ["regular_expression"] full = [ "codegen", "mangler", From 7afe176bc79761bf4345110228e3b52fe4a3baab Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 19 Mar 2025 20:43:06 +0800 Subject: [PATCH 5/6] u --- crates/oxc/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/oxc/Cargo.toml b/crates/oxc/Cargo.toml index d89efa704f813..67c5494fead59 100644 --- a/crates/oxc/Cargo.toml +++ b/crates/oxc/Cargo.toml @@ -45,6 +45,7 @@ oxc_transformer = { workspace = true, optional = true } [features] default = ["regular_expression"] + full = [ "codegen", "mangler", From f2ee8861661cec964b93ce1eb86db775948e24cc Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 19 Mar 2025 20:44:46 +0800 Subject: [PATCH 6/6] u --- napi/parser/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napi/parser/Cargo.toml b/napi/parser/Cargo.toml index d9c912fe1829c..a8b0cea024d21 100644 --- a/napi/parser/Cargo.toml +++ b/napi/parser/Cargo.toml @@ -22,7 +22,7 @@ test = false doctest = false [dependencies] -oxc = { workspace = true, features = ["ast_visit", "semantic", "serialize"] } +oxc = { workspace = true, features = ["ast_visit", "regular_expression", "semantic", "serialize"] } oxc_ast_macros = { workspace = true } oxc_estree = { workspace = true } oxc_napi = { workspace = true }