Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Nov 26, 2022
1 parent 65c8496 commit 9faf1c5
Show file tree
Hide file tree
Showing 25 changed files with 188 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/regexpu-core.d.ts
Expand Up @@ -6,6 +6,8 @@ declare module "regexpu-core" {
unicodePropertyEscapes?: "transform" | false;
namedGroups?: "transform" | false;
onNamedGroup?: (name: string, index: number) => void;
modifiers?: "transform" | false;
onNewFlags?: (name: string) => void;
};
function rewritePattern(
pattern: string,
Expand Down
@@ -1,6 +1,6 @@
{
"name": "@babel/helper-create-regexp-features-plugin",
"version": "7.19.0",
"version": "7.20.4",
"author": "The Babel Team (https://babel.dev/team)",
"license": "MIT",
"description": "Compile ESNext Regular Expressions to ES5",
Expand All @@ -19,7 +19,7 @@
],
"dependencies": {
"@babel/helper-annotate-as-pure": "workspace:^",
"regexpu-core": "^5.2.1"
"regexpu-core": "https://github.com/liuxingbaoyu/regexpu-core#head=modifiers"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
Expand Down
Expand Up @@ -6,6 +6,7 @@ export const FEATURES = Object.freeze({
unicodeSetsFlag_syntax: 1 << 4,
unicodeSetsFlag: 1 << 5,
duplicateNamedCaptureGroups: 1 << 6,
modifiers: 1 << 7,
});

// We can't use a symbol because this needs to always be the same, even if
Expand Down
Expand Up @@ -116,6 +116,13 @@ export function createRegExpFeaturePlugin({
};
}

let newFlags;
if (regexpuOptions.modifiers === "transform") {
regexpuOptions.onNewFlags = flags => {
newFlags = flags;
};
}

node.pattern = rewritePattern(node.pattern, node.flags, regexpuOptions);

if (
Expand All @@ -133,7 +140,7 @@ export function createRegExpFeaturePlugin({
path.replaceWith(call);
}

node.flags = transformFlags(regexpuOptions, node.flags);
node.flags = transformFlags(regexpuOptions, newFlags ?? node.flags);
},
},
};
Expand Down
Expand Up @@ -40,6 +40,7 @@ export function generateRegexpuOptions(
unicodePropertyEscapes: feat("unicodePropertyEscape"),
namedGroups: feat("namedCaptureGroups") || featDuplicateNamedGroups(),
onNamedGroup: () => {},
modifiers: feat("modifiers"),
};
}

Expand Down Expand Up @@ -71,6 +72,10 @@ export function canSkipRegexpu(
return false;
}

