Transform ES2015 Unicode Escapes to ES5 (#11377)

This commit is contained in:
Justin Ridgewell 2020-05-24 19:26:55 -04:00 committed by GitHub
parent 66b86e088c
commit 97f0b7c4a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
138 changed files with 468 additions and 2 deletions

View File

@ -98,6 +98,7 @@
"chrome": "62",
"opera": "49",
"edge": "79",
"firefox": "78",
"safari": "11.1",
"node": "8.10",
"ios": "11.3",
@ -108,6 +109,7 @@
"chrome": "64",
"opera": "51",
"edge": "79",
"firefox": "78",
"safari": "11.1",
"node": "10",
"ios": "11.3",
@ -279,6 +281,17 @@
"samsung": "5",
"electron": "0.37"
},
"transform-unicode-escapes": {
"chrome": "44",
"opera": "31",
"edge": "12",
"firefox": "53",
"safari": "9",
"node": "4",
"ios": "9",
"samsung": "4",
"electron": "0.30"
},
"transform-unicode-regex": {
"chrome": "50",
"opera": "37",

View File

@ -62,6 +62,7 @@ const es2015 = {
'RegExp "y" and "u" flags / "y" flag',
],
},
"transform-unicode-escapes": "Unicode code point escapes",
"transform-unicode-regex": {
features: [
'RegExp "y" and "u" flags / "u" flag, case folding',

View File

@ -1,7 +1,7 @@
#!/bin/bash
set -e
COMPAT_TABLE_COMMIT=9e07df3875d8416af85cf523716519e9dd1e5e44
COMPAT_TABLE_COMMIT=50e5424d113869b08911a5df956d0e931722e5b5
GIT_HEAD=build/compat-table/.git/HEAD
if [ -d "build/compat-table" ]; then

View File

@ -0,0 +1 @@
var foo = bar`\u0061\u{0061}\ud835\udc9c\u{1d49c}`;

View File

@ -0,0 +1,13 @@
function _templateObject() {
const data = _taggedTemplateLiteral(["aa\uD835\uDC9C\uD835\uDC9C"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"]);
_templateObject = function () {
return data;
};
return data;
}
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
var foo = bar(_templateObject());

View File

@ -0,0 +1,3 @@
src
test
*.log

View File

@ -0,0 +1,19 @@
# @babel/plugin-transform-unicode-escapes
> Compile ES2015 Unicode escapes to ES5
See our website [@babel/plugin-transform-unicode-escapes](https://babeljs.io/docs/en/next/babel-plugin-transform-unicode-escapes.html) for more information.
## Install
Using npm:
```sh
npm install --save-dev @babel/plugin-transform-unicode-escapes
```
or using yarn:
```sh
yarn add @babel/plugin-transform-unicode-escapes --dev
```

View File

@ -0,0 +1,24 @@
{
"name": "@babel/plugin-transform-unicode-escapes",
"version": "7.8.3",
"description": "Compile ES2015 Unicode escapes to ES5",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-unicode-escapes",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"@babel/helper-plugin-utils": "^7.8.3"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
},
"devDependencies": {
"@babel/core": "^7.8.3",
"@babel/helper-plugin-test-runner": "^7.8.3"
}
}

View File

@ -0,0 +1,107 @@
import { declare } from "@babel/helper-plugin-utils";
import { types as t } from "@babel/core";
export default declare(api => {
api.assertVersion(7);
const surrogate = /[\ud800-\udfff]/g;
const unicodeEscape = /(\\+)u\{([0-9a-fA-F]+)\}/g;
function escape(code) {
let str = code.toString(16);
// Sigh, node 6 doesn't have padStart
// TODO: Remove in Babel 8, when we drop node 6.
while (str.length < 4) str = "0" + str;
return "\\u" + str;
}
function replacer(match, backslashes, code) {
if (backslashes.length % 2 === 0) {
return match;
}
const char = String.fromCodePoint(parseInt(code, 16));
const escaped = backslashes.slice(0, -1) + escape(char.charCodeAt(0));
return char.length === 1 ? escaped : escaped + escape(char.charCodeAt(1));
}
function replaceUnicodeEscapes(str) {
return str.replace(unicodeEscape, replacer);
}
function getUnicodeEscape(str) {
let match;
while ((match = unicodeEscape.exec(str))) {
if (match[1].length % 2 === 0) continue;
unicodeEscape.lastIndex = 0;
return match[0];
}
return null;
}
return {
name: "transform-unicode-escapes",
visitor: {
Identifier(path) {
const { node, key } = path;
const { name } = node;
const replaced = name.replace(surrogate, c => {
return `_u${c.charCodeAt(0).toString(16)}`;
});
if (name === replaced) return;
const str = t.inherits(t.stringLiteral(name), node);
if (key === "key") {
path.replaceWith(str);
return;
}
const { parentPath, scope } = path;
if (
parentPath.isMemberExpression({ property: node }) ||
parentPath.isOptionalMemberExpression({ property: node })
) {
parentPath.node.computed = true;
path.replaceWith(str);
return;
}
const binding = scope.getBinding(name);
if (binding) {
scope.rename(name, scope.generateUid(replaced));
return;
}
throw path.buildCodeFrameError(
`Can't reference '${name}' as a bare identifier`,
);
},
"StringLiteral|DirectiveLiteral"(path) {
const { node } = path;
const { extra } = node;
if (extra?.raw) extra.raw = replaceUnicodeEscapes(extra.raw);
},
TemplateElement(path) {
const { node, parentPath } = path;
const { value } = node;
const firstEscape = getUnicodeEscape(value.raw);
if (!firstEscape) return;
const grandParent = parentPath.parentPath;
if (grandParent.isTaggedTemplateExpression()) {
throw path.buildCodeFrameError(
`Can't replace Unicode escape '${firstEscape}' inside tagged template literals. You can enable '@babel/plugin-transform-template-literals' to compile them to classic strings.`,
);
}
value.raw = replaceUnicodeEscapes(value.raw);
},
},
};
});

View File

@ -0,0 +1 @@
"𝒜\ud835\udc9c\u{1d49c}"

View File

@ -0,0 +1 @@
"𝒜\ud835\udc9c\ud835\udc9c";

View File

@ -0,0 +1,3 @@
{
"throws": "Can't reference '𝒜' as a bare identifier"
}

View File

@ -0,0 +1,2 @@
var \u{1d49c} = 1;
\u{1d49c};

View File

@ -0,0 +1,2 @@
var _ud835_udc9c = 1;
_ud835_udc9c;

View File

@ -0,0 +1 @@
var o = class { get \u{1d49c}() {} };

View File

@ -0,0 +1,4 @@
var o = class {
get "\uD835\uDC9C"() {}
};

View File

@ -0,0 +1 @@
var o = class { \u{1d49c} = 1 };

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-unicode-escapes", "syntax-class-properties"]
}

View File

@ -0,0 +1,3 @@
var o = class {
"\uD835\uDC9C" = 1;
};

View File

@ -0,0 +1 @@
var o = class { \u{1d49c}() {} };

View File

@ -0,0 +1,4 @@
var o = class {
"\uD835\uDC9C"() {}
};

View File

@ -0,0 +1 @@
var o = { get \u{1d49c}() {} };

View File

@ -0,0 +1,4 @@
var o = {
get "\uD835\uDC9C"() {}
};

View File

@ -0,0 +1 @@
var o = { \u{1d49c}() {} };

View File

@ -0,0 +1,4 @@
var o = {
"\uD835\uDC9C"() {}
};

View File

@ -0,0 +1,3 @@
var o = {
"\uD835\uDC9C": 1
};

View File

@ -0,0 +1,3 @@
{
"throws": "Can't reference '𝒜' as a bare identifier"
}

View File

@ -0,0 +1,2 @@
var \u{1d49c} = 1;
var o = { \u{1d49c} };

View File

@ -0,0 +1,4 @@
var _ud835_udc9c = 1;
var o = {
"\uD835\uDC9C": _ud835_udc9c
};

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-unicode-escapes"]
}

View File

@ -0,0 +1,3 @@
{
"throws": "Can't reference '𝒜' as a bare identifier"
}

View File

@ -0,0 +1,2 @@
var 𝒜 = 1;
𝒜;

View File

@ -0,0 +1,2 @@
var _ud835_udc9c = 1;
_ud835_udc9c;

View File

@ -0,0 +1 @@
var o = class { get 𝒜 () {} };

View File

@ -0,0 +1,4 @@
var o = class {
get "\uD835\uDC9C"() {}
};

View File

@ -0,0 +1 @@
var o = class { 𝒜 = 1 };

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-unicode-escapes", "syntax-class-properties"]
}

View File

@ -0,0 +1,3 @@
var o = class {
"\uD835\uDC9C" = 1;
};

View File

@ -0,0 +1 @@
var o = class { 𝒜 () {} };

View File

@ -0,0 +1,4 @@
var o = class {
"\uD835\uDC9C"() {}
};

View File

@ -0,0 +1 @@
var o = { get 𝒜 () {} };

View File

@ -0,0 +1,4 @@
var o = {
get "\uD835\uDC9C"() {}
};

View File

@ -0,0 +1 @@
var o = { 𝒜 () {} };

View File

@ -0,0 +1,4 @@
var o = {
"\uD835\uDC9C"() {}
};

View File

@ -0,0 +1,3 @@
var o = {
"\uD835\uDC9C": 1
};

View File

@ -0,0 +1,3 @@
{
"throws": "Can't reference '𝒜' as a bare identifier"
}

View File

@ -0,0 +1,2 @@
var 𝒜 = 1;
var o = { 𝒜 };

View File

@ -0,0 +1,4 @@
var _ud835_udc9c = 1;
var o = {
"\uD835\uDC9C": _ud835_udc9c
};

View File

@ -0,0 +1,7 @@
0;
"𝒜\ud835\udc9c\u{1d49c}";
"\u{1d49c}";
"\\u{1d49c}";
"\\\u{1d49c}";
"\\\\u{1d49c}";

View File

@ -0,0 +1,6 @@
0;
"𝒜\ud835\udc9c\ud835\udc9c";
"\ud835\udc9c";
"\\u{1d49c}";
"\\\ud835\udc9c";
"\\\\u{1d49c}";

View File

@ -0,0 +1,6 @@
test`𝒜\ud835\udc9c\u{1d49c}`;
test`\u{1d49c}`;
test`\\u{1d49c}`;
test`\\\u{1d49c}`;
test`\\\\u{1d49c}`;

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-unicode-escapes", "transform-template-literals"]
}

