babel
babel copied to clipboard
Incorrect scope after transform-es2015-block-scoping (T7429)
Issue originally made by Juriy Zaytsev (kangax)
Options
{ plugins: ['transform-es2015-block-scoping', {
visitor: {
CallExpression: function(path) {
console.log(`
${path.node.callee.name} ---
own: ${Object.keys(path.scope.bindings)}
parent: ${Object.keys(path.scope.parent.bindings)}
`.replace(/\s+/g, ' '));
}
}
}] },
Input code
function f(x) {
let smth = x.foo;
for (let i = 0; i; i++) {
let n;
if (n) return;
g(() => n);
}
}
Transformed with block-scoping to:
function f(x) {
var smth = x.foo;
var _loop = function (i) {
var n = void 0;
if (n) return {
v: void 0
};
g(() => n);
};
for (var i = 0; i; i++) {
var _ret = _loop(i);
if (typeof _ret === "object") return _ret.v;
}
}
Description
This outputs:
g --- own: i,n parent: x,smth,_loop,_ret
_loop --- own: parent: i
When encountering CallExpression within a loop, the scope doesn't look correct. Current scope should have _ret, i, _loop, and x bindings.