Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transform support for the "regexp unicode sets" proposal (#14125)
- Loading branch information
1 parent
2b36ca7
commit 28ed83f
Showing
35 changed files
with
368 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 58 additions & 46 deletions
104
packages/babel-helper-create-regexp-features-plugin/src/util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,77 @@ | ||
import type { types as t } from "@babel/core"; | ||
import { FEATURES, hasFeature } from "./features"; | ||
|
||
type RegexpuOptions = { | ||
useUnicodeFlag: boolean; | ||
unicodeFlag: "transform" | false; | ||
unicodeSetsFlag: "transform" | "parse" | false; | ||
dotAllFlag: "transform" | false; | ||
unicodePropertyEscapes: "transform" | false; | ||
namedGroups: "transform" | false; | ||
onNamedGroup: (name: string, index: number) => void; | ||
namedGroup: boolean; | ||
unicodePropertyEscape: boolean; | ||
dotAllFlag: boolean; | ||
lookbehind: boolean; | ||
}; | ||
|
||
export function generateRegexpuOptions(node, features): RegexpuOptions | null { | ||
let useUnicodeFlag = false, | ||
dotAllFlag = false, | ||
unicodePropertyEscape = false, | ||
namedGroup = false; | ||
export function generateRegexpuOptions(toTransform: number): RegexpuOptions { | ||
type Experimental = 1; | ||
|
||
const feat = <Stability extends 0 | 1 = 0>( | ||
name: keyof typeof FEATURES, | ||
ok: "transform" | (Stability extends 0 ? never : "parse") = "transform", | ||
) => { | ||
return hasFeature(toTransform, FEATURES[name]) ? ok : false; | ||
}; | ||
|
||
return { | ||
unicodeFlag: feat("unicodeFlag"), | ||
unicodeSetsFlag: | ||
feat<Experimental>("unicodeSetsFlag") || | ||
feat<Experimental>("unicodeSetsFlag_syntax", "parse"), | ||
dotAllFlag: feat("dotAllFlag"), | ||
unicodePropertyEscapes: feat("unicodePropertyEscape"), | ||
namedGroups: feat("namedCaptureGroups"), | ||
onNamedGroup: () => {}, | ||
}; | ||
} | ||
|
||
export function canSkipRegexpu( | ||
node: t.RegExpLiteral, | ||
options: RegexpuOptions, | ||
): boolean { | ||
const { flags, pattern } = node; | ||
const flagsIncludesU = flags.includes("u"); | ||
|
||
if (flagsIncludesU) { | ||
if (!hasFeature(features, FEATURES.unicodeFlag)) { | ||
useUnicodeFlag = true; | ||
} | ||
if (flags.includes("v")) { | ||
if (options.unicodeSetsFlag === "transform") return false; | ||
} | ||
|
||
if (flags.includes("u")) { | ||
if (options.unicodeFlag === "transform") return false; | ||
if ( | ||
hasFeature(features, FEATURES.unicodePropertyEscape) && | ||
options.unicodePropertyEscapes === "transform" && | ||
/\\[pP]{/.test(pattern) | ||
) { | ||
unicodePropertyEscape = true; | ||
return false; | ||
} | ||
} | ||
|
||
if (hasFeature(features, FEATURES.dotAllFlag) && flags.indexOf("s") >= 0) { | ||
dotAllFlag = true; | ||
if (flags.includes("s")) { | ||
if (options.dotAllFlag === "transform") return false; | ||
} | ||
if ( | ||
hasFeature(features, FEATURES.namedCaptureGroups) && | ||
/\(\?<(?![=!])/.test(pattern) | ||
) { | ||
namedGroup = true; | ||
|
||
if (options.namedGroups === "transform" && /\(\?<(?![=!])/.test(pattern)) { | ||
return false; | ||
} | ||
if ( | ||
!namedGroup && | ||
!unicodePropertyEscape && | ||
!dotAllFlag && | ||
(!flagsIncludesU || useUnicodeFlag) | ||
) { | ||
return null; | ||
|
||
return true; | ||
} | ||
|
||
export function transformFlags(regexpuOptions: RegexpuOptions, flags: string) { | ||
if (regexpuOptions.unicodeSetsFlag === "transform") { | ||
flags = flags.replace("v", "u"); | ||
} | ||
// Now we have to feed regexpu-core the regex | ||
if (flagsIncludesU && flags.indexOf("s") >= 0) { | ||
// When flags includes u, `config.unicode` will be enabled even if `u` is supported natively. | ||
// In this case we have to enable dotAllFlag, otherwise `rewritePattern(/./su)` will return | ||
// incorrect result | ||
// https://github.com/mathiasbynens/regexpu-core/blob/v4.6.0/rewrite-pattern.js#L191 | ||
dotAllFlag = true; | ||
if (regexpuOptions.unicodeFlag === "transform") { | ||
flags = flags.replace("u", ""); | ||
} | ||
return { | ||
useUnicodeFlag, | ||
onNamedGroup: () => {}, | ||
namedGroup, | ||
unicodePropertyEscape, | ||
dotAllFlag, | ||
lookbehind: true, | ||
}; | ||
if (regexpuOptions.dotAllFlag === "transform") { | ||
flags = flags.replace("s", ""); | ||
} | ||
return flags; | ||
} |
2 changes: 1 addition & 1 deletion
2
...es/plugin-transform-named-capturing-groups-regex/runtime-false-not-overwritten/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/([0-9]{4})/; | ||
/(\d{4})/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
test | ||
*.log |
19 changes: 19 additions & 0 deletions
19
packages/babel-plugin-proposal-unicode-sets-regex/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# @babel/plugin-proposal-unicode-sets-regex | ||
|
||
> Compile regular expressions' unicodeSets (v) flag. | ||
See our website [@babel/plugin-proposal-unicode-sets-regex](https://babeljs.io/docs/en/babel-plugin-proposal-unicode-sets-regex) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-proposal-unicode-sets-regex | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-proposal-unicode-sets-regex --dev | ||
``` |
48 changes: 48 additions & 0 deletions
48
packages/babel-plugin-proposal-unicode-sets-regex/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "@babel/plugin-proposal-unicode-sets-regex", | ||
"version": "7.16.7", | ||
"description": "Compile regular expressions' unicodeSets (v) flag.", | ||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-unicode-sets-regex", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "./lib/index.js", | ||
"keywords": [ | ||
"babel-plugin", | ||
"regex", | ||
"regexp", | ||
"unicode", | ||
"sets", | ||
"properties", | ||
"property", | ||
"string", | ||
"strings", | ||
"regular expressions" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel.git", | ||
"directory": "packages/babel-plugin-proposal-unicode-sets-regex" | ||
}, | ||
"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:^", | ||
"@babel/helper-plugin-test-runner": "workspace:^" | ||
}, | ||
"author": "The Babel Team (https://babel.dev/team)", | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"engines": { | ||
"node": ">=6.9.0" | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/babel-plugin-proposal-unicode-sets-regex/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* 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); | ||
|
||
return createRegExpFeaturePlugin({ | ||
name: "transform-unicode-sets-regex", | ||
feature: "unicodeSetsFlag", | ||
manipulateOptions(opts, parserOpts) { | ||
parserOpts.plugins.push("regexpUnicodeSets"); | ||
}, | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-proposal-unicode-sets-regex/test/fixtures/basic/basic/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/[[0-7]&&[5-9]]\u{10FFFF}/v; |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-unicode-sets-regex/test/fixtures/basic/basic/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["proposal-unicode-sets-regex"] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-proposal-unicode-sets-regex/test/fixtures/basic/basic/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/[5-7]\u{10FFFF}/u; |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-proposal-unicode-sets-regex/test/fixtures/basic/transform-u/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/[[0-7]&&[5-9]]\u{10FFFF}/v; |
3 changes: 3 additions & 0 deletions
3
...ges/babel-plugin-proposal-unicode-sets-regex/test/fixtures/basic/transform-u/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["proposal-unicode-sets-regex", "transform-unicode-regex"] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-proposal-unicode-sets-regex/test/fixtures/basic/transform-u/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/[5-7](?:\uDBFF\uDFFF)/; |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-unicode-sets-regex/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import runner from "@babel/helper-plugin-test-runner"; | ||
|
||
runner(import.meta.url); |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-proposal-unicode-sets-regex/test/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "type": "module" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
test | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# @babel/plugin-syntax-unicode-sets-regex | ||
|
||
> Parse regular expressions' unicodeSets (v) flag. | ||
See our website [@babel/plugin-syntax-unicode-sets-regex](https://babeljs.io/docs/en/babel-plugin-syntax-unicode-sets-regex) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-syntax-unicode-sets-regex | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-syntax-unicode-sets-regex --dev | ||
``` |
Oops, something went wrong.