Fix unicode handling in generated function names (#14047)

This commit is contained in:
Sneh Khatri 2021-12-21 02:33:35 +05:30 committed by GitHub
parent ad17fe1cce
commit d6ff91d560
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 34 additions and 1 deletions

View File

@ -172,6 +172,7 @@ function visit(node, name, scope) {
/**
* @param {NodePath} param0
* @param {Boolean} localBinding whether a name could shadow a self-reference (e.g. converting arrow function)
* @param {Boolean} supportUnicodeId whether a target support unicodeId or not
*/
export default function (
{
@ -181,6 +182,7 @@ export default function (
id,
}: { node: any; parent?: any; scope: any; id?: any },
localBinding = false,
supportUnicodeId = false,
) {
// has an `id` so we don't need to infer one
if (node.id) return;
@ -227,6 +229,10 @@ export default function (
return;
}
if (!supportUnicodeId && isFunction(node) && /[\uD800-\uDFFF]/.test(name)) {
return;
}
name = toBindingIdentifierName(name);
id = identifier(name);

View File

@ -17,6 +17,7 @@
"babel-plugin"
],
"dependencies": {
"@babel/helper-compilation-targets": "workspace:^",
"@babel/helper-function-name": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^"
},

View File

@ -1,8 +1,13 @@
import { isRequired } from "@babel/helper-compilation-targets";
import { declare } from "@babel/helper-plugin-utils";
import nameFunction from "@babel/helper-function-name";
export default declare(api => {
api.assertVersion(7);
const supportUnicodeId = !isRequired(
"transform-unicode-escapes",
api.targets(),
);
return {
name: "transform-function-name",
@ -20,7 +25,7 @@ export default declare(api => {
ObjectProperty(path) {
const value = path.get("value");
if (value.isFunction()) {
const newNode = nameFunction(value);
const newNode = nameFunction(value, false, supportUnicodeId);
if (newNode) value.replaceWith(newNode);
}
},

View File

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

View File

@ -0,0 +1,4 @@
{
"plugins": ["transform-function-name", "transform-shorthand-properties"],
"targets": { "firefox": 52 }
}

View File

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

View File

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

View File

@ -0,0 +1,4 @@
{
"plugins": ["transform-function-name", "transform-shorthand-properties"],
"targets": { "firefox": 60 }
}

View File

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

View File

@ -2437,6 +2437,7 @@ __metadata:
resolution: "@babel/plugin-transform-function-name@workspace:packages/babel-plugin-transform-function-name"
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-compilation-targets": "workspace:^"
"@babel/helper-function-name": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"