A bug when destructuring in a for .. of loop
Opened this issue · 1 comments
mourner commented
Minimal test case:
{
let a;
}
for (const [a] of b) a();
Produces:
{
var a;
}
for (var i = 0, list = b; i < list.length; i += 1) {
var ref = list[i];
var a = ref[0];
a$1();
}
Note that the a$1
reference at the end is incorrect, since this symbol is not defined.
thomaswelter commented
You can sidestep this issue for now by using var instead of let or const, or by using assignment like for([a] of b) a()
The problem seems to be that in BlockStatement.js
a new alias is created but the Node is kept the same. Then later when destructure
is called in ForOfStatement.js
the resolveName
is passed that node in destructure.js
at destructureIdentifier
causing the old identifier to be used.
I can create a fix this but I need a little guidance. I don't quite understand where this needs to be changed. I see two parts that I think might be important:
destructureIdentifier
indestructure.js
:
resolveName should take into account the either an updated Node or receive the declaration with the rewritten = true property set.- In
BlockStatement.js
attranspileBlockScopedIdentifiers
:
declaration.name = alias
could be changed todeclaration.node.name
. But this is not a full solution because it creates problems in other places (such as property access).