Fix compression of conditionals

Don't move the condition on the right side of an assignment when
the left side may have side effects.

Fix #677
This commit is contained in:
Mihai Bazon
2015-04-13 17:29:48 +03:00
parent e04ef56243
commit 18c63ff3d8
2 changed files with 13 additions and 2 deletions

View File

@@ -2343,6 +2343,7 @@ merge(Compressor.prototype, {
&& alternative instanceof AST_Assign && alternative instanceof AST_Assign
&& consequent.operator == alternative.operator && consequent.operator == alternative.operator
&& consequent.left.equivalent_to(alternative.left) && consequent.left.equivalent_to(alternative.left)
&& !consequent.left.has_side_effects(compressor)
) { ) {
/* /*
* Stuff like this: * Stuff like this:

View File

@@ -86,7 +86,9 @@ ifs_4: {
x(foo)[10].bar.baz = something_else(); x(foo)[10].bar.baz = something_else();
} }
expect: { expect: {
x(foo)[10].bar.baz = (foo && bar) ? something() : something_else(); foo && bar
? x(foo)[10].bar.baz = something()
: x(foo)[10].bar.baz = something_else();
} }
} }
@@ -133,6 +135,7 @@ ifs_6: {
comparisons: true comparisons: true
}; };
input: { input: {
var x;
if (!foo && !bar && !baz && !boo) { if (!foo && !bar && !baz && !boo) {
x = 10; x = 10;
} else { } else {
@@ -140,6 +143,7 @@ ifs_6: {
} }
} }
expect: { expect: {
var x;
x = foo || bar || baz || boo ? 20 : 10; x = foo || bar || baz || boo ? 20 : 10;
} }
} }
@@ -165,6 +169,7 @@ cond_2: {
conditionals: true conditionals: true
}; };
input: { input: {
var x;
if (some_condition()) { if (some_condition()) {
x = new FooBar(1); x = new FooBar(1);
} else { } else {
@@ -172,6 +177,7 @@ cond_2: {
} }
} }
expect: { expect: {
var x;
x = new FooBar(some_condition() ? 1 : 2); x = new FooBar(some_condition() ? 1 : 2);
} }
} }
@@ -303,6 +309,7 @@ cond_7_1: {
evaluate : true evaluate : true
}; };
input: { input: {
var x;
// access to global should be assumed to have side effects // access to global should be assumed to have side effects
if (y) { if (y) {
x = 1+1; x = 1+1;
@@ -311,6 +318,7 @@ cond_7_1: {
} }
} }
expect: { expect: {
var x;
x = (y, 2); x = (y, 2);
} }
} }
@@ -321,6 +329,7 @@ cond_8: {
evaluate : true evaluate : true
}; };
input: { input: {
var a;
// compress these // compress these
a = condition ? true : false; a = condition ? true : false;
@@ -355,6 +364,7 @@ cond_8: {
} }
expect: { expect: {
var a;
a = !!condition; a = !!condition;
a = !condition; a = !condition;
a = !!condition(); a = !!condition();
@@ -367,4 +377,4 @@ cond_8: {
a = condition ? 0 : true; a = condition ? 0 : true;
a = condition ? 1 : 0; a = condition ? 1 : 0;
} }
} }