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
4 changes: 3 additions & 1 deletion Cargo.lock

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

8 changes: 4 additions & 4 deletions crates/swc_ecma_compat_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ version = "24.0.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
swc_common = { version = "17.0.1", path = "../swc_common" }
swc_ecma_ast = { version = "18.0.0", path = "../swc_ecma_ast" }
swc_ecma_utils = { version = "24.0.0", path = "../swc_ecma_utils" }
swc_ecma_visit = { version = "18.0.1", path = "../swc_ecma_visit" }
swc_common = { version = "17.0.1", path = "../swc_common" }
swc_ecma_ast = { version = "18.0.0", path = "../swc_ecma_ast" }
swc_ecma_transformer = { version = "0.1.0", path = "../swc_ecma_transformer" }
swc_ecma_utils = { version = "24.0.0", path = "../swc_ecma_utils" }
63 changes: 14 additions & 49 deletions crates/swc_ecma_compat_common/src/regexp.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use swc_common::util::take::Take;
use swc_ecma_ast::{CallExpr, Expr, Lit, Pass, Regex};
use swc_ecma_utils::{quote_ident, ExprFactory};
use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith};
use swc_ecma_ast::Pass;

pub fn regexp(config: Config) -> impl Pass {
visit_mut_pass(RegExp { config })
let mut options = swc_ecma_transformer::Options::default();

let t = &mut options.env.regexp;
t.dot_all_regex = config.dot_all_regex;
t.has_indices = config.has_indices;
t.lookbehind_assertion = config.lookbehind_assertion;
t.named_capturing_groups_regex = config.named_capturing_groups_regex;
t.sticky_regex = config.sticky_regex;
t.unicode_property_regex = config.unicode_property_regex;
t.unicode_regex = config.unicode_regex;
t.unicode_sets_regex = config.unicode_sets_regex;

options.into_pass()
}

#[derive(Default, Clone, Copy)]
Expand All @@ -26,47 +35,3 @@ pub struct Config {
// [RegExp.prototype.unicodeSets](https://github.com/tc39/proposal-regexp-v-flag)
pub unicode_sets_regex: bool,
}

struct RegExp {
config: Config,
}

