Skip to content

uglify-es compress conditionals: increment erroneously not reached #3010

@sirreal

Description

@sirreal

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=true

The issue is not observed when:

--compress conditionals=false

JavaScript 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:

https://github.com/visionmedia/debug/blob/22f993216dcdcee07eb0601ea71a917e4925a30a/src/browser.js#L90-L102

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions