fix: early return when param binding is not found (#13910)
This commit is contained in:
parent
cba7f9e503
commit
833b39112d
@ -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:^"
|
||||||
},
|
},
|
||||||
|
|||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
@ -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:^"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user