Optimize conditionals where the consequent and alternative are both booleans and not equivalent

This commit is contained in:
Tal Ater 2014-09-13 19:47:10 +03:00
parent c662ff0eb5
commit cd44892301
2 changed files with 57 additions and 0 deletions

View File

@ -2341,6 +2341,20 @@ merge(Compressor.prototype, {
right: alternative right: alternative
}); });
} }
// x=y?true:false --> x=!!y
if (consequent instanceof AST_True
&& alternative instanceof AST_False) {
self.condition = self.condition.negate(compressor);
return make_node(AST_UnaryPrefix, self.condition, {
operator: "!",
expression: self.condition
});
}
// x=y?false:true --> x=!y
if (consequent instanceof AST_False
&& alternative instanceof AST_True) {
return self.condition.negate(compressor)
}
return self; return self;
}); });

View File

@ -327,4 +327,47 @@ cond_8: {
a = condition() ? condition() : b; a = condition() ? condition() : b;
a = condition() ? condition() : b; a = condition() ? condition() : b;
} }
}
cond_9: {
options = {
conditionals: true,
evaluate : true
};
input: {
// compress these
a = condition ? true : false;
a = !condition ? true : false;
a = condition() ? true : false;
if (condition) {
a = true;
} else {
a = false;
}
a = condition ? false : true;
a = !condition ? false : true;
a = condition() ? false : true;
if (condition) {
a = false;
} else {
a = true;
}
}
expect: {
a = !!condition;
a = !condition;
a = !!condition();
a = !!condition;
a = !condition;
a = !!condition;
a = !condition();
a = !condition;
}
} }