if (options.modifiers === "transform" && /\(\?.{1,10}:/.test(pattern)) {
return false;
}

return true;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/babel-plugin-proposal-regexp-modifiers/.npmignore
@@ -0,0 +1,3 @@
src
test
*.log
19 changes: 19 additions & 0 deletions packages/babel-plugin-proposal-regexp-modifiers/README.md
@@ -0,0 +1,19 @@
# @babel/plugin-proposal-regexp-modifiers

> Compile regular expressions using duplicate named groups to index-based groups.
See our website [@babel/plugin-proposal-regexp-modifiers](https://babeljs.io/docs/en/babel-plugin-proposal-regexp-modifiers) for more information.

## Install

Using npm:

```sh
npm install --save-dev @babel/plugin-proposal-regexp-modifiers
```

or using yarn:

```sh
yarn add @babel/plugin-proposal-regexp-modifiers --dev
```
50 changes: 50 additions & 0 deletions packages/babel-plugin-proposal-regexp-modifiers/package.json
@@ -0,0 +1,50 @@
{
"name": "@babel/plugin-proposal-regexp-modifiers",
"version": "7.19.1",
"description": "TODO",
"homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-regexp-modifiers",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "./lib/index.js",
"keywords": [
"babel-plugin",
"regex",
"regexp",
"regular expressions"
],
"repository": {
"type": "git",
"url": "https://github.com/babel/babel.git",
"directory": "packages/babel-plugin-proposal-regexp-modifiers"
},
"bugs": "https://github.com/babel/babel/issues",
"dependencies": {
"@babel/helper-create-regexp-features-plugin": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
},
"devDependencies": {
"@babel/core": "workspace:^"
},
"engines": {
"node": ">=6.9.0"
},
"author": "The Babel Team (https://babel.dev/team)",
"conditions": {
"USE_ESM": [
{
"type": "module"
},
null
]
},
"exports": {
".": "./lib/index.js",
"./package.json": "./package.json"
},
"type": "commonjs"
}
12 changes: 12 additions & 0 deletions packages/babel-plugin-proposal-regexp-modifiers/src/index.ts
@@ -0,0 +1,12 @@
/* eslint-disable @babel/development/plugin-name */
import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";
import { declare } from "@babel/helper-plugin-utils";

export default declare(api => {
api.assertVersion("^7.19.0");

return createRegExpFeaturePlugin({
name: "proposal-regexp-modifiers",
feature: "modifiers",
});
});
@@ -0,0 +1,11 @@
let regex = /(?ims:^[a-z].1$)/

expect(regex.test("Ax1")).toBeTruthy();
expect(regex.test("ax1")).toBeTruthy();
expect(regex.test("ax2")).toBeFalsy();
expect(regex.test("1ax1")).toBeFalsy();

expect(regex.test("a\r1")).toBeTruthy();
expect(regex.test("a\r\n1")).toBeFalsy();

expect(regex.test("\nax1\n\n\n\n\n\n\n\n\n")).toBeTruthy();
@@ -0,0 +1,2 @@
/(?ims:^[a-z]1$)/;
/(?-ims:^[a-z].1$)(^[a-z].)/ims;
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-regexp-modifiers"]
}
@@ -0,0 +1,2 @@
/((?:^|(?<=[\n\r\u2028\u2029]))[A-Za-z]1(?:$|(?=[\n\r\u2028\u2029])))/;
/(^[a-z].1$)((?:^|(?<=[\n\r\u2028\u2029]))[A-Za-z][\s\S])/;
@@ -0,0 +1,3 @@
let regex = /(?ims:^[a-z])/u;

expect(regex.test("\u017F")).toBeTruthy();
@@ -0,0 +1,2 @@
/(?ims:^[a-z])/u;
/(?-ims:^[a-z].)(^[a-z].)/uims;
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-regexp-modifiers", "transform-unicode-regex"]
}

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

3 changes: 3 additions & 0 deletions packages/babel-plugin-proposal-regexp-modifiers/test/index.js
@@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";

