Skip to content

Commit

Permalink
enhance side_effects (#3383)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Apr 24, 2019
1 parent c56d89f commit a206964
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
16 changes: 12 additions & 4 deletions lib/compress.js
Expand Up @@ -4172,10 +4172,18 @@ merge(Compressor.prototype, {
var right = this.right.drop_side_effect_free(compressor, first_in_statement);
if (!right) return this.left.drop_side_effect_free(compressor, first_in_statement);
if (lazy_op[this.operator]) {
if (right === this.right) return this;
var node = this.clone();
node.right = right.drop_side_effect_free(compressor);
return node;
var node;
if (right === this.right) {
node = this;
} else {
node = this.clone();
node.right = right.drop_side_effect_free(compressor);
}
return (first_in_statement ? best_of_statement : best_of_expression)(node, make_node(AST_Binary, this, {
operator: node.operator == "&&" ? "||" : "&&",
left: node.left.negate(compressor, first_in_statement),
right: node.right
}));
} else {
var left = this.left.drop_side_effect_free(compressor, first_in_statement);
if (!left) return right;
Expand Down
19 changes: 19 additions & 0 deletions test/compress/conditionals.js
Expand Up @@ -1416,3 +1416,22 @@ issue_3271: {
}
expect_stdout: "1 1"
}

iife_condition: {
options = {
conditionals: true,
side_effects: true,
}
input: {
if (function() {
return console;
}())
console.log("PASS");
}
expect: {
!function() {
return console;
}() || console.log("PASS");
}
expect_stdout: "PASS"
}
13 changes: 4 additions & 9 deletions test/compress/issue-1639.js
@@ -1,4 +1,3 @@

issue_1639_1: {
options = {
booleans: true,
Expand All @@ -12,7 +11,6 @@ issue_1639_1: {
}
input: {
var a = 100, b = 10;

var L1 = 5;
while (--L1 > 0) {
if ((--b), false) {
Expand All @@ -21,15 +19,14 @@ issue_1639_1: {
}
}
}

console.log(a, b);
}
expect: {
for (var a = 100, b = 10, L1 = 5; --L1 > 0;)
if (--b, 0) var ignore = 0;
console.log(a, b);
}
expect_stdout: true
expect_stdout: "100 6"
}

issue_1639_2: {
Expand All @@ -44,25 +41,23 @@ issue_1639_2: {
}
input: {
var a = 100, b = 10;

function f19() {
if (++a, false)
if (a)
if (++a);
}
f19();

console.log(a, b);
}
expect: {
var a = 100, b = 10;
function f19() {
++a, 0;
++a, 1;
}
f19(),
console.log(a, b);
}
expect_stdout: true
expect_stdout: "101 10"
}

issue_1639_3: {
Expand All @@ -84,5 +79,5 @@ issue_1639_3: {
a++,
console.log(a, b);
}
expect_stdout: true
expect_stdout: "101 10"
}
2 changes: 1 addition & 1 deletion test/compress/sequences.js
Expand Up @@ -702,7 +702,7 @@ side_effects_cascade_3: {
}
expect: {
function f(a, b) {
!(b += a) && ((b = a) || (b -= a, b ^= a)),
(b += a) || (b = a) || (b -= a, b ^= a),
a--;
}
}
Expand Down

0 comments on commit a206964

Please sign in to comment.