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

Fix unicode handling in generated function names #14047

Merged
merged 4 commits into from Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
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 @@ -223,6 +225,10 @@ export default function (
name = id.name;
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This test is more expensive, consider place it after the if (name === undefined) check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will move it down.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you commit the changes?


if (name === undefined) {
return;
}
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, null, supportUnicodeId);
The-x-Theorist marked this conversation as resolved.
Show resolved Hide resolved
if (newNode) value.replaceWith(newNode);
}
},
Expand Down
@@ -0,0 +1,3 @@
var o = {
"\uD835\uDC9C"() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test cases use shorthand-properties which is not supported by function-name. We can either plug in transform-shorthand-properties or just use ObjectProperty instead.

var o = {
  "\uD835\uDC9C": function () {}
}

When unicode escapes are supported, the output should be

var o = {
  "\uD835\uDC9C": function 𝒜() {}
};

otherwise, the output should be

var o = {
  "\uD835\uDC9C": function () {}
};

In this case the plugin does nothing.

};
@@ -0,0 +1,3 @@
{
The-x-Theorist marked this conversation as resolved.
Show resolved Hide resolved
"plugins": ["transform-function-name", "transform-shorthand-properties"]
}
@@ -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": 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 @@ -2438,6 +2438,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