diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-04-21 23:30:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-22 06:30:08 +0800 |
commit | 9577c8c1b70699f8c1719117dd07df2bdeba7daa (patch) | |
tree | 2c101ecc93b2012053891cb85066ff9964219f62 | |
parent | 925a0ca1a04c5c50f80378ce3f8f3c8a6c81d8b4 (diff) | |
download | tracifyjs-9577c8c1b70699f8c1719117dd07df2bdeba7daa.tar.gz tracifyjs-9577c8c1b70699f8c1719117dd07df2bdeba7daa.zip |
fix corner case in `conditionals` (#3809)
fixes #3808
-rw-r--r-- | lib/compress.js | 24 | ||||
-rw-r--r-- | test/compress/conditionals.js | 34 |
2 files changed, 52 insertions, 6 deletions
diff --git a/lib/compress.js b/lib/compress.js index ef032b3f..d055132d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4933,9 +4933,21 @@ merge(Compressor.prototype, { }); def(AST_Sequence, function(compressor, first_in_statement) { var expressions = trim(this.expressions, compressor, first_in_statement); - if (expressions === this.expressions) return this; if (!expressions) return null; - return make_sequence(this, expressions); + var end = expressions.length - 1; + var assign, cond, lhs; + if (compressor.option("conditionals") + && end > 0 + && (assign = expressions[end - 1]) instanceof AST_Assign + && assign.operator == "=" + && (lhs = assign.left) instanceof AST_SymbolRef + && (cond = to_conditional_assignment(compressor, lhs.definition(), assign.right, expressions[end]))) { + assign = assign.clone(); + assign.right = cond; + expressions = expressions.slice(0, -2); + expressions.push(assign); + } + return expressions === this.expressions ? this : make_sequence(this, expressions); }); def(AST_Sub, function(compressor, first_in_statement) { if (this.expression.may_throw_on_access(compressor)) return this; @@ -6390,16 +6402,16 @@ merge(Compressor.prototype, { function merge_conditional_assignments() { if (!compressor.option("conditionals")) return; - for (var i = 0; i < end; i++) { - var assign = expressions[i]; + for (var i = 1; i < end; i++) { + var assign = expressions[i - 1]; if (!(assign instanceof AST_Assign)) continue; if (assign.operator != "=") continue; if (!(assign.left instanceof AST_SymbolRef)) continue; var def = assign.left.definition(); - var cond = to_conditional_assignment(compressor, def, assign.right, expressions[i + 1]); + var cond = to_conditional_assignment(compressor, def, assign.right, expressions[i]); if (!cond) continue; assign.right = cond; - expressions.splice(i + 1, 1); + expressions.splice(i, 1); end--; } } diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index 26f6b73e..ab9c4108 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -1759,3 +1759,37 @@ conditional_assignments_3: { } expect_stdout: "PASS" } + +issue_3808_1: { + options = { + conditionals: true, + side_effects: true, + } + input: { + var a; + a = "PASS", [] + "" && (a = "FAIL"); + console.log(a); + } + expect: { + var a; + a = [] + "" ? "FAIL" : "PASS"; + console.log(a); + } + expect_stdout: "PASS" +} + +issue_3808_2: { + options = { + conditionals: true, + side_effects: true, + } + input: { + var a; + console.log((a = "PASS", [] + "" && (a = "FAIL")), a); + } + expect: { + var a; + console.log((a = "PASS", [] + "" && (a = "FAIL")), a); + } + expect_stdout: " PASS" +} |