New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add transform support for the "regexp unicode sets" proposal #14125
Merged
nicolo-ribaudo
merged 4 commits into
babel:feat-7.17.0/regexp-unicode-sets
from
nicolo-ribaudo:regexp-unicode-sets-transform
Jan 10, 2022
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
103 changes: 57 additions & 46 deletions
103
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,76 @@ | ||
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; | ||
type Stable = 0; | ||
|
||
export function generateRegexpuOptions(toTransform: number): RegexpuOptions { | ||
const feat = <IsStable extends 0 | 1>( | ||
name: keyof typeof FEATURES, | ||
ok: "transform" | (IsStable extends Stable ? never : "parse") = "transform", | ||
) => { | ||
return hasFeature(toTransform, FEATURES[name]) ? ok : false; | ||
}; | ||
|
||
return { | ||
unicodeFlag: feat<Stable>("unicodeFlag"), | ||
unicodeSetsFlag: | ||
feat("unicodeSetsFlag") || feat("unicodeSetsFlag_syntax", "parse"), | ||
dotAllFlag: feat<Stable>("dotAllFlag"), | ||
unicodePropertyEscapes: feat<Stable>("unicodePropertyEscape"), | ||
namedGroups: feat<Stable>("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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Since most flags are stable, we can use
Experimental = 1
in type notations.