diff --git a/lib/compress.js b/lib/compress.js index 99543fc984e..c7b1b792f01 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -12865,19 +12865,17 @@ Compressor.prototype.compress = function(node) { var alt_tail = alternative.tail_node(); // x ? y : y ---> x, y // x ? (a, c) : (b, c) ---> x ? a : b, c - if (seq_tail.equals(alt_tail)) { - return make_sequence(self, consequent.equals(alternative) ? [ - condition, - consequent, - ] : [ - make_node(AST_Conditional, self, { - condition: condition, - consequent: pop_seq(consequent), - alternative: pop_seq(alternative), - }), - seq_tail, - ]).optimize(compressor); - } + if (seq_tail.equals(alt_tail)) return make_sequence(self, consequent.equals(alternative) ? [ + condition, + consequent, + ] : [ + make_node(AST_Conditional, self, { + condition: condition, + consequent: pop_seq(consequent), + alternative: pop_seq(alternative), + }), + alt_tail, + ]).optimize(compressor); // x ? y.p : z.p ---> (x ? y : z).p // x ? y(a) : z(a) ---> (x ? y : z)(a) // x ? y.f(a) : z.f(a) ---> (x ? y : z).f(a) diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index dc035bb14b9..72a1c2ec116 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -2878,3 +2878,53 @@ issue_5546_3: { } expect_stdout: "PASS" } + +issue_5666_1: { + options = { + conditionals: true, + reduce_vars: true, + unused: true, + } + input: { + var a; + (function() { + var b = a; + a ? a = b : (b++, a = b); + })(); + console.log(a); + } + expect: { + var a; + (function() { + var b = a; + a = (a ? 0 : b++, b); + })(); + console.log(a); + } + expect_stdout: "NaN" +} + +issue_5666_2: { + options = { + conditionals: true, + reduce_vars: true, + unused: true, + } + input: { + var a = "foo"; + (function() { + var b = a; + a ? (b++, a = b) : a = b; + })(); + console.log(a); + } + expect: { + var a = "foo"; + (function() { + var b = a; + a = (a ? b++ : 0, b); + })(); + console.log(a); + } + expect_stdout: "NaN" +}