Check shadow variable to identifier in default parameters (#10053)
When there is a variable declaration inside the function body, which shares its name to a referenced identifier in default parameter expression, the function body should be wrapped into iife, otherwise the binding in default parameter scope will be shadowed by function body.
This commit is contained in:
parent
0b3f883ed1
commit
bffa415b83
@ -32,7 +32,11 @@ function isSafeBinding(scope, node) {
|
||||
const iifeVisitor = {
|
||||
ReferencedIdentifier(path, state) {
|
||||
const { scope, node } = path;
|
||||
if (node.name === "eval" || !isSafeBinding(scope, node)) {
|
||||
if (
|
||||
node.name === "eval" ||
|
||||
!isSafeBinding(scope, node) ||
|
||||
!isSafeBinding(state.scope, node)
|
||||
) {
|
||||
state.iife = true;
|
||||
path.stop();
|
||||
}
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
let x = "outside";
|
||||
function outer(a = () => x) {
|
||||
let x = "inside";
|
||||
return a();
|
||||
}
|
||||
outer();
|
||||
13
packages/babel-plugin-transform-parameters/test/fixtures/parameters/default-iife-9947/output.js
vendored
Normal file
13
packages/babel-plugin-transform-parameters/test/fixtures/parameters/default-iife-9947/output.js
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
var x = "outside";
|
||||
|
||||
function outer() {
|
||||
var a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {
|
||||
return x;
|
||||
};
|
||||
return function () {
|
||||
var x = "inside";
|
||||
return a();
|
||||
}();
|
||||
}
|
||||
|
||||
outer();
|
||||
Loading…
x
Reference in New Issue
Block a user