-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): support all regexp plugins
- Loading branch information
Showing
29 changed files
with
390 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"esbuild-regexp-lookbehind-assertions": { | ||
"chrome": "62", | ||
"deno": "1.0", | ||
"edge": "79", | ||
"firefox": "78", | ||
"hermes": "0.7", | ||
"ios": "16.4", | ||
"node": "8.10", | ||
"opera": "49", | ||
"safari": "16.4" | ||
}, | ||
"esbuild-regexp-match-indices": { | ||
"chrome": "90", | ||
"deno": "1.8", | ||
"edge": "90", | ||
"firefox": "88", | ||
"ios": "15.0", | ||
"node": "16.0", | ||
"opera": "76", | ||
"safari": "15.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
//! All RegExp plugins are supported in this module. | ||
//! | ||
//! ## References: | ||
//! * xxx | ||
//! * yyy | ||
mod options; | ||
|
||
pub use options::RegExpOptions; | ||
use oxc_ast::ast::*; | ||
use oxc_semantic::ReferenceFlags; | ||
use oxc_traverse::Traverse; | ||
|
||
use crate::context::Ctx; | ||
|
||
pub struct RegExp<'a> { | ||
_ctx: Ctx<'a>, | ||
options: RegExpOptions, | ||
} | ||
|
||
impl<'a> RegExp<'a> { | ||
pub fn new(options: RegExpOptions, ctx: Ctx<'a>) -> Self { | ||
Self { _ctx: ctx, options } | ||
} | ||
} | ||
|
||
impl<'a> Traverse<'a> for RegExp<'a> { | ||
fn enter_expression( | ||
&mut self, | ||
expr: &mut Expression<'a>, | ||
ctx: &mut oxc_traverse::TraverseCtx<'a>, | ||
) { | ||
let Expression::RegExpLiteral(regexp) = expr else { | ||
return; | ||
}; | ||
|
||
if !self.is_unsupported_regular_expression(regexp) { | ||
return; | ||
} | ||
|
||
let callee = { | ||
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "RegExp"); | ||
let ident = ctx.create_reference_id( | ||
regexp.span, | ||
"RegExp".into(), | ||
symbol_id, | ||
ReferenceFlags::read(), | ||
); | ||
ctx.ast.expression_from_identifier_reference(ident) | ||
}; | ||
|
||
let mut arguments = ctx.ast.vec_with_capacity(2); | ||
// Escape backslashes and quotes in the pattern | ||
let pattern = regexp.regex.pattern.as_str().replace('\\', "\\\\").replace('\"', "\\\""); | ||
arguments.push( | ||
ctx.ast.argument_expression(ctx.ast.expression_string_literal(regexp.span, pattern)), | ||
); | ||
|
||
let flags = regexp.regex.flags.to_string(); | ||
let flags = | ||
ctx.ast.argument_expression(ctx.ast.expression_string_literal(regexp.span, flags)); | ||
arguments.push(flags); | ||
|
||
*expr = ctx.ast.expression_new( | ||
regexp.span, | ||
callee, | ||
arguments, | ||
None::<TSTypeParameterInstantiation>, | ||
); | ||
} | ||
} | ||
|
||
impl<'a> RegExp<'a> { | ||
/// Check if the regular expression contains any unsupported features. | ||
/// | ||
/// Port from [esbuild](https://github.com/evanw/esbuild/blob/332727499e62315cff4ecaff9fa8b86336555e46/internal/js_parser/js_parser.go#L12667-L12800) | ||
fn is_unsupported_regular_expression(&mut self, literal: &RegExpLiteral<'a>) -> bool { | ||
let regex = &literal.regex; | ||
let pattern = ®ex.pattern; | ||
let flags = ®ex.flags; | ||
|
||
let has_unsupported_feature = flags.iter().any(|f| match f { | ||
RegExpFlags::G | RegExpFlags::I | RegExpFlags::M => true, | ||
RegExpFlags::S if self.options.dot_all_flag => true, | ||
RegExpFlags::Y if self.options.sticky_flag => true, | ||
RegExpFlags::U if self.options.unicode_flag => true, | ||
RegExpFlags::D if self.options.match_indices => true, | ||
RegExpFlags::V if self.options.set_notation => true, | ||
_ => false, | ||
}); | ||
|
||
if has_unsupported_feature { | ||
return true; | ||
} | ||
|
||
let mut paren_depth = 0; | ||
let mut i = 0; | ||
let is_unicode = flags.contains(RegExpFlags::U); | ||
|
||
while i < pattern.len() { | ||
let c = pattern.chars().nth(i).unwrap(); | ||
i += 1; | ||
|
||
match c { | ||
'[' => { | ||
while i < pattern.len() { | ||
let c = pattern.chars().nth(i).unwrap(); | ||
i += 1; | ||
match c { | ||
']' => break, | ||
'\\' => i += 1, | ||
_ => {} | ||
} | ||
} | ||
} | ||
'(' => { | ||
let tail = &pattern[i..]; | ||
if tail.starts_with("?<=") || tail.starts_with("?<!") { | ||
if self.options.look_behind_assertions { | ||
return true; | ||
} | ||
} else if self.options.named_capture_groups | ||
&& tail.starts_with("?<") | ||
&& tail[2..].find('>').is_some() | ||
{ | ||
return true; | ||
} | ||
paren_depth += 1; | ||
} | ||
')' => { | ||
if paren_depth == 0 { | ||
return true; | ||
} | ||
paren_depth -= 1; | ||
} | ||
'\\' => { | ||
let tail = &pattern[i + 1..]; | ||
if self.options.unicode_property_escapes | ||
&& is_unicode | ||
&& (tail.starts_with("p{") || tail.starts_with("P{")) | ||
&& tail[i + 3..].find('}').is_some() | ||
{ | ||
return true; | ||
} | ||
i += 1; | ||
} | ||
_ => continue, | ||
} | ||
} | ||
|
||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::env::{can_enable_plugin, Versions}; | ||
|
||
#[derive(Default, Debug, Clone, Copy)] | ||
pub struct RegExpOptions { | ||
pub sticky_flag: bool, | ||
pub unicode_flag: bool, | ||
pub dot_all_flag: bool, | ||
pub look_behind_assertions: bool, | ||
pub named_capture_groups: bool, | ||
pub unicode_property_escapes: bool, | ||
pub match_indices: bool, | ||
pub set_notation: bool, | ||
} | ||
|
||
impl RegExpOptions { | ||
pub fn with_sticky_flag(&mut self, value: bool) -> &Self { | ||
self.sticky_flag = value; | ||
self | ||
} | ||
pub fn with_unicode_flag(&mut self, value: bool) -> &Self { | ||
self.unicode_flag = value; | ||
self | ||
} | ||
|
||
pub fn with_dot_all(&mut self, value: bool) -> &Self { | ||
self.dot_all_flag = value; | ||
self | ||
} | ||
|
||
pub fn with_unicode_property_regex(&mut self, value: bool) -> &Self { | ||
self.look_behind_assertions = value; | ||
self | ||
} | ||
|
||
pub fn with_named_capture_groups(&mut self, value: bool) -> &Self { | ||
self.named_capture_groups = value; | ||
self | ||
} | ||
|
||
pub fn with_unicode_property_escapes(&mut self, value: bool) -> &Self { | ||
self.unicode_property_escapes = value; | ||
self | ||
} | ||
|
||
pub fn with_regexp_match_indices(&mut self, value: bool) -> &Self { | ||
self.match_indices = value; | ||
self | ||
} | ||
|
||
pub fn with_regexp_set_notation(&mut self, value: bool) -> &Self { | ||
self.set_notation = value; | ||
self | ||
} | ||
|
||
#[must_use] | ||
pub fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self { | ||
Self { | ||
sticky_flag: can_enable_plugin("transform-sticky-regex", targets, bugfixes), | ||
unicode_flag: can_enable_plugin("transform-unicode-regex", targets, bugfixes), | ||
dot_all_flag: can_enable_plugin("transform-dotall-regex", targets, bugfixes), | ||
look_behind_assertions: can_enable_plugin( | ||
"esbuild-regexp-lookbehind-assertions", | ||
targets, | ||
bugfixes, | ||
), | ||
named_capture_groups: can_enable_plugin( | ||
"transform-named-capturing-groups-regex", | ||
targets, | ||
bugfixes, | ||
), | ||
unicode_property_escapes: can_enable_plugin( | ||
"transform-unicode-property-regex", | ||
targets, | ||
bugfixes, | ||
), | ||
match_indices: can_enable_plugin("esbuild-regexp-match-indices", targets, bugfixes), | ||
set_notation: can_enable_plugin("transform-unicode-sets-regex", targets, bugfixes), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.