bublejs / buble

https://buble.surge.sh
MIT License
871 stars 64 forks source link

Mutated variables in pseudo-block-scope loops aren't written back when using `continue` #264

Open marijnh opened 4 years ago

marijnh commented 4 years ago

For this code...

for (let i = 0; i < 5; i++) {
  console.log(i++, [2].every(x => x != i))
  continue
}

Bublé creates this:

var loop = function ( i$1 ) {
  console.log(i$1++, [2].every(function (x) { return x != i$1; }))
  return
};

for (var i = 0; i < 5; i++) loop( i );

It'd normally add an i = i$1 to the end of the function, because it noticed that i$1 is mutated, but for some reason it doesn't do that when the end of the loop isn't reachable. I originally observed a case where the assignment was generated, but not reached because of a continue (compiled to a return).

In any case, this generated code produces different output than the original, because the mutations to i are dropped at the end of the loop body.