Fix invalid removal of left side in && and || compression

See #637. This does not produce the optimal result, but it does prevent the removal of non-side-effect-free code.
This commit is contained in:
Richard van Velzen
2015-02-11 21:05:49 +01:00
parent 7b71344051
commit 992b6b9fcc
2 changed files with 34 additions and 0 deletions

View File

@@ -2110,6 +2110,12 @@ merge(Compressor.prototype, {
var rr = self.right.evaluate(compressor);
if ((ll.length > 1 && !ll[1]) || (rr.length > 1 && !rr[1])) {
compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
if (self.left.has_side_effects(compressor)) {
return make_node(AST_Seq, self, {
car: self.left,
cdr: make_node(AST_False)
}).optimize(compressor);
}
return make_node(AST_False, self);
}
if (ll.length > 1 && ll[1]) {
@@ -2124,6 +2130,12 @@ merge(Compressor.prototype, {
var rr = self.right.evaluate(compressor);
if ((ll.length > 1 && ll[1]) || (rr.length > 1 && rr[1])) {
compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start);
if (self.left.has_side_effects(compressor)) {
return make_node(AST_Seq, self, {
car: self.left,
cdr: make_node(AST_True)
}).optimize(compressor);
}
return make_node(AST_True, self);
}
if (ll.length > 1 && !ll[1]) {

View File

@@ -0,0 +1,22 @@
wrongly_optimized: {
options = {
conditionals: true,
booleans: true,
evaluate: true
};
input: {
function func() {
foo();
}
if (func() || true) {
bar();
}
}
expect: {
function func() {
foo();
}
// TODO: optimize to `func(), bar()`
(func(), 0) || bar();
}
}