impl VisitMut for RegExp {
noop_visit_mut_type!(fail);

fn visit_mut_expr(&mut self, expr: &mut Expr) {
expr.visit_mut_children_with(self);

if let Expr::Lit(Lit::Regex(regex)) = expr {
if (self.config.dot_all_regex && regex.flags.contains('s'))
|| (self.config.sticky_regex && regex.flags.contains('y'))
|| (self.config.unicode_regex && regex.flags.contains('u'))
|| (self.config.unicode_sets_regex && regex.flags.contains('v'))
|| (self.config.has_indices && regex.flags.contains('d'))
|| (self.config.named_capturing_groups_regex && regex.exp.contains("(?<"))
|| (self.config.lookbehind_assertion && regex.exp.contains("(?<=")
|| regex.exp.contains("(?<!"))
|| (self.config.unicode_property_regex
&& (regex.exp.contains("\\p{") || regex.exp.contains("\\P{")))
{
let Regex { exp, flags, span } = regex.take();

let exp: Expr = exp.into();
let mut args = vec![exp.into()];

if !flags.is_empty() {
let flags: Expr = flags.into();
args.push(flags.into());
}

*expr = CallExpr {
span,
callee: quote_ident!("RegExp").as_callee(),
args,
..Default::default()
}
.into()
}
}
}
}
1 change: 1 addition & 0 deletions crates/swc_ecma_preset_env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ swc_atoms = { version = "9.0.0", path = "../swc_atoms" }
swc_common = { version = "17.0.1", path = "../swc_common" }
swc_ecma_ast = { version = "18.0.0", path = "../swc_ecma_ast" }
swc_ecma_compiler = { version = "8.0.0", path = "../swc_ecma_compiler" }
swc_ecma_transformer = { version = "0.1.0", path = "../swc_ecma_transformer" }
swc_ecma_transforms = { version = "38.0.0", path = "../swc_ecma_transforms", features = [
"compat",
"proposal",
Expand Down
55 changes: 19 additions & 36 deletions crates/swc_ecma_preset_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use swc_ecma_transforms::{
class_fields_use_set::class_fields_use_set,
es2015::{self, generator::generator},
es2016, es2017, es2018, es2019, es2020, es2022, es3,
regexp::{self, regexp},
},
Assumptions,
};
Expand Down Expand Up @@ -51,6 +50,7 @@ where
C: Comments + Clone,
{
let pass = noop_pass();
let mut options = swc_ecma_transformer::Options::default();

macro_rules! add {
($prev:expr, $feature:ident, $pass:expr) => {{
Expand Down Expand Up @@ -114,39 +114,20 @@ where
),
);

let pass = {
let enable_dot_all_regex = !caniuse(Feature::DotAllRegex);
let enable_named_capturing_groups_regex = !caniuse(Feature::NamedCapturingGroupsRegex);
let enable_sticky_regex = !caniuse(Feature::StickyRegex);
let enable_unicode_property_regex = !caniuse(Feature::UnicodePropertyRegex);
let enable_unicode_regex = !caniuse(Feature::UnicodeRegex);
let enable_unicode_sets_regex = !caniuse(Feature::UnicodeSetsRegex);

let enable = enable_dot_all_regex
|| enable_named_capturing_groups_regex
|| enable_sticky_regex
|| enable_unicode_property_regex
|| enable_unicode_regex;

(
pass,
Optional::new(
regexp(regexp::Config {
dot_all_regex: enable_dot_all_regex,
// TODO: add Feature:HasIndicesRegex
has_indices: false,
// TODO: add Feature::LookbehindAssertion
lookbehind_assertion: false,
named_capturing_groups_regex: enable_named_capturing_groups_regex,
sticky_regex: enable_sticky_regex,
unicode_property_regex: enable_unicode_property_regex,
unicode_regex: enable_unicode_regex,
unicode_sets_regex: enable_unicode_sets_regex,
}),
enable,
),
)
};
{
let t = &mut options.env.regexp;

t.dot_all_regex = !caniuse(Feature::DotAllRegex);
t.named_capturing_groups_regex = !caniuse(Feature::NamedCapturingGroupsRegex);
t.sticky_regex = !caniuse(Feature::StickyRegex);
t.unicode_property_regex = !caniuse(Feature::UnicodePropertyRegex);
t.unicode_regex = !caniuse(Feature::UnicodeRegex);
t.unicode_sets_regex = !caniuse(Feature::UnicodeSetsRegex);
// TODO: add Feature:HasIndicesRegex
t.has_indices = false;
// TODO: add Feature::LookbehindAssertion
t.lookbehind_assertion = false;
}

// Proposals

Expand Down Expand Up @@ -344,11 +325,13 @@ where
bugfixes::template_literal_caching()
);

add!(
let pass = add!(
pass,
BugfixSafariIdDestructuringCollisionInFunctionExpression,
bugfixes::safari_id_destructuring_collision_in_function_expression()
)
);

(pass, options.into_pass())
}

pub fn transform_from_env<C>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.regexp.to-string.js";
var a = RegExp("(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})", "u");
var b = RegExp(".", "s");
var c = RegExp(".", "imsuy");
var c = new RegExp(".", "imsuy");
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output shows inconsistent behavior where variable c is transformed to new RegExp(...) while variables a and b on lines 4-5 are transformed to RegExp(...) without the new keyword. Since the transformation code creates a CallExpr without new, this suggests an unintended change in behavior. Please verify that this test output is correct or investigate why the new keyword is being added for this specific case.

Suggested change
var c = new RegExp(".", "imsuy");
var c = RegExp(".", "imsuy");

Copilot uses AI. Check for mistakes.
console.log(a.unicode);
console.log(b.dotAll);
console.log(c.sticky);
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_transformer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ version = "0.1.0"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(swc_ast_unknown)'] }

[dependencies]
tracing = { workspace = true }
swc_common = { version = "17.0.1", path = "../swc_common" }
swc_ecma_ast = { version = "18.0.0", path = "../swc_ecma_ast" }
swc_ecma_hooks = { version = "0.2.0", path = "../swc_ecma_hooks" }
swc_ecma_transforms_base = { version = "30.0.0", path = "../swc_ecma_transforms_base" }
swc_ecma_utils = { version = "24.0.0", path = "../swc_ecma_utils" }
swc_ecma_visit = { version = "18.0.1", path = "../swc_ecma_visit" }
tracing = { workspace = true }
31 changes: 31 additions & 0 deletions crates/swc_ecma_transformer/src/hook_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use swc_ecma_ast::*;
use swc_ecma_hooks::{CompositeHook, VisitMutHook};

use crate::TraverseCtx;
Expand All @@ -6,11 +7,41 @@ pub(crate) struct OptionalHook<H>(pub Option<H>)
where
H: VisitMutHook<TraverseCtx>;

macro_rules! optional_method {
($enter_name:ident, $exit_name:ident, $T:ty) => {
fn $enter_name(&mut self, node: &mut $T, ctx: &mut TraverseCtx) {
if let Some(hook) = &mut self.0 {
hook.$enter_name(node, ctx);
}
}

fn $exit_name(&mut self, node: &mut $T, ctx: &mut TraverseCtx) {
if let Some(hook) = &mut self.0 {
hook.$exit_name(node, ctx);
}
}
};
}

impl<H> VisitMutHook<TraverseCtx> for OptionalHook<H>
where
H: VisitMutHook<TraverseCtx>,
{
// TODO: Implement lots of hooks, or move it to `swc_ecma_hooks`

optional_method!(enter_expr, exit_expr, Expr);

optional_method!(enter_pat, exit_pat, Pat);

optional_method!(enter_stmt, exit_stmt, Stmt);

optional_method!(enter_module_item, exit_module_item, ModuleItem);

optional_method!(enter_module, exit_module, Module);

optional_method!(enter_script, exit_script, Script);

optional_method!(enter_program, exit_program, Program);
}

pub(crate) struct NoopHook;
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_ecma_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ pub fn hook_pass<H: VisitMutHook<TraverseCtx>>(hook: H) -> impl Pass {

visit_mut_pass(VisitMutWithHook { hook, context: ctx })
}

impl Options {
pub fn into_pass(self) -> impl Pass {
let hook = transform_hook(self);

hook_pass(hook)
}
}
39 changes: 38 additions & 1 deletion crates/swc_ecma_transformer/src/regexp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use swc_common::util::take::Take;
use swc_ecma_ast::*;
use swc_ecma_hooks::VisitMutHook;
use swc_ecma_utils::{quote_ident, ExprFactory};

use crate::TraverseCtx;

Expand Down Expand Up @@ -44,4 +47,38 @@ struct RegexpPass {
options: RegExpOptions,
}

impl VisitMutHook<TraverseCtx> for RegexpPass {}
impl VisitMutHook<TraverseCtx> for RegexpPass {
fn exit_expr(&mut self, expr: &mut Expr, _: &mut TraverseCtx) {
if let Expr::Lit(Lit::Regex(regex)) = expr {
if (self.options.dot_all_regex && regex.flags.contains('s'))
|| (self.options.sticky_regex && regex.flags.contains('y'))
|| (self.options.unicode_regex && regex.flags.contains('u'))
|| (self.options.unicode_sets_regex && regex.flags.contains('v'))
|| (self.options.has_indices && regex.flags.contains('d'))
|| (self.options.named_capturing_groups_regex && regex.exp.contains("(?<"))
|| (self.options.lookbehind_assertion && regex.exp.contains("(?<=")
|| regex.exp.contains("(?<!"))
Comment on lines +59 to +60
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logical operator precedence issue causes regex.exp.contains("(?<!") to always trigger transformation regardless of the lookbehind_assertion option. The condition should be:

|| (self.options.lookbehind_assertion && (regex.exp.contains("(?<=") || regex.exp.contains("(?<!")))

This ensures both lookbehind patterns are only transformed when the option is enabled.

Suggested change
|| (self.options.lookbehind_assertion && regex.exp.contains("(?<=")
|| regex.exp.contains("(?<!"))
|| (self.options.lookbehind_assertion && (regex.exp.contains("(?<=") || regex.exp.contains("(?<!")))

Copilot uses AI. Check for mistakes.
Comment on lines +59 to +60
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operator precedence issue: the condition for lookbehind_assertion is incorrectly grouped. Line 60's || regex.exp.contains(\"(?<!\") is not checked against self.options.lookbehind_assertion, so negative lookbehind patterns (?<!) will always be transformed regardless of the option setting. Add parentheses around the entire lookbehind check: || (self.options.lookbehind_assertion && (regex.exp.contains(\"(?<=\") || regex.exp.contains(\"(?<!\")))

Suggested change
|| (self.options.lookbehind_assertion && regex.exp.contains("(?<=")
|| regex.exp.contains("(?<!"))
|| (self.options.lookbehind_assertion && (regex.exp.contains("(?<=") || regex.exp.contains("(?<!")))

Copilot uses AI. Check for mistakes.
|| (self.options.unicode_property_regex
&& (regex.exp.contains("\\p{") || regex.exp.contains("\\P{")))
{
let Regex { exp, flags, span } = regex.take();

let exp: Expr = exp.into();
let mut args = vec![exp.into()];

if !flags.is_empty() {
let flags: Expr = flags.into();
args.push(flags.into());
}

*expr = CallExpr {
span,
callee: quote_ident!("RegExp").as_callee(),
args,
..Default::default()
}
.into()
}
}
}
}
Loading