Skip to content

Commit 7270671

Browse files
authoredOct 28, 2022
fix corner case in conditionals (#5723)
fixes #5722
1 parent 30bf068 commit 7270671

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed
 

‎lib/compress.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -11634,10 +11634,14 @@ Compressor.prototype.compress = function(node) {
1163411634
});
1163511635
self.right = rhs.right;
1163611636
if (compressor) {
11637-
self.left = self.left.transform(compressor);
11638-
} else if (self.operator == rhs.left.operator) {
11639-
swap_chain(self.left);
11637+
var left = self.left.transform(compressor);
11638+
if (left !== self.left) {
11639+
self = self.clone();
11640+
self.left = left;
11641+
}
11642+
return self;
1164011643
}
11644+
if (self.operator == rhs.left.operator) swap_chain(self.left);
1164111645
}
1164211646

1164311647
OPT(AST_Binary, function(self, compressor) {
@@ -11825,7 +11829,7 @@ Compressor.prototype.compress = function(node) {
1182511829
// x && (y && z) ---> x && y && z
1182611830
// w || (x, y || z) ---> w || (x, y) || z
1182711831
var rhs = self.right.tail_node();
11828-
if (rhs instanceof AST_Binary && self.operator == rhs.operator) swap_chain(self, compressor);
11832+
if (rhs instanceof AST_Binary && self.operator == rhs.operator) self = swap_chain(self, compressor);
1182911833
}
1183011834
if (compressor.option("strings") && self.operator == "+") {
1183111835
// "foo" + 42 + "" ---> "foo" + 42
@@ -11858,7 +11862,7 @@ Compressor.prototype.compress = function(node) {
1185811862
&& (self.left.is_string(compressor) && rhs.is_string(compressor)
1185911863
|| rhs.left.is_string(compressor)
1186011864
&& (self.left.is_constant() || !rhs.right.has_side_effects(compressor)))) {
11861-
swap_chain(self, compressor);
11865+
self = swap_chain(self, compressor);
1186211866
}
1186311867
}
1186411868
if (compressor.option("evaluate")) {

‎test/compress/conditionals.js

+20
Original file line numberDiff line numberDiff line change
@@ -3068,3 +3068,23 @@ issue_5712: {
30683068
}
30693069
expect_stdout: "PASS"
30703070
}
3071+
3072+
issue_5722: {
3073+
options = {
3074+
conditionals: true,
3075+
evaluate: true,
3076+
keep_fnames: true,
3077+
side_effects: true,
3078+
}
3079+
input: {
3080+
var a = true;
3081+
a && function f() {
3082+
return 42;
3083+
}(a++) ? null + (console.log("PASS") && a++) : "";
3084+
}
3085+
expect: {
3086+
var a = true;
3087+
a && (void a++, console.log("PASS")) && a++;
3088+
}
3089+
expect_stdout: "PASS"
3090+
}

0 commit comments

Comments
 (0)
Please sign in to comment.