Skip to content

Commit

Permalink
Fix unicode handling in generated function names (#14047)
Browse files Browse the repository at this point in the history
  • Loading branch information
The-x-Theorist committed Dec 20, 2021
1 parent ad17fe1 commit d6ff91d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/babel-helper-function-name/src/index.ts
Expand Up @@ -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 (
{
Expand All @@ -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;
Expand Down Expand Up @@ -227,6 +229,10 @@ export default function (
return;
}

if (!supportUnicodeId && isFunction(node) && /[\uD800-\uDFFF]/.test(name)) {
return;
}

name = toBindingIdentifierName(name);
id = identifier(name);

Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-function-name/package.json
Expand Up @@ -17,6 +17,7 @@
"babel-plugin"
],
"dependencies": {
"@babel/helper-compilation-targets": "workspace:^",
"@babel/helper-function-name": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^"
},
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-plugin-transform-function-name/src/index.ts
@@ -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",
Expand All @@ -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);
}
},
Expand Down
@@ -0,0 +1,3 @@
var o = {
"\uD835\uDC9C"() {}
};
@@ -0,0 +1,4 @@
{
"plugins": ["transform-function-name", "transform-shorthand-properties"],
"targets": { "firefox": 52 }
}
@@ -0,0 +1,3 @@
var o = {
"\uD835\uDC9C": function () {}
};
@@ -0,0 +1,3 @@
var o = {
"\uD835\uDC9C"() {}
};
@@ -0,0 +1,4 @@
{
"plugins": ["transform-function-name", "transform-shorthand-properties"],
"targets": { "firefox": 60 }
}
@@ -0,0 +1,3 @@
var o = {
"\uD835\uDC9C": function 饾挏() {}
};
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -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:^"
Expand Down

0 comments on commit d6ff91d

Please sign in to comment.