View File

@ -0,0 +1,57 @@
function _templateObject5() {
const data = _taggedTemplateLiteral(["\\\\u{1d49c}"], ["\\\\\\\\u{1d49c}"]);
_templateObject5 = function () {
return data;
};
return data;
}
function _templateObject4() {
const data = _taggedTemplateLiteral(["\\\uD835\uDC9C"], ["\\\\\\u{1d49c}"]);
_templateObject4 = function () {
return data;
};
return data;
}
function _templateObject3() {
const data = _taggedTemplateLiteral(["\\u{1d49c}"], ["\\\\u{1d49c}"]);
_templateObject3 = function () {
return data;
};
return data;
}
function _templateObject2() {
const data = _taggedTemplateLiteral(["\uD835\uDC9C"], ["\\u{1d49c}"]);
_templateObject2 = function () {
return data;
};
return data;
}
function _templateObject() {
const data = _taggedTemplateLiteral(["\uD835\uDC9C\uD835\uDC9C\uD835\uDC9C"], ["\uD835\uDC9C\\ud835\\udc9c\\u{1d49c}"]);
_templateObject = function () {
return data;
};
return data;
}
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
test(_templateObject());
test(_templateObject2());
test(_templateObject3());
test(_templateObject4());
test(_templateObject5());

View File

