-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Bug report or feature request?
Bug report
ES5 or ES6+ input?
ES5 input using uglify-es@3.3.9
Uglify version (uglifyjs -V)
uglify-es 3.3.9
JavaScript input
var index = 0;
var whereIsTheLetterC = 0;
[ 'a', 'b', 'c' ].forEach( function( letter ) {
index++;
if ( 'c' === letter ) {
whereIsTheLetterC = index;
}
} );
console.log( 'This letter "c" is number 3: %d', whereIsTheLetterC );The uglifyjs CLI command executed or minify() options used.
--compress conditionals=trueThe issue is not observed when:
--compress conditionals=falseJavaScript output or error produced.
var index = 0, whereIsTheLetterC = 0;
[ "a", "b", "c" ].forEach(function(letter) {
"c" === letter && (whereIsTheLetterC = ++index);
}), console.log('This letter "c" is number 3: %d', whereIsTheLetterC);Detailed description
In the samples above, the input will produce:
This letter "c" is number 3: 3
The uglified (compressed conditionals) version will produce:
This letter "c" is number 3: 1
You can see that in the input, index will always be incremented. However, in the uglified output, index is prefix-incremented only when "c" === letter evaluates to true.
This bug was discovered in Automattic/wp-calypso#21489
The following is the "real-life" code that exposed the bug:
The bug was noticed in the result of applying uglify-es to the above code as part of a Webpack build using https://github.com/webpack-contrib/uglifyjs-webpack-plugin.
Massive thanks to @ockham for help with this issue.