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:
Huáng Jùnliàng 2019-12-13 08:39:37 -05:00 committed by Nicolò Ribaudo
parent 0b3f883ed1
commit bffa415b83
3 changed files with 24 additions and 1 deletions

View File

@ -32,7 +32,11 @@ function isSafeBinding(scope, node) {
const iifeVisitor = { const iifeVisitor = {
ReferencedIdentifier(path, state) { ReferencedIdentifier(path, state) {
const { scope, node } = path; 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; state.iife = true;
path.stop(); path.stop();
} }

View File

@ -0,0 +1,6 @@
let x = "outside";
function outer(a = () => x) {
let x = "inside";
return a();
}
outer();

View 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();