Skip to content

Constants defined before a while-loop are removed or moved out of scope #801

Description

@rykdesjardins

I saw this was fixed and merged in #713, but I can't seem to make it work. Using version 0.3.0 from npm, on Linux.

The smallest code snippet I could come up with is the following.

const array = [];
let loopvar = true;
while (loopvar) { loopvar = false }
array.push(0);

REPL - Try it here

To reproduce, we need a variable defined before a while loop, a while loop, and to refer to the previously defined variable.

Actual Output

// Minified
for(var loopvar=!0;loopvar;)loopvar=!1;array.push(0);

// Manually unminified
for(var loopvar=!0;loopvar;) {
  loopvar=!1;
}
array.push(0);

Expected Output

Something similar to this.

var array = [];
for(var loopvar=!0;loopvar;) {
  loopvar=!1;
}
array.push(0);

Details

For the babelrc, I'm only using two presets.

{
  "presets": ["minify", "es2015"]
}

I believe this is coming from the following sub-package (plugin) : https://github.com/babel/minify/tree/master/packages/babel-plugin-transform-merge-sibling-variables. Might take a look at it and see if I can actually contribute and fix.

The most basic npm script I could come up with is babel dev.js -o build.js. For now, I'm using a simple workaround where I define the array in a higher scope, but it's bad programming. I was thinking of maybe using a reducer instead, but I figured this was worth opening an issue anyways.

For a concrete test, try cloning this repo https://github.com/rykdesjardins/lilium-text, run npm install && npm run build, and open the build/liliumtext.js file. You can find another of a similar issue example if you search for the function called createSelectionContext under LiliumText.

Let me know if you need any additional information!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bughas PRHas an open PR that fixes this issue

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions