Skip to content

Commit

Permalink
fix: early return when param binding is not found (#13910)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 1, 2021
1 parent cba7f9e commit 833b391
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 8 deletions.
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
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

0 comments on commit 833b391

Please sign in to comment.