Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/oxc_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ doctest = true
oxc_allocator = { workspace = true }
oxc_ast_macros = { workspace = true }
oxc_data_structures = { workspace = true, features = ["inline_string"] }
oxc_diagnostics = { workspace = true }
oxc_estree = { workspace = true }
oxc_regular_expression = { workspace = true }
oxc_span = { workspace = true }
Expand Down
27 changes: 27 additions & 0 deletions crates/oxc_ast/src/ast_impl/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,33 @@ impl Display for BigIntLiteral<'_> {
}
}

impl<'a> RegExpLiteral<'a> {
/// Parse the pattern string.
///
/// # Errors
/// Returns an error if the pattern is invalid.
pub fn parse_pattern(
&self,
allocator: &'a Allocator,
) -> oxc_diagnostics::Result<oxc_regular_expression::ast::Pattern<'a>> {
let pattern_text = self.regex.pattern.text.as_str();
#[expect(clippy::cast_possible_truncation)]
let pattern_len = pattern_text.len() as u32;
let literal_span = self.span;
let pattern_span_offset = literal_span.start + 1; // +1 to skip the opening `/`
let flags_span_offset = pattern_span_offset + pattern_len + 1; // +1 to skip the closing `/`
let flags_text = &self.regex.flags.to_inline_string();

oxc_regular_expression::LiteralParser::new(
allocator,
pattern_text,
Some(flags_text),
oxc_regular_expression::Options { pattern_span_offset, flags_span_offset },
)
.parse()
}
}

impl Display for RegExp<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "/{}/{}", self.pattern.text, self.flags)
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_regular_expression/src/parser/parser_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ use crate::{
/// Regular expression parser for `/literal/` usage.
/// - `pattern_text`: the text between `/` and `/`
/// - `flags_text`: the text after the second `/`
pub struct LiteralParser<'a> {
pub struct LiteralParser<'a, 'b> {
allocator: &'a Allocator,
pattern_text: &'a str,
flags_text: Option<&'a str>,
flags_text: Option<&'b str>,
options: Options,
}

impl<'a> LiteralParser<'a> {
impl<'a, 'b> LiteralParser<'a, 'b> {
pub fn new(
allocator: &'a Allocator,
pattern_text: &'a str,
flags_text: Option<&'a str>,
flags_text: Option<&'b str>,
options: Options,
) -> Self {
Self { allocator, pattern_text, flags_text, options }
Expand Down
30 changes: 1 addition & 29 deletions crates/oxc_transformer/src/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
//! (actually these would be improvements on ESBuild, not Babel)

use oxc_ast::{NONE, ast::*};
use oxc_diagnostics::Result;
use oxc_regular_expression::ast::{
CharacterClass, CharacterClassContents, LookAroundAssertionKind, Pattern, Term,
};
Expand Down Expand Up @@ -145,21 +144,7 @@ impl<'a> RegExp<'a, '_> {
let pattern = if let Some(pattern) = &regexp.regex.pattern.pattern {
pattern
} else {
#[expect(clippy::cast_possible_truncation)]
let pattern_len = pattern_text.len() as u32;
let literal_span = regexp.span;
let pattern_span_start = literal_span.start + 1; // +1 to skip the opening `/`
let flags_span_start = pattern_span_start + pattern_len + 1; // +1 to skip the closing `/`
let flags_text =
Span::new(flags_span_start, literal_span.end).source_text(self.ctx.source_text);
// Try to parse pattern
match try_parse_pattern(
pattern_text.as_str(),
pattern_span_start,
flags_text,
flags_span_start,
ctx,
) {
match regexp.parse_pattern(ctx.ast.allocator) {
Ok(pattern) => {
owned_pattern = Some(pattern);
owned_pattern.as_ref().unwrap()
Expand Down Expand Up @@ -236,16 +221,3 @@ fn character_class_has_unicode_property_escape(character_class: &CharacterClass)
_ => false,
})
}

fn try_parse_pattern<'a>(
raw: &'a str,
pattern_span_offset: u32,
flags_text: &'a str,
flags_span_offset: u32,
ctx: &TraverseCtx<'a>,
) -> Result<Pattern<'a>> {
use oxc_regular_expression::{LiteralParser, Options};

let options = Options { pattern_span_offset, flags_span_offset };
LiteralParser::new(ctx.ast.allocator, raw, Some(flags_text), options).parse()
}
Loading