runner(import.meta.url);
@@ -0,0 +1 @@
{ "type": "module" }
1 change: 1 addition & 0 deletions packages/babel-standalone/package.json
Expand Up @@ -35,6 +35,7 @@
"@babel/plugin-proposal-private-methods": "workspace:^",
"@babel/plugin-proposal-private-property-in-object": "workspace:^",
"@babel/plugin-proposal-record-and-tuple": "workspace:^",
"@babel/plugin-proposal-regexp-modifiers": "workspace:^",
"@babel/plugin-proposal-throw-expressions": "workspace:^",
"@babel/plugin-proposal-unicode-property-regex": "workspace:^",
"@babel/plugin-proposal-unicode-sets-regex": "workspace:^",
Expand Down
1 change: 1 addition & 0 deletions packages/babel-standalone/scripts/pluginConfig.json
Expand Up @@ -59,6 +59,7 @@
"proposal-private-methods",
"proposal-private-property-in-object",
"proposal-record-and-tuple",
"proposal-regexp-modifiers",
"proposal-throw-expressions",
"proposal-unicode-property-regex",
"proposal-unicode-sets-regex",
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-standalone/src/generated/plugins.ts
Expand Up @@ -43,6 +43,7 @@ import proposalPipelineOperator from "@babel/plugin-proposal-pipeline-operator";
import proposalPrivateMethods from "@babel/plugin-proposal-private-methods";
import proposalPrivatePropertyInObject from "@babel/plugin-proposal-private-property-in-object";
import proposalRecordAndTuple from "@babel/plugin-proposal-record-and-tuple";
import proposalRegexpModifiers from "@babel/plugin-proposal-regexp-modifiers";
import proposalThrowExpressions from "@babel/plugin-proposal-throw-expressions";
import proposalUnicodePropertyRegex from "@babel/plugin-proposal-unicode-property-regex";
import proposalUnicodeSetsRegex from "@babel/plugin-proposal-unicode-sets-regex";
Expand Down Expand Up @@ -145,6 +146,7 @@ export {
proposalPrivateMethods,
proposalPrivatePropertyInObject,
proposalRecordAndTuple,
proposalRegexpModifiers,
proposalThrowExpressions,
proposalUnicodePropertyRegex,
proposalUnicodeSetsRegex,
Expand Down Expand Up @@ -249,6 +251,7 @@ export const all: { [k: string]: any } = {
"proposal-private-methods": proposalPrivateMethods,
"proposal-private-property-in-object": proposalPrivatePropertyInObject,
"proposal-record-and-tuple": proposalRecordAndTuple,
"proposal-regexp-modifiers": proposalRegexpModifiers,
"proposal-throw-expressions": proposalThrowExpressions,
"proposal-unicode-property-regex": proposalUnicodePropertyRegex,
"proposal-unicode-sets-regex": proposalUnicodeSetsRegex,
Expand Down
1 change: 1 addition & 0 deletions packages/babel-standalone/src/preset-stage-3.ts
Expand Up @@ -6,6 +6,7 @@ export default (_: any, { loose = false } = {}) => {
babelPlugins.syntaxImportAssertions,
babelPlugins.proposalUnicodeSetsRegex,
babelPlugins.proposalDuplicateNamedCapturingGroupsRegex,
babelPlugins.proposalRegexpModifiers,
];

if (!process.env.BABEL_8_BREAKING) {
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Expand Up @@ -68,6 +68,7 @@
"./packages/babel-plugin-proposal-private-methods/src/**/*.ts",
"./packages/babel-plugin-proposal-private-property-in-object/src/**/*.ts",
"./packages/babel-plugin-proposal-record-and-tuple/src/**/*.ts",
"./packages/babel-plugin-proposal-regexp-modifiers/src/**/*.ts",
"./packages/babel-plugin-proposal-throw-expressions/src/**/*.ts",
"./packages/babel-plugin-proposal-unicode-property-regex/src/**/*.ts",
"./packages/babel-plugin-proposal-unicode-sets-regex/src/**/*.ts",
Expand Down Expand Up @@ -370,6 +371,9 @@
"@babel/plugin-proposal-record-and-tuple": [
"./packages/babel-plugin-proposal-record-and-tuple/src"
],
"@babel/plugin-proposal-regexp-modifiers": [
"./packages/babel-plugin-proposal-regexp-modifiers/src"
],
"@babel/plugin-proposal-throw-expressions": [
"./packages/babel-plugin-proposal-throw-expressions/src"
],
Expand Down
54 changes: 44 additions & 10 deletions yarn.lock
Expand Up @@ -614,7 +614,7 @@ __metadata:
"@babel/core": "workspace:^"
"@babel/helper-annotate-as-pure": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
regexpu-core: ^5.2.1
regexpu-core: "https://github.com/liuxingbaoyu/regexpu-core#head=modifiers"
peerDependencies:
"@babel/core": ^7.0.0
languageName: unknown
Expand Down Expand Up @@ -1749,6 +1749,18 @@ __metadata:
languageName: unknown
linkType: soft

"@babel/plugin-proposal-regexp-modifiers@workspace:^, @babel/plugin-proposal-regexp-modifiers@workspace:packages/babel-plugin-proposal-regexp-modifiers":
version: 0.0.0-use.local
resolution: "@babel/plugin-proposal-regexp-modifiers@workspace:packages/babel-plugin-proposal-regexp-modifiers"
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-create-regexp-features-plugin": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"
peerDependencies:
"@babel/core": ^7.0.0
languageName: unknown
linkType: soft

"@babel/plugin-proposal-throw-expressions@workspace:^, @babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions":
version: 0.0.0-use.local
resolution: "@babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions"
Expand Down Expand Up @@ -3660,6 +3672,7 @@ __metadata:
"@babel/plugin-proposal-private-methods": "workspace:^"
"@babel/plugin-proposal-private-property-in-object": "workspace:^"
"@babel/plugin-proposal-record-and-tuple": "workspace:^"
"@babel/plugin-proposal-regexp-modifiers": "workspace:^"
"@babel/plugin-proposal-throw-expressions": "workspace:^"
"@babel/plugin-proposal-unicode-property-regex": "workspace:^"
"@babel/plugin-proposal-unicode-sets-regex": "workspace:^"
Expand Down Expand Up @@ -12974,17 +12987,38 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"regexpu-core@npm:^5.1.0, regexpu-core@npm:^5.2.1":
version: 5.2.1
resolution: "regexpu-core@npm:5.2.1"
"regexpu-core@https://github.com/liuxingbaoyu/regexpu-core#head=modifiers":
version: 5.2.2
resolution: "regexpu-core@https://github.com/liuxingbaoyu/regexpu-core.git#commit=332ffe3042ed3e508355a959b9331340c2e92750"
dependencies:
regenerate: ^1.4.2
regenerate-unicode-properties: ^10.1.0
regjsgen: "https://github.com/liuxingbaoyu/regjsgen"
regjsparser: ^0.9.1
unicode-match-property-ecmascript: ^2.0.0
unicode-match-property-value-ecmascript: ^2.1.0
checksum: 6f6e2f2ec41251a7078f2d8836f5be78f1f756f24cdb1361f7704c56d7a88e95c34e39b25b32158a6365d05989cee991dd830de290d44fb6cfa676d89b59b185
languageName: node
linkType: hard

"regexpu-core@npm:^5.1.0":
version: 5.2.2
resolution: "regexpu-core@npm:5.2.2"
dependencies:
regenerate: ^1.4.2
regenerate-unicode-properties: ^10.1.0
regjsgen: ^0.7.1
regjsparser: ^0.9.1
unicode-match-property-ecmascript: ^2.0.0
unicode-match-property-value-ecmascript: ^2.0.0
checksum: c1244db79f7a4597414cd7fdf5171fa73905f0cbc684385c78127fc6198f9cade8fe829a1c4036c8ec57ac75b1ffb8c196451abdd2e153f26a4d8043fa10bbb3
unicode-match-property-value-ecmascript: ^2.1.0
checksum: 87c56815e20d213848d38f6b047ba52f0d632f36e791b777f59327e8d350c0743b27cc25feab64c0eadc9fe9959dde6b1261af71108a9371b72c8c26beda05ef
languageName: node
linkType: hard

"regjsgen@https://github.com/liuxingbaoyu/regjsgen":
version: 0.7.1
resolution: "regjsgen@https://github.com/liuxingbaoyu/regjsgen.git#commit=7ad3e87547248239b37552116cdbdf91e39795fe"
checksum: 7cac399921c58db8e16454869283ff66871531180218064fa938ac05c11c2976792a00706c3c78bbc625e1d793ca373065ea90564e06189a751a7b4ae33acadc
languageName: node
linkType: hard

Expand Down Expand Up @@ -14848,10 +14882,10 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"unicode-match-property-value-ecmascript@npm:^2.0.0":
version: 2.0.0
resolution: "unicode-match-property-value-ecmascript@npm:2.0.0"
checksum: 8fe6a09d9085a625cabcead5d95bdbc1a2d5d481712856092ce0347231e81a60b93a68f1b69e82b3076a07e415a72c708044efa2aa40ae23e2e7b5c99ed4a9ea
"unicode-match-property-value-ecmascript@npm:^2.1.0":
version: 2.1.0
resolution: "unicode-match-property-value-ecmascript@npm:2.1.0"
checksum: 8d6f5f586b9ce1ed0e84a37df6b42fdba1317a05b5df0c249962bd5da89528771e2d149837cad11aa26bcb84c35355cb9f58a10c3d41fa3b899181ece6c85220
languageName: node
linkType: hard

Expand Down

0 comments on commit 9faf1c5

Please sign in to comment.