Skip to content

Commit 79e1452

Browse files
feat: Support regexp modifiers proposal (#15226)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
1 parent c292e68 commit 79e1452

26 files changed

Lines changed: 182 additions & 25 deletions

File tree

lib/regexpu-core.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ declare module "regexpu-core" {
66
unicodePropertyEscapes?: "transform" | false;
77
namedGroups?: "transform" | false;
88
onNamedGroup?: (name: string, index: number) => void;
9+
modifiers?: "transform" | false;
10+
onNewFlags?: (name: string) => void;
911
};
1012
function rewritePattern(
1113
pattern: string,

packages/babel-helper-create-regexp-features-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"dependencies": {
2121
"@babel/helper-annotate-as-pure": "workspace:^",
22-
"regexpu-core": "^5.2.1"
22+
"regexpu-core": "^5.3.1"
2323
},
2424
"peerDependencies": {
2525
"@babel/core": "^7.0.0"

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

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

1112
// 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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ export function createRegExpFeaturePlugin({
116116
};
117117
}
118118

119+
let newFlags;
120+
if (regexpuOptions.modifiers === "transform") {
121+
regexpuOptions.onNewFlags = flags => {
122+
newFlags = flags;
123+
};
124+
}
125+
119126
node.pattern = rewritePattern(node.pattern, node.flags, regexpuOptions);
120127

121128
if (
@@ -133,7 +140,7 @@ export function createRegExpFeaturePlugin({
133140
path.replaceWith(call);
134141
}
135142

136-
node.flags = transformFlags(regexpuOptions, node.flags);
143+
node.flags = transformFlags(regexpuOptions, newFlags ?? node.flags);
137144
},
138145
},
139146
};

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function generateRegexpuOptions(
4040
unicodePropertyEscapes: feat("unicodePropertyEscape"),
4141
namedGroups: feat("namedCaptureGroups") || featDuplicateNamedGroups(),
4242
onNamedGroup: () => {},
43+
modifiers: feat("modifiers"),
4344
};
4445
}
4546

@@ -71,6 +72,10 @@ export function canSkipRegexpu(
7172
return false;
7273
}
7374

75+
if (options.modifiers === "transform" && /\(\?[\w-]+:/.test(pattern)) {
76+
return false;
77+
}
78+
7479
return true;
7580
}
7681

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src
2+
test
3+
*.log
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# @babel/plugin-proposal-regexp-modifiers
2+
3+
> Compile regular expressions using duplicate named groups to index-based groups.
4+
5+
See our website [@babel/plugin-proposal-regexp-modifiers](https://babeljs.io/docs/en/babel-plugin-proposal-regexp-modifiers) for more information.
6+
7+
## Install
8+
9+
Using npm:
10+
11+
```sh
12+
npm install --save-dev @babel/plugin-proposal-regexp-modifiers
13+
```
14+
15+
or using yarn:
16+
17+
```sh
18+
yarn add @babel/plugin-proposal-regexp-modifiers --dev
19+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "@babel/plugin-proposal-regexp-modifiers",
3+
"version": "7.19.1",
4+
"description": "Compile inline regular expression modifiers",
5+
"homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-regexp-modifiers",
6+
"license": "MIT",
7+
"publishConfig": {
8+
"access": "public"
9+
},
10+
"main": "./lib/index.js",
11+
"keywords": [
12+
"babel-plugin",
13+
"regex",
14+
"regexp",
15+
"regular expressions"
16+
],
17+
"repository": {
18+
"type": "git",
19+
"url": "https://github.com/babel/babel.git",
20+
"directory": "packages/babel-plugin-proposal-regexp-modifiers"
21+
},
22+
"bugs": "https://github.com/babel/babel/issues",
23+
"dependencies": {
24+
"@babel/helper-create-regexp-features-plugin": "workspace:^",
25+
"@babel/helper-plugin-utils": "workspace:^"
26+
},
27+
"peerDependencies": {
28+
"@babel/core": "^7.0.0"
29+
},
30+
"devDependencies": {
31+
"@babel/core": "workspace:^"
32+
},
33+
"engines": {
34+
"node": ">=6.9.0"
35+
},
36+
"author": "The Babel Team (https://babel.dev/team)",
37+
"conditions": {
38+
"USE_ESM": [
39+
{
40+
"type": "module"
41+
},
42+
null
43+
]
44+
},
45+
"exports": {
46+
".": "./lib/index.js",
47+
"./package.json": "./package.json"
48+
},
49+
"type": "commonjs"
50+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable @babel/development/plugin-name */
2+
import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";
3+
import { declare } from "@babel/helper-plugin-utils";
4+
5+
export default declare(api => {
6+
api.assertVersion("^7.19.0");
7+
8+
return createRegExpFeaturePlugin({
9+
name: "proposal-regexp-modifiers",
10+
feature: "modifiers",
11+
});
12+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let regex = /(?ims:^[a-z])/u;
2+
3+
expect(regex.test("\u017F")).toBeTruthy();

0 commit comments

Comments
 (0)