Skip to content

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

@marijnh

Description

@marijnh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions