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 8e6fc08e2292f..67c5494fead59 100644 --- a/crates/oxc/Cargo.toml +++ b/crates/oxc/Cargo.toml @@ -28,8 +28,8 @@ required-features = ["full"] [dependencies] oxc_allocator = { workspace = true } oxc_ast = { workspace = true } -oxc_parser = { workspace = true } -oxc_regular_expression = { workspace = true } +oxc_parser = { workspace = true, features = [] } +oxc_regular_expression = { workspace = true, optional = true } oxc_ast_visit = { workspace = true, optional = true } oxc_cfg = { workspace = true, optional = true } @@ -44,6 +44,8 @@ oxc_syntax = { workspace = true } oxc_transformer = { workspace = true, optional = true } [features] +default = ["regular_expression"] + full = [ "codegen", "mangler", @@ -53,6 +55,7 @@ full = [ "isolated_declarations", "ast_visit", "cfg", + "regular_expression", ] semantic = ["oxc_semantic"] @@ -63,6 +66,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", diff --git a/crates/oxc/src/lib.rs b/crates/oxc/src/lib.rs index e2a54bd6b4990..8f4c600daee0a 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 = "regular_expression")] pub mod regular_expression { #[doc(inline)] pub use oxc_regular_expression::*; diff --git a/crates/oxc_parser/Cargo.toml b/crates/oxc_parser/Cargo.toml index bb37450cd9844..267e87d1c457d 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 } @@ -43,5 +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 = [] diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 55403028e5008..61a1e1794b544 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 = "regular_expression")] 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 = "regular_expression")] + 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 = "regular_expression"))] + 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 = "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 48121dc68d0b7..930231efd391d 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 = "regular_expression")] 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 = "regular_expression")] parse_regular_expression: false, allow_return_outside_function: false, preserve_parens: true, 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 } diff --git a/napi/playground/Cargo.toml b/napi/playground/Cargo.toml index 7cebc8c8076e6..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"] } +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 f51baceec1777..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"], 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"] }