Skip to content
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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/babel-core/src/parser/util/missing-plugin-helper.ts
Expand Up @@ -201,6 +201,16 @@ const pluginNameMap = {
url: "https://git.io/JvKp3",
},
},
regexpUnicodeSets: {
syntax: {
name: "@babel/plugin-syntax-unicode-sets-regex",
url: "https://git.io/J9GTd",
},
transform: {
name: "@babel/plugin-proposal-unicode-sets-regex",
url: "https://git.io/J9GTQ",
},
},
throwExpressions: {
syntax: {
name: "@babel/plugin-syntax-throw-expressions",
Expand Down
Expand Up @@ -3,6 +3,8 @@ export const FEATURES = Object.freeze({
dotAllFlag: 1 << 1,
unicodePropertyEscape: 1 << 2,
namedCaptureGroups: 1 << 3,
unicodeSetsFlag_syntax: 1 << 4,
unicodeSetsFlag: 1 << 5,
});

// We can't use a symbol because this needs to always be the same, even if
Expand Down
Expand Up @@ -20,9 +20,13 @@ export function createRegExpFeaturePlugin({
name,
feature,
options = {} as any,
manipulateOptions = (() => {}) as (opts: any, parserOpts: any) => void,
}) {
return {
name,

manipulateOptions,

pre() {
const { file } = this;
const features = file.get(featuresKey) ?? 0;
Expand Down
27 changes: 21 additions & 6 deletions packages/babel-helper-create-regexp-features-plugin/src/util.ts
Expand Up @@ -3,22 +3,30 @@ import { FEATURES, hasFeature } from "./features";

type RegexpuOptions = {
unicodeFlag: "transform" | false;
unicodeSetsFlag: "transform" | "parse" | false;
dotAllFlag: "transform" | false;
unicodePropertyEscapes: "transform" | false;
namedGroups: "transform" | false;
onNamedGroup: (name: string, index: number) => void;
};

type Stable = 0;

export function generateRegexpuOptions(toTransform: number): RegexpuOptions {
const feat = (name: keyof typeof FEATURES) => {
return hasFeature(toTransform, FEATURES[name]) ? "transform" : false;
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("unicodeFlag"),
dotAllFlag: feat("dotAllFlag"),
unicodePropertyEscapes: feat("unicodePropertyEscape"),
namedGroups: feat("namedCaptureGroups"),
unicodeFlag: feat<Stable>("unicodeFlag"),
Copy link
Contributor

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.

unicodeSetsFlag:
feat("unicodeSetsFlag") || feat("unicodeSetsFlag_syntax", "parse"),
dotAllFlag: feat<Stable>("dotAllFlag"),
unicodePropertyEscapes: feat<Stable>("unicodePropertyEscape"),
namedGroups: feat<Stable>("namedCaptureGroups"),
onNamedGroup: () => {},
};
}
Expand All @@ -29,6 +37,10 @@ export function canSkipRegexpu(
): boolean {
const { flags, pattern } = node;

if (flags.includes("v")) {
if (options.unicodeSetsFlag === "transform") return false;
}

if (flags.includes("u")) {
if (options.unicodeFlag === "transform") return false;
if (
Expand All @@ -51,6 +63,9 @@ export function canSkipRegexpu(
}

export function transformFlags(regexpuOptions: RegexpuOptions, flags: string) {
if (regexpuOptions.unicodeSetsFlag === "transform") {
flags = flags.replace("v", "u");
}
if (regexpuOptions.unicodeFlag === "transform") {
flags = flags.replace("u", "");
}
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-plugin-proposal-unicode-sets-regex/.npmignore
@@ -0,0 +1,3 @@
src
test
*.log
19 changes: 19 additions & 0 deletions packages/babel-plugin-proposal-unicode-sets-regex/README.md
@@ -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 packages/babel-plugin-proposal-unicode-sets-regex/package.json
@@ -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 packages/babel-plugin-proposal-unicode-sets-regex/src/index.ts
@@ -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");
},
});
});
@@ -0,0 +1 @@
/[[0-7]&&[5-9]]\u{10FFFF}/v;
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-unicode-sets-regex"]
}
@@ -0,0 +1 @@
/[5-7]\u{10FFFF}/u;
@@ -0,0 +1 @@
/[[0-7]&&[5-9]]\u{10FFFF}/v;
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-unicode-sets-regex", "transform-unicode-regex"]
}
@@ -0,0 +1 @@
/[5-7](?:\uDBFF\uDFFF)/;
@@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";

runner(import.meta.url);
@@ -0,0 +1 @@
{ "type": "module" }
3 changes: 3 additions & 0 deletions packages/babel-plugin-syntax-unicode-sets-regex/.npmignore
@@ -0,0 +1,3 @@
src
test
*.log
19 changes: 19 additions & 0 deletions packages/babel-plugin-syntax-unicode-sets-regex/README.md
@@ -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
```
48 changes: 48 additions & 0 deletions packages/babel-plugin-syntax-unicode-sets-regex/package.json
@@ -0,0 +1,48 @@
{
"name": "@babel/plugin-syntax-unicode-sets-regex",
"version": "7.16.7",
"description": "Parse regular expressions' unicodeSets (v) flag.",
"homepage": "https://babel.dev/docs/en/next/babel-plugin-syntax-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-syntax-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 packages/babel-plugin-syntax-unicode-sets-regex/src/index.ts
@@ -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: "syntax-unicode-sets-regex",
feature: "unicodeSetsFlag_syntax",
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("regexpUnicodeSets");
},
});
});
@@ -0,0 +1 @@
/[[0-7]&&[5-9]]/v;
@@ -0,0 +1,3 @@
{
"plugins": ["syntax-unicode-sets-regex"]
}
@@ -0,0 +1 @@
/[[0-7]&&[5-9]]/v;
@@ -0,0 +1 @@
/[[0-7]&&[5-9]]/v;
@@ -0,0 +1,3 @@
{
"throws": "Support for the experimental syntax 'regexpUnicodeSets' isn't currently enabled"
}
@@ -0,0 +1 @@
/[[0-7]&&[5-9]](?<n>.)/v;
@@ -0,0 +1,4 @@
{
"plugins": ["transform-named-capturing-groups-regex"],
"throws": "Support for the experimental syntax 'regexpUnicodeSets' isn't currently enabled"
}
3 changes: 3 additions & 0 deletions packages/babel-plugin-syntax-unicode-sets-regex/test/index.js
@@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";

runner(import.meta.url);
@@ -0,0 +1 @@
{ "type": "module" }
8 changes: 8 additions & 0 deletions tsconfig.json
Expand Up @@ -63,6 +63,7 @@
"./packages/babel-plugin-proposal-record-and-tuple/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",
"./packages/babel-plugin-syntax-async-do-expressions/src/**/*.ts",
"./packages/babel-plugin-syntax-decimal/src/**/*.ts",
"./packages/babel-plugin-syntax-decorators/src/**/*.ts",
Expand All @@ -79,6 +80,7 @@
"./packages/babel-plugin-syntax-record-and-tuple/src/**/*.ts",
"./packages/babel-plugin-syntax-throw-expressions/src/**/*.ts",
"./packages/babel-plugin-syntax-typescript/src/**/*.ts",
"./packages/babel-plugin-syntax-unicode-sets-regex/src/**/*.ts",
"./packages/babel-plugin-transform-arrow-functions/src/**/*.ts",
"./packages/babel-plugin-transform-async-to-generator/src/**/*.ts",
"./packages/babel-plugin-transform-block-scoped-functions/src/**/*.ts",
Expand Down Expand Up @@ -331,6 +333,9 @@
"@babel/plugin-proposal-unicode-property-regex": [
"./packages/babel-plugin-proposal-unicode-property-regex/src"
],
"@babel/plugin-proposal-unicode-sets-regex": [
"./packages/babel-plugin-proposal-unicode-sets-regex/src"
],
"@babel/plugin-syntax-async-do-expressions": [
"./packages/babel-plugin-syntax-async-do-expressions/src"
],
Expand Down Expand Up @@ -379,6 +384,9 @@
"@babel/plugin-syntax-typescript": [
"./packages/babel-plugin-syntax-typescript/src"
],
"@babel/plugin-syntax-unicode-sets-regex": [
"./packages/babel-plugin-syntax-unicode-sets-regex/src"
],
"@babel/plugin-transform-arrow-functions": [
"./packages/babel-plugin-transform-arrow-functions/src"
],
Expand Down