Skip to content

Commit a391897

Browse files
authored
enhance conditionals (#5703)
1 parent 4a1da49 commit a391897

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/compress.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11806,8 +11806,18 @@ Compressor.prototype.compress = function(node) {
1180611806
}
1180711807
}
1180811808
// x && (y && z) ---> x && y && z
11809-
// x || (y || z) ---> x || y || z
11810-
if (self.right instanceof AST_Binary && self.operator == self.right.operator) swap_chain(self, compressor);
11809+
// w || (x, y || z) ---> w || (x, y) || z
11810+
var rhs = self.right.tail_node();
11811+
if (rhs instanceof AST_Binary && self.operator == rhs.operator) {
11812+
if (rhs !== self.right) {
11813+
var exprs = self.right.expressions.slice(0, -1);
11814+
exprs.push(rhs.left);
11815+
rhs = rhs.clone();
11816+
rhs.left = make_sequence(self.right, exprs);
11817+
self.right = rhs;
11818+
}
11819+
swap_chain(self, compressor);
11820+
}
1181111821
}
1181211822
if (compressor.option("strings") && self.operator == "+") {
1181311823
// "foo" + 42 + "" ---> "foo" + 42
@@ -13101,7 +13111,7 @@ Compressor.prototype.compress = function(node) {
1310113111
operator: "||",
1310213112
left: booleanize(condition),
1310313113
right: alternative,
13104-
});
13114+
}).optimize(compressor);
1310513115
}
1310613116
if (is_false(consequent)) {
1310713117
// c ? false : true ---> !c
@@ -13111,20 +13121,20 @@ Compressor.prototype.compress = function(node) {
1311113121
operator: "&&",
1311213122
left: booleanize(condition.negate(compressor)),
1311313123
right: alternative,
13114-
});
13124+
}).optimize(compressor);
1311513125
}
1311613126
// c ? x : true ---> !c || x
1311713127
if (is_true(alternative)) return make_node(AST_Binary, self, {
1311813128
operator: "||",
1311913129
left: booleanize(condition.negate(compressor)),
1312013130
right: consequent,
13121-
});
13131+
}).optimize(compressor);
1312213132
// c ? x : false ---> !!c && x
1312313133
if (is_false(alternative)) return make_node(AST_Binary, self, {
1312413134
operator: "&&",
1312513135
left: booleanize(condition),
1312613136
right: consequent,
13127-
});
13137+
}).optimize(compressor);
1312813138
if (compressor.option("typeofs")) mark_locally_defined(condition, consequent, alternative);
1312913139
return self;
1313013140

test/compress/transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ if_return: {
127127
if (w) {
128128
if (y) return;
129129
} else if (z) return;
130-
return x != y && (x && w(), y && z()), !0;
130+
return x != y && (x && w(), y) && z(), !0;
131131
}
132132
}
133133
}

0 commit comments

Comments
 (0)