@ -0,0 +1 @@
test`𝒜\ud835\udc9c\u{1d49c}`;

View File

@ -0,0 +1,3 @@
{
"throws": "Can't replace Unicode escape '\\u{1d49c}' inside tagged template literals. You can enable '@babel/plugin-transform-template-literals' to compile them to classic strings."
}

View File

@ -0,0 +1,6 @@
`𝒜\ud835\udc9c\u{1d49c}`;
`\u{1d49c}`;
`\\u{1d49c}`;
`\\\u{1d49c}`;
`\\\\u{1d49c}`;

View File

@ -0,0 +1,5 @@
`𝒜\ud835\udc9c\ud835\udc9c`;
`\ud835\udc9c`;
`\\u{1d49c}`;
`\\\ud835\udc9c`;
`\\\\u{1d49c}`;

View File

@ -0,0 +1,4 @@
var \u0061 = {\u0061: 1};
\u0061.\u0061;
var o = { \u0061 };

View File

@ -0,0 +1,7 @@
var a = {
a: 1
};
a.a;
var o = {
a
};

View File

@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);

View File

@ -66,6 +66,7 @@
"@babel/plugin-transform-sticky-regex": "^7.8.3",
"@babel/plugin-transform-template-literals": "^7.8.3",
"@babel/plugin-transform-typeof-symbol": "^7.8.4",
"@babel/plugin-transform-unicode-escapes": "^7.8.3",
"@babel/plugin-transform-unicode-regex": "^7.8.3",
"@babel/preset-modules": "^0.1.3",
"@babel/types": "^7.9.6",

