Skip to content

Commit

Permalink
Check shadow variable to identifier in default parameters (#10053)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Dec 13, 2019
1 parent 0b3f883 commit bffa415
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/babel-plugin-transform-parameters/src/params.js
Expand Up @@ -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();
}
Expand Down
@@ -0,0 +1,6 @@
let x = "outside";
function outer(a = () => x) {
let x = "inside";
return a();
}
outer();
@@ -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();

0 comments on commit bffa415

Please sign in to comment.