enhance side_effects (#3383)

This commit is contained in:
Alex Lam S.L
2019-04-25 04:14:21 +08:00
committed by GitHub
parent c56d89f804
commit a206964c0a
4 changed files with 36 additions and 14 deletions

View File

@@ -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;

View File

@@ -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"
}

View File

@@ -1,4 +1,3 @@
issue_1639_1: {
options = {
booleans: true,
@@ -12,7 +11,6 @@ issue_1639_1: {
}
input: {
var a = 100, b = 10;
var L1 = 5;
while (--L1 > 0) {
if ((--b), false) {
@@ -21,7 +19,6 @@ issue_1639_1: {
}
}
}
console.log(a, b);
}
expect: {
@@ -29,7 +26,7 @@ issue_1639_1: {
if (--b, 0) var ignore = 0;
console.log(a, b);
}
expect_stdout: true
expect_stdout: "100 6"
}
issue_1639_2: {
@@ -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: {
@@ -84,5 +79,5 @@ issue_1639_3: {
a++,
console.log(a, b);
}
expect_stdout: true
expect_stdout: "101 10"
}

View File

@@ -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--;
}
}