View File

@ -52,6 +52,7 @@ import transformSpread from "@babel/plugin-transform-spread";
import transformStickyRegex from "@babel/plugin-transform-sticky-regex";
import transformTemplateLiterals from "@babel/plugin-transform-template-literals";
import transformTypeofSymbol from "@babel/plugin-transform-typeof-symbol";
import transformUnicodeEscapes from "@babel/plugin-transform-unicode-escapes";
import transformUnicodeRegex from "@babel/plugin-transform-unicode-regex";
import bugfixAsyncArrowsInClass from "@babel/preset-modules/lib/plugins/transform-async-arrows-in-class";
@ -119,5 +120,6 @@ export default {
"transform-sticky-regex": transformStickyRegex,
"transform-template-literals": transformTemplateLiterals,
"transform-typeof-symbol": transformTypeofSymbol,
"transform-unicode-escapes": transformUnicodeEscapes,
"transform-unicode-regex": transformUnicodeRegex,
};

View File

@ -31,6 +31,7 @@ Using plugins:
transform-computed-properties { "chrome":"40" }
transform-for-of { "chrome":"40" }
transform-sticky-regex { "chrome":"40" }
transform-unicode-escapes { "chrome":"40" }
transform-unicode-regex { "chrome":"40" }
transform-spread { "chrome":"40" }
transform-block-scoping { "chrome":"40" }

View File

@ -32,6 +32,7 @@ Using plugins:
transform-computed-properties { "android":"3" }
transform-for-of { "android":"3" }
transform-sticky-regex { "android":"3" }
transform-unicode-escapes { "android":"3" }
transform-unicode-regex { "android":"3" }
transform-spread { "android":"3" }
transform-destructuring { "android":"3" }

View File

@ -39,6 +39,7 @@ Using plugins:
transform-computed-properties { "ie":"11" }
transform-for-of { "ie":"11" }
transform-sticky-regex { "ie":"11" }
transform-unicode-escapes { "ie":"11" }
transform-unicode-regex { "ie":"11" }
transform-spread { "ie":"11" }
transform-destructuring { "ie":"11" }

View File

