From e4bff315eb6e8f57b9e0f6dddf81f7e0cb978708 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 17 Sep 2022 03:42:32 +0100 Subject: [PATCH] fix corner case in `conditionals` (#5667) fixes #5666 --- lib/compress.js | 24 ++++++++--------- test/compress/conditionals.js | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 13 deletions(-) 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" +}