Skip to content

Variable initialized before while moved to for-loop initializer  #742

@blutorange

Description

@blutorange

The simplify plugin seems to place variables declared and initialized before a while loop into a for loop, even if the variable is used again after the loop.

versions

babel-preset-minify@0.2.0
babel-cli@6.26.0

.babelrc

{
    "presets": [
        ["env", {"targets":{"browsers":["last 2 versions"]}}],
        ["minify", {}]
    ]
}

Input Code

(function() {
     let count = 10;
     while (count++ < 20) {}
     return count;
})();

Actual Output

npx babel test.js  --out-file test.min.js
"use strict";(function(){for(var a=10;20>a++;);return count})();

This gives a Uncaught ReferenceError: count is not defined(…).

Expected Output

"use strict";(function(){var a=10;while(20>a++){}return a})();

Details

It works as expected if I disabled the simplify plugin:

{
    "presets": [
        ["env", {
            "modules": false,
            "targets":{
"browsers": ["last 2 version"]
}
        }],
        ["minify", {
           "simplify": false
       }]
    ]
}

It also works if I change the input code as follows:

```js
(function() {
     let count = 10;
     0;
     while (count++ < 20) {}
     return count;
})();

Also, this

(function(x) {
     let count = 10;
     while (x++ < 20) {}
     return count;
})(5);

becomes

"use strict";(function(a){for(;20>a++;);return count})(5);

which removes the variable initialization to 10 completely.

This one also happens on babeljs.io. It works correctly if I either turn off all presets or minify.

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