Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/oxc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions crates/oxc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -45,3 +45,5 @@ pico-args = { workspace = true }
[features]
# Expose Lexer for benchmarks
benchmarking = []
# Parse regex
regex = ["oxc_regular_expression"]
37 changes: 26 additions & 11 deletions crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -342,25 +343,39 @@ 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 },
Some(Atom::from(raw)),
))
}

#[cfg(feature = "regex")]
fn parse_regex_pattern(
&mut self,
pattern_span_offset: u32,
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion napi/playground/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion tasks/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
Loading