From 0eea7da1c8dc736093b22307f528123436d4621b Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Sun, 31 Aug 2025 15:15:49 +0000 Subject: [PATCH] fix(regular_expression): don't lower capture groups that are not named ones (#13469) Capture groups that are not named ones are lowered even though it's not needed to. --- crates/oxc_regular_expression/src/ast_impl/support.rs | 4 +++- tasks/coverage/snapshots/semantic_typescript.snap | 4 ++-- .../fixtures/all-regex-plugins-enabled-by-targets/input.js | 6 +++--- .../fixtures/all-regex-plugins-enabled-by-targets/output.js | 6 +++--- .../transform-named-capturing-groups-regex/input.js | 4 +++- .../transform-named-capturing-groups-regex/output.js | 4 +++- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/crates/oxc_regular_expression/src/ast_impl/support.rs b/crates/oxc_regular_expression/src/ast_impl/support.rs index 7900e63008664..12a5daa15204e 100644 --- a/crates/oxc_regular_expression/src/ast_impl/support.rs +++ b/crates/oxc_regular_expression/src/ast_impl/support.rs @@ -22,7 +22,9 @@ fn term_contains_unsupported(mut term: &Term, unsupported: &RegexUnsupportedPatt // Loop because `Term::Quantifier` contains a nested `Term` loop { match term { - Term::CapturingGroup(_) => return unsupported.named_capture_groups, + Term::CapturingGroup(group) => { + return group.name.is_some() && unsupported.named_capture_groups; + } Term::UnicodePropertyEscape(_) => return unsupported.unicode_property_escapes, Term::CharacterClass(character_class) => { return unsupported.unicode_property_escapes diff --git a/tasks/coverage/snapshots/semantic_typescript.snap b/tasks/coverage/snapshots/semantic_typescript.snap index 16352dffe7c61..c6712e5439c0d 100644 --- a/tasks/coverage/snapshots/semantic_typescript.snap +++ b/tasks/coverage/snapshots/semantic_typescript.snap @@ -5918,8 +5918,8 @@ Reference symbol mismatch for "require": after transform: SymbolId(0) "require" rebuilt : Unresolved references mismatch: -after transform: ["Error", "JSON", "RegExp"] -rebuilt : ["Error", "JSON", "RegExp", "require"] +after transform: ["Error", "JSON"] +rebuilt : ["Error", "JSON", "require"] semantic Error: tasks/coverage/typescript/tests/cases/compiler/controlFlowUnionContainingTypeParameter1.ts Unresolved references mismatch: diff --git a/tasks/transform_conformance/tests/regexp/test/fixtures/all-regex-plugins-enabled-by-targets/input.js b/tasks/transform_conformance/tests/regexp/test/fixtures/all-regex-plugins-enabled-by-targets/input.js index 31519d315e94c..40bed4b16c673 100644 --- a/tasks/transform_conformance/tests/regexp/test/fixtures/all-regex-plugins-enabled-by-targets/input.js +++ b/tasks/transform_conformance/tests/regexp/test/fixtures/all-regex-plugins-enabled-by-targets/input.js @@ -9,11 +9,11 @@ a1 = /a.b/s // RegExpLookbehindAssertions b1 = /(?b)/ -c2 = /((?d)){4}/; +c2 = /((?d)){4}/; // FIXME(sapphi-red): will be fixed in the next PR // RegExpUnicodePropertyEscapes d1 = /\p{Emoji}/u // ES2022 diff --git a/tasks/transform_conformance/tests/regexp/test/fixtures/all-regex-plugins-enabled-by-targets/output.js b/tasks/transform_conformance/tests/regexp/test/fixtures/all-regex-plugins-enabled-by-targets/output.js index c430be659e84f..22c965e5bab3c 100644 --- a/tasks/transform_conformance/tests/regexp/test/fixtures/all-regex-plugins-enabled-by-targets/output.js +++ b/tasks/transform_conformance/tests/regexp/test/fixtures/all-regex-plugins-enabled-by-targets/output.js @@ -3,10 +3,10 @@ x2 = new RegExp(".", "u"); a1 = new RegExp("a.b", "s"); b1 = new RegExp("(?b)", ""); -c2 = new RegExp("((?d)){4}", ""); +c2 = /((?d)){4}/; d1 = new RegExp("\\p{Emoji}", "u"); f1 = new RegExp("y", "d"); g1 = new RegExp("[\\p{White_Space}&&\\p{ASCII}]", "v"); diff --git a/tasks/transform_conformance/tests/regexp/test/fixtures/transform-named-capturing-groups-regex/input.js b/tasks/transform_conformance/tests/regexp/test/fixtures/transform-named-capturing-groups-regex/input.js index 9b29e8fd65904..52680bf055f1a 100644 --- a/tasks/transform_conformance/tests/regexp/test/fixtures/transform-named-capturing-groups-regex/input.js +++ b/tasks/transform_conformance/tests/regexp/test/fixtures/transform-named-capturing-groups-regex/input.js @@ -1,2 +1,4 @@ c1 = /(?b)/ -c2 = /((?b)){2}/ +c2 = /((?b)){2}/ // FIXME(sapphi-red): will be fixed in the next PR + +n1 = /(a)/ diff --git a/tasks/transform_conformance/tests/regexp/test/fixtures/transform-named-capturing-groups-regex/output.js b/tasks/transform_conformance/tests/regexp/test/fixtures/transform-named-capturing-groups-regex/output.js index 9ab527d8f46a2..c179c37555ea1 100644 --- a/tasks/transform_conformance/tests/regexp/test/fixtures/transform-named-capturing-groups-regex/output.js +++ b/tasks/transform_conformance/tests/regexp/test/fixtures/transform-named-capturing-groups-regex/output.js @@ -1,2 +1,4 @@ c1 = new RegExp("(?b)", ""); -c2 = new RegExp("((?b)){2}", ""); +c2 = /((?b)){2}/; + +n1 = /(a)/;