1. Minimal code to reproduce the bug
Input Code
function f() {
var a = 1;
var b = a;
var a = 2;
console.log(a, b);
}
f();
or
function f() {
var a = 1, b = a, a = 2;
console.log(a, b);
}
f();
Live example on babeljs.io
Actual Output
function f(){var c=1,c=2;console.log(c,c)}f();
Expected Output
function f(){var c=1,d=c;c=2,console.log(c,d)}f();
Details
Note the incorrect constant propagation in the console.log statement: it has console.log(c,c), even though the two arguments in the original are different. This happens because the variable a is declared multiple times as var, yet is not recognized as the same variable.
In the correct interpretation a=2 is recognized as a modification to the same variable, so constant propagation is not allowed to happen. Changing the second var a to simply a makes the problem disappear.
Slightly more complex example:
function loop() {
var end = 0;
var start = end;
while (end < 10) {
console.log(start, end);
var end = end + 1;
}
}
2. Stack Trace if there is an Error thrown during minification
None.
3. Configuration - babel-minify configuration or babelrc
Default.
The problem is with the deadcode option (babel-plugin-minify-dead-code-elimination), which is switched on by default.
4. Whether you used it with other presets/plugins - like es2015 or env
None.
5. Environment: gulp, rollup, webpack, babel-cli, etc...?
All.
1. Minimal code to reproduce the bug
Input Code
or
Live example on babeljs.io
Actual Output
Expected Output
Details
Note the incorrect constant propagation in the
console.logstatement: it hasconsole.log(c,c), even though the two arguments in the original are different. This happens because the variableais declared multiple times asvar, yet is not recognized as the same variable.In the correct interpretation
a=2is recognized as a modification to the same variable, so constant propagation is not allowed to happen. Changing the secondvar ato simplyamakes the problem disappear.Slightly more complex example:
2. Stack Trace if there is an Error thrown during minification
None.
3. Configuration - babel-minify configuration or babelrc
Default.
The problem is with the
deadcodeoption (babel-plugin-minify-dead-code-elimination), which is switched on by default.4. Whether you used it with other presets/plugins - like es2015 or env
None.
5. Environment: gulp, rollup, webpack, babel-cli, etc...?
All.