Incorrect code generation in nested for loop
A generated "this" local isn't being created or used under at least one condition.
Source code:
class Foo {
constructor() {
this.foo = { a: 1, b: 2 };
}
do() {
for (let move = 0; move < 10; ++move) {
for (let id in this.foo) {
console.log(id);
}
let bar = ()=>{ console.log(move); };
// ...
}
}
}
Generated Code:
var Foo = function Foo() {
this.foo = { a: 1, b: 2 };
};
Foo.prototype.do = function do$1 () {
var loop = function ( move ) {
for (var id in this.foo) {
console.log(id);
}
var bar = function (){ console.log(move); };
// ...
};
for (var move = 0; move < 10; ++move) loop( move );
};
That's as simple as I could make the repro case.
The problem is in for (var id in this.foo)
. That line references this
in do$1, not the Class this. So it fails. This is true even if another line generates a this$1 inside of do().
The problem only shows up when, inside the loop, a closure is created that binds to the outer loop variable, which triggers the inner loop to be wrapped in a function. It doesn't matter whether the "let bar=" line is before or after the loop.