fix: early return when param binding is not found (#13910)

This commit is contained in:
Huáng Jùnliàng 2021-11-01 15:11:30 -04:00 committed by GitHub
parent cba7f9e503
commit 833b39112d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 72 additions and 8 deletions

View File

@ -30,6 +30,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "workspace:^", "@babel/core": "workspace:^",
"@babel/helper-function-name": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^", "@babel/helper-plugin-test-runner": "workspace:^",
"@babel/traverse": "workspace:^" "@babel/traverse": "workspace:^"
}, },

View File

@ -18,16 +18,16 @@ export function shouldTransform(
// On collision, `getOwnBinding` returns the param binding // On collision, `getOwnBinding` returns the param binding
// with the id binding be registered as constant violation // with the id binding be registered as constant violation
const paramNameBinding = path.scope.getOwnBinding(name); const paramNameBinding = path.scope.getOwnBinding(name);
const constantViolations = paramNameBinding.constantViolations; if (paramNameBinding === undefined) {
if (constantViolations.length === 0) { // Case 1: the function id is injected by babel-helper-name-function, which
// the function scope has no such collided bindings // 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; return false;
} }
const firstViolation = constantViolations[0]; if (paramNameBinding.kind !== "param") {
// the function id does not reproduce in params
if (firstViolation.node !== node) {
// the violation does not happen in id
// e.g. (function a() { var a; })
return false; return false;
} }

View File

@ -0,0 +1,6 @@
{
"plugins": [
"./plugin.js",
"bugfix-safari-id-destructuring-collision-in-function-expression"
]
}

View File

@ -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);
},
},
});

View File

@ -0,0 +1,6 @@
{
"plugins": [
"bugfix-safari-id-destructuring-collision-in-function-expression",
"./plugin.js"
]
}

View File

@ -0,0 +1,11 @@
module.exports = ({ types: { identifier } }) => ({
visitor: {
FunctionExpression(path) {
const { node } = path;
if (!node.id) {
node.id = identifier("a");
path.requeue();
}
},
},
});

View File

@ -0,0 +1,6 @@
{
"plugins": [
"./plugin.js",
"bugfix-safari-id-destructuring-collision-in-function-expression"
]
}

View File

@ -0,0 +1,11 @@
module.exports = ({ types: { identifier } }) => ({
visitor: {
FunctionExpression(path) {
const { node } = path;
if (!node.id) {
node.id = identifier("a");
path.requeue();
}
},
},
});

View File

@ -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" resolution: "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression@workspace:packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression"
dependencies: dependencies:
"@babel/core": "workspace:^" "@babel/core": "workspace:^"
"@babel/helper-function-name": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^" "@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^" "@babel/helper-plugin-utils": "workspace:^"
"@babel/traverse": "workspace:^" "@babel/traverse": "workspace:^"