Skip to content

Commit

Permalink
enhance comparisons (#5358)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Feb 18, 2022
1 parent 82e8ebd commit 9686379
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
30 changes: 10 additions & 20 deletions lib/compress.js
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions test/benchmark.js
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions test/compress/comparisons.js
Expand Up @@ -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;
}
}

0 comments on commit 9686379

Please sign in to comment.