diff --git a/lib/compress.js b/lib/compress.js index ab6629495a..6070f1ff13 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3739,8 +3739,9 @@ Compressor.prototype.compress = function(node) { function join_assigns(defn, body, keep) { var exprs = extract_exprs(body); if (!exprs) return; + keep = keep || 0; var trimmed = false; - for (var i = exprs.length - (keep || 0); --i >= 0;) { + for (var i = exprs.length - keep; --i >= 0;) { var expr = exprs[i]; if (!can_trim(expr)) continue; var tail; @@ -3762,7 +3763,6 @@ Compressor.prototype.compress = function(node) { exprs = exprs.slice(0, i).concat(expr, tail); } if (defn instanceof AST_Definitions) { - keep = keep || 0; for (var i = defn.definitions.length; --i >= 0;) { var def = defn.definitions[i]; if (!def.value) continue; @@ -11079,30 +11079,20 @@ Compressor.prototype.compress = function(node) { // void 0 !== x && null !== x ---> null != x // void 0 === x || null === x ---> null == x var lhs = self.left; - if (lhs.operator == self.operator) { - lhs = lhs.right; - } + if (lhs.operator == self.operator) lhs = lhs.right; + var expr = lhs.right; + if (expr instanceof AST_Assign && expr.operator == "=") expr = expr.left; if (lhs instanceof AST_Binary && lhs.operator == (self.operator == "&&" ? "!==" : "===") && self.right instanceof AST_Binary && lhs.operator == self.right.operator && (is_undefined(lhs.left, compressor) && self.right.left instanceof AST_Null || lhs.left instanceof AST_Null && is_undefined(self.right.left, compressor)) - && !lhs.right.has_side_effects(compressor) - && lhs.right.equivalent_to(self.right.right)) { - var combined = make_node(AST_Binary, self, { - operator: lhs.operator.slice(0, -1), - left: make_node(AST_Null, self), - right: lhs.right - }); - if (lhs !== self.left) { - combined = make_node(AST_Binary, self, { - operator: self.operator, - left: self.left.left, - right: combined - }); - } - return combined; + && !expr.has_side_effects(compressor) + && expr.equivalent_to(self.right.right)) { + lhs.operator = lhs.operator.slice(0, -1); + lhs.left = make_node(AST_Null, self); + return self.left; } break; } diff --git a/test/benchmark.js b/test/benchmark.js index 5bc7a5ce03..53d9ee2f84 100644 --- a/test/benchmark.js +++ b/test/benchmark.js @@ -17,6 +17,7 @@ var urls = [ "https://unpkg.com/mathjs@6.2.3/dist/math.js", "https://unpkg.com/react@15.3.2/dist/react.js", "https://cdnjs.cloudflare.com/ajax/libs/d3/6.7.0/d3.js", + "https://cdnjs.cloudflare.com/ajax/libs/antd/4.18.7/antd.js", "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js", "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js", "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.12.2/ember.prod.js", diff --git a/test/compress/comparisons.js b/test/compress/comparisons.js index 3a728a3a76..64c75837e5 100644 --- a/test/compress/comparisons.js +++ b/test/compress/comparisons.js @@ -493,3 +493,32 @@ issue_3413: { } expect_stdout: "PASS" } + +nullish_assign: { + options = { + comparisons: true, + } + input: { + var a; + void 0 !== (a = "PASS".split("")) && null !== a && console.log(a.join("-")); + } + expect: { + var a; + null != (a = "PASS".split("")) && console.log(a.join("-")); + } + expect_stdout: "P-A-S-S" +} + +nullish_chain: { + options = { + comparisons: true, + } + input: { + var a; + A || B || void 0 === a || null === a || C; + } + expect: { + var a; + A || B || null == a || C; + } +}