From 20be5209c08f95802198c7a5d3acd0767fcd4e70 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 3 Mar 2021 19:30:07 +0000 Subject: fix corner cases in `ie8` & `side_effects` (#4731) fixes #4729 fixes #4730 --- lib/compress.js | 18 ++++++++++++------ test/compress/ie8.js | 25 +++++++++++++++++++++++++ test/compress/side_effects.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 99e67b91..ed048593 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -7226,30 +7226,36 @@ merge(Compressor.prototype, { } var rhs = right.drop_side_effect_free(compressor, first_in_statement); if (!rhs) return left.drop_side_effect_free(compressor, first_in_statement); - if (lazy_op[op] && !(rhs instanceof AST_Function)) { + if (lazy_op[op] && rhs.has_side_effects(compressor)) { var node = this; if (op == "&&" && rhs instanceof AST_PropAccess && left.equivalent_to(rhs.expression) && !left.has_side_effects(compressor)) { - var prop = rhs instanceof AST_Sub && rhs.property.drop_side_effect_free(compressor); + var prop = rhs instanceof AST_Sub + && rhs.property.drop_side_effect_free(compressor, first_in_statement); if (!prop) return left.drop_side_effect_free(compressor, first_in_statement); node = node.clone(); node.right = prop; - } else if (rhs !== right) { + return node.drop_side_effect_free(compressor, first_in_statement); + } + if (rhs !== right) { node = node.clone(); node.right = rhs.drop_side_effect_free(compressor); } if (op == "??") return node; - return (first_in_statement ? best_of_statement : best_of_expression)(node, make_node(AST_Binary, this, { + var negated = make_node(AST_Binary, this, { operator: op == "&&" ? "||" : "&&", left: left.negate(compressor, first_in_statement), right: node.right, - })); + }); + return first_in_statement ? best_of_statement(node, negated) : best_of_expression(node, negated); } else { var lhs = left.drop_side_effect_free(compressor, first_in_statement); if (!lhs) return rhs; - return make_sequence(this, [ lhs, rhs.drop_side_effect_free(compressor) ]); + rhs = rhs.drop_side_effect_free(compressor); + if (!rhs) return lhs; + return make_sequence(this, [ lhs, rhs ]); } }); def(AST_Call, function(compressor, first_in_statement) { diff --git a/test/compress/ie8.js b/test/compress/ie8.js index ed3950a0..7e49672f 100644 --- a/test/compress/ie8.js +++ b/test/compress/ie8.js @@ -2919,3 +2919,28 @@ issue_4568: { } expect_stdout: "undefined 1" } + +issue_4729: { + options = { + ie8: true, + toplevel: true, + unused: true, + } + input: { + try { + f; + } catch(e) { + var a = a && a[function f() {}]; + console.log("PASS"); + } + } + expect: { + try { + f; + } catch(e) { + (function f() {}); + console.log("PASS"); + } + } + expect_stdout: "PASS" +} diff --git a/test/compress/side_effects.js b/test/compress/side_effects.js index fd6cabc6..03298274 100644 --- a/test/compress/side_effects.js +++ b/test/compress/side_effects.js @@ -558,3 +558,33 @@ drop_side_effect_free_call: { } expect_stdout: "PASS" } + +issue_4730_1: { + options = { + side_effects: true, + } + input: { + var a; + console.log("PASS") + (a && a[a.p]); + } + expect: { + var a; + console.log("PASS"); + } + expect_stdout: "PASS" +} + +issue_4730_2: { + options = { + side_effects: true, + } + input: { + var a; + !console.log("PASS") || a && a[a.p]; + } + expect: { + var a; + console.log("PASS"); + } + expect_stdout: "PASS" +} -- cgit v1.2.3