bublejs / bublejs/buble

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

Open
#264 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
874
Forks
62
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the provided loop example and trace Bublé's pseudo-block-scope loop transformation, especially how `continue` becomes a `return`. The fix is done when mutations to `i` are written back and the transformed code preserves the original program's output.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.