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

skip id-destructuring bugfix when binding info is not found #13910

Merged
merged 1 commit into from Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -30,6 +30,7 @@
},
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/helper-function-name": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^",
"@babel/traverse": "workspace:^"
},
Expand Down
Expand Up @@ -18,16 +18,16 @@ export function shouldTransform(
// On collision, `getOwnBinding` returns the param binding
// with the id binding be registered as constant violation
const paramNameBinding = path.scope.getOwnBinding(name);
const constantViolations = paramNameBinding.constantViolations;
if (constantViolations.length === 0) {
// the function scope has no such collided bindings
if (paramNameBinding === undefined) {
// Case 1: the function id is injected by babel-helper-name-function, which
// assigns `NOT_LOCAL_BINDING` to the `functionId` and thus not registering id
// in scope tracking
// Case 2: the function id is injected by a third party plugin which does not update the
// scope info
Comment on lines +25 to +26
Copy link
Member

Choose a reason for hiding this comment

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

If we are in Case 2, it means that the function didn't originally have a paramter with the same name of the new injected id?

e.g. does this work?

let x = function ([a]) {};
// plugin
Function(path) {
  path.node.id = t.identifier("a");
}

(if it doesn't it's ok - the plugin needs to be fixed)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it works if the path is then requeued (as is shown in the https://github.com/babel/babel/pull/13910/files#diff-9c3e0b47cab12356380982d8d3c8b5c3bbf3e160e18448619c2b54e9106bcb03) so that the bugfix can have a chance to be executed again.

return false;
}
const firstViolation = constantViolations[0];

if (firstViolation.node !== node) {
// the violation does not happen in id
// e.g. (function a() { var a; })
if (paramNameBinding.kind !== "param") {
// the function id does not reproduce in params
return false;
}

Expand Down
@@ -0,0 +1 @@
var a = function ([a]) { a };
@@ -0,0 +1,6 @@
{
"plugins": [
"./plugin.js",
"bugfix-safari-id-destructuring-collision-in-function-expression"
]
}
@@ -0,0 +1,3 @@
var a = function a([_a]) {
_a;
};
@@ -0,0 +1,10 @@
const nameFunction = require("@babel/helper-function-name").default;

module.exports = api => ({
visitor: {
FunctionExpression(path) {
const replacement = nameFunction(path);
if (replacement) path.replaceWith(replacement);
},
},
});
@@ -0,0 +1 @@
(function ([a]) { a });
@@ -0,0 +1,6 @@
{
"plugins": [
"bugfix-safari-id-destructuring-collision-in-function-expression",
"./plugin.js"
]
}
@@ -0,0 +1,3 @@
(function a([_a]) {
_a;
});
@@ -0,0 +1,11 @@
module.exports = ({ types: { identifier } }) => ({
visitor: {
FunctionExpression(path) {
const { node } = path;
if (!node.id) {
node.id = identifier("a");
path.requeue();
}
},
},
});
@@ -0,0 +1 @@
(function ([a]) { a });
@@ -0,0 +1,6 @@
{
"plugins": [
"./plugin.js",
"bugfix-safari-id-destructuring-collision-in-function-expression"
]
}
@@ -0,0 +1,3 @@
(function a([_a]) {
_a;
});
@@ -0,0 +1,11 @@
module.exports = ({ types: { identifier } }) => ({
visitor: {
FunctionExpression(path) {
const { node } = path;
if (!node.id) {
node.id = identifier("a");
path.requeue();
}
},
},
});
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -1047,6 +1047,7 @@ __metadata:
resolution: "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression@workspace:packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression"
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-function-name": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"
"@babel/traverse": "workspace:^"
Expand Down