@ -32,6 +32,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -32,6 +32,7 @@ Using plugins:
transform-computed-properties { "android":"4" }
transform-for-of { "android":"4" }
transform-sticky-regex { "android":"4" }
transform-unicode-escapes { "android":"4" }
transform-unicode-regex { "android":"4" }
transform-spread { "android":"4" }
transform-destructuring { "android":"4" }

View File

@ -32,6 +32,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -30,6 +30,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -33,6 +33,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -37,6 +37,7 @@ Using plugins:
transform-computed-properties { "ie":"10", "safari":"7" }
transform-for-of { "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
transform-sticky-regex { "ie":"10", "ios":"9", "safari":"7" }
transform-unicode-escapes { "firefox":"49", "ie":"10", "safari":"7" }
transform-unicode-regex { "ie":"10", "ios":"9", "safari":"7" }
transform-spread { "ie":"10", "ios":"9", "safari":"7" }
transform-destructuring { "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }

View File

@ -43,6 +43,7 @@ Using plugins:
transform-computed-properties { "ie":"10" }
transform-for-of { "electron":"0.36", "ie":"10", "node":"6.1" }
transform-sticky-regex { "electron":"0.36", "ie":"10" }
transform-unicode-escapes { "ie":"10" }
transform-unicode-regex { "electron":"0.36", "ie":"10" }
transform-spread { "ie":"10" }
transform-destructuring { "electron":"0.36", "ie":"10", "node":"6.1" }

View File

@ -34,6 +34,7 @@ Using plugins:
transform-computed-properties { "ie":"10" }
transform-for-of { "ie":"10" }
transform-sticky-regex { "ie":"10" }
transform-unicode-escapes { "ie":"10" }
transform-unicode-regex { "ie":"10" }
transform-spread { "ie":"10" }
transform-destructuring { "ie":"10" }

View File

@ -34,6 +34,7 @@ Using plugins:
transform-computed-properties { "ie":"10" }
transform-for-of { "ie":"10", "node":"6" }
transform-sticky-regex { "ie":"10" }
transform-unicode-escapes { "ie":"10" }
transform-unicode-regex { "ie":"10" }
transform-spread { "ie":"10" }
transform-destructuring { "ie":"10", "node":"6" }

View File

@ -33,6 +33,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -32,6 +32,7 @@ Using plugins:
transform-computed-properties { "android":"4" }
transform-for-of { "android":"4" }
transform-sticky-regex { "android":"4" }
transform-unicode-escapes { "android":"4" }
transform-unicode-regex { "android":"4" }
transform-spread { "android":"4" }
transform-destructuring { "android":"4" }

View File

@ -33,6 +33,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -33,6 +33,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -33,6 +33,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -32,6 +32,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -30,6 +30,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -33,6 +33,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -37,6 +37,7 @@ Using plugins:
transform-computed-properties { "ie":"10", "safari":"7" }
transform-for-of { "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
transform-sticky-regex { "ie":"10", "ios":"9", "safari":"7" }
transform-unicode-escapes { "firefox":"49", "ie":"10", "safari":"7" }
transform-unicode-regex { "ie":"10", "ios":"9", "safari":"7" }
transform-spread { "ie":"10", "ios":"9", "safari":"7" }
transform-destructuring { "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }

View File

@ -33,6 +33,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -33,6 +33,7 @@ Using plugins:
transform-computed-properties {}
transform-for-of {}
transform-sticky-regex {}
transform-unicode-escapes {}
transform-unicode-regex {}
transform-spread {}
transform-destructuring {}

View File

@ -43,6 +43,7 @@ Using plugins:
transform-computed-properties { "ie":"10" }
transform-for-of { "electron":"0.36", "ie":"10", "node":"6.1" }
transform-sticky-regex { "electron":"0.36", "ie":"10" }
transform-unicode-escapes { "ie":"10" }
transform-unicode-regex { "electron":"0.36", "ie":"10" }
transform-spread { "ie":"10" }
transform-destructuring { "electron":"0.36", "ie":"10", "node":"6.1" }

Some files were not shown because too many files have changed in this diff Show More