Skip to content

Commit 80c6889

Browse files
Add support for the duplicate named capturing groups proposal (#14805)
1 parent eec9574 commit 80c6889

52 files changed

Lines changed: 286 additions & 23 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Makefile‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,16 @@ test-test262-update-allowlist:
185185

186186

187187
new-version-checklist:
188-
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
189-
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
190-
# @echo "!!!!!! !!!!!!"
191-
# @echo "!!!!!! Write any message that should !!!!!!"
192-
# @echo "!!!!!! block the release here !!!!!!"
193-
# @echo "!!!!!! !!!!!!"
194-
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
195-
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
196-
# @exit 1
188+
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
189+
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
190+
@echo "!!!!!! !!!!!!"
191+
@echo "!!!!!! Update minVersion in the wrapRegExp helper, !!!!!!"
192+
@echo "!!!!!! and the requried core version in the duplicate !!!!!!"
193+
@echo "!!!!!! named groups plugin. !!!!!!"
194+
@echo "!!!!!! !!!!!!"
195+
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
196+
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
197+
@exit 1
197198

198199
new-version:
199200
$(MAKE) new-version-checklist

‎packages/babel-helper-create-regexp-features-plugin/src/features.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const FEATURES = Object.freeze({
55
namedCaptureGroups: 1 << 3,
66
unicodeSetsFlag_syntax: 1 << 4,
77
unicodeSetsFlag: 1 << 5,
8+
duplicateNamedCaptureGroups: 1 << 6,
89
});
910

1011
// We can't use a symbol because this needs to always be the same, even if

‎packages/babel-helper-create-regexp-features-plugin/src/index.ts‎

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import rewritePattern from "regexpu-core";
2-
import { featuresKey, FEATURES, enableFeature, runtimeKey } from "./features";
2+
import {
3+
featuresKey,
4+
FEATURES,
5+
enableFeature,
6+
runtimeKey,
7+
hasFeature,
8+
} from "./features";
39
import { generateRegexpuOptions, canSkipRegexpu, transformFlags } from "./util";
410
import type { NodePath } from "@babel/traverse";
511

@@ -44,16 +50,37 @@ export function createRegExpFeaturePlugin({
4450
const features = file.get(featuresKey) ?? 0;
4551
let newFeatures = enableFeature(features, FEATURES[feature]);
4652

47-
const { useUnicodeFlag, runtime = true } = options;
53+
const { useUnicodeFlag, runtime } = options;
4854
if (useUnicodeFlag === false) {
4955
newFeatures = enableFeature(newFeatures, FEATURES.unicodeFlag);
5056
}
5157
if (newFeatures !== features) {
5258
file.set(featuresKey, newFeatures);
5359
}
5460

55-
if (!runtime) {
56-
file.set(runtimeKey, false);
61+
if (runtime !== undefined) {
62+
if (
63+
file.has(runtimeKey) &&
64+
file.get(runtimeKey) !== runtime &&
65+
// TODO(Babel 8): Remove this check. It's necessary because in Babel 7
66+
// we allow multiple copies of transform-named-capturing-groups-regex
67+
// with conflicting 'runtime' options.
68+
hasFeature(newFeatures, FEATURES.duplicateNamedCaptureGroups)
69+
) {
70+
throw new Error(
71+
`The 'runtime' option must be the same for ` +
72+
`'@babel/plugin-transform-named-capturing-groups-regex' and ` +
73+
`'@babel/plugin-proposal-duplicate-named-capturing-groups-regex'.`,
74+
);
75+
}
76+
// TODO(Babel 8): Remove this check and always set it.
77+
// It's necessary because in Babel 7 we allow multiple copies of
78+
// transform-named-capturing-groups-regex with conflicting 'runtime' options.
79+
if (feature === "namedCaptureGroups") {
80+
if (!runtime || !file.has(runtimeKey)) file.set(runtimeKey, runtime);
81+
} else {
82+
file.set(runtimeKey, runtime);
83+
}
5784
}
5885

5986
if (!file.has(versionKey) || file.get(versionKey) < version) {
@@ -68,13 +95,24 @@ export function createRegExpFeaturePlugin({
6895
const features = file.get(featuresKey);
6996
const runtime = file.get(runtimeKey) ?? true;
7097

71-
const regexpuOptions = generateRegexpuOptions(features);
72-
if (canSkipRegexpu(node, regexpuOptions)) return;
98+
const regexpuOptions = generateRegexpuOptions(node.pattern, features);
99+
if (canSkipRegexpu(node, regexpuOptions)) {
100+
return;
101+
}
73102

74-
const namedCaptureGroups: Record<string, number> = {};
103+
const namedCaptureGroups: Record<string, number | number[]> = {
104+
__proto__: null,
105+
};
75106
if (regexpuOptions.namedGroups === "transform") {
76107
regexpuOptions.onNamedGroup = (name, index) => {
77-
namedCaptureGroups[name] = index;
108+
const prev = namedCaptureGroups[name];
109+
if (typeof prev === "number") {
110+
namedCaptureGroups[name] = [prev, index];
111+
} else if (Array.isArray(prev)) {
112+
prev.push(index);
113+
} else {
114+
namedCaptureGroups[name] = index;
115+
}
78116
};
79117
}
80118

‎packages/babel-helper-create-regexp-features-plugin/src/util.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { FEATURES, hasFeature } from "./features";
33

44
import type { RegexpuOptions } from "regexpu-core";
55

6-
export function generateRegexpuOptions(toTransform: number): RegexpuOptions {
6+
export function generateRegexpuOptions(
7+
pattern: string,
8+
toTransform: number,
9+
): RegexpuOptions {
710
type Experimental = 1;
811

912
const feat = <Stability extends 0 | 1 = 0>(
@@ -13,14 +16,29 @@ export function generateRegexpuOptions(toTransform: number): RegexpuOptions {
1316
return hasFeature(toTransform, FEATURES[name]) ? ok : false;
1417
};
1518

19+
const featDuplicateNamedGroups = (): "transform" | false => {
20+
if (!feat("duplicateNamedCaptureGroups")) return false;
21+
22+
// This can return false positive, for example for /\(?<a>\)/.
23+
// However, it's such a rare occurrence that it's ok to compile
24+
// the regexp even if we only need to compile regexps with
25+
// duplicate named capturing groups.
26+
const regex = /\(\?<([^>]+)>/g;
27+
const seen = new Set();
28+
for (let match; (match = regex.exec(pattern)); seen.add(match[1])) {
29+
if (seen.has(match[1])) return "transform";
30+
}
31+
return false;
32+
};
33+
1634
return {
1735
unicodeFlag: feat("unicodeFlag"),
1836
unicodeSetsFlag:
1937
feat<Experimental>("unicodeSetsFlag") ||
2038
feat<Experimental>("unicodeSetsFlag_syntax", "parse"),
2139
dotAllFlag: feat("dotAllFlag"),
2240
unicodePropertyEscapes: feat("unicodePropertyEscape"),
23-
namedGroups: feat("namedCaptureGroups"),
41+
namedGroups: feat("namedCaptureGroups") || featDuplicateNamedGroups(),
2442
onNamedGroup: () => {},
2543
};
2644
}

packages/babel-helper-create-regexp-features-plugin/test/fixtures/plugin-transform-named-capturing-groups-regex/runtime-false-not-overwritten/input.mjs renamed to packages/babel-helper-create-regexp-features-plugin/test/fixtures/runtime/conflict-false-true/input.mjs

File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"plugins": [
3+
["transform-named-capturing-groups-regex", { "runtime": true }],
4+
["proposal-duplicate-named-capturing-groups-regex", { "runtime": false }]
5+
],
6+
"throws": "The 'runtime' option must be the same for '@babel/plugin-transform-named-capturing-groups-regex' and '@babel/plugin-proposal-duplicate-named-capturing-groups-regex'."
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/(?<year>\d{4})/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"plugins": [
3+
["transform-named-capturing-groups-regex", { "runtime": false }],
4+
["proposal-duplicate-named-capturing-groups-regex", { "runtime": true }]
5+
],
6+
"throws": "The 'runtime' option must be the same for '@babel/plugin-transform-named-capturing-groups-regex' and '@babel/plugin-proposal-duplicate-named-capturing-groups-regex'."
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/(?<year>\d{4})/

packages/babel-helper-create-regexp-features-plugin/test/fixtures/plugin-transform-named-capturing-groups-regex/runtime-false-not-overwritten/options.json renamed to packages/babel-helper-create-regexp-features-plugin/test/fixtures/runtime/plugin-transform-named-capturing-groups-regex-runtime-false-not-overwritten/options.json

File renamed without changes.

0 commit comments

Comments
 (0)