#873 Fix conditionals optimizations with default compress options
This commit is contained in:
@@ -726,6 +726,32 @@ merge(Compressor.prototype, {
|
||||
return [ this ];
|
||||
}
|
||||
});
|
||||
AST_Node.DEFMETHOD("is_constant", function(compressor){
|
||||
// Accomodate when compress option evaluate=false
|
||||
// as well as the common constant expressions !0 and !1
|
||||
return this instanceof AST_Constant
|
||||
|| (this instanceof AST_UnaryPrefix && this.operator == "!"
|
||||
&& this.expression instanceof AST_Constant)
|
||||
|| this.evaluate(compressor).length > 1;
|
||||
});
|
||||
// Obtain the constant value of an expression already known to be constant.
|
||||
// Result only valid iff this.is_constant(compressor) is true.
|
||||
AST_Node.DEFMETHOD("constant_value", function(compressor){
|
||||
// Accomodate when option evaluate=false.
|
||||
if (this instanceof AST_Constant) return this.value;
|
||||
// Accomodate the common constant expressions !0 and !1 when option evaluate=false.
|
||||
if (this instanceof AST_UnaryPrefix
|
||||
&& this.operator == "!"
|
||||
&& this.expression instanceof AST_Constant) {
|
||||
return !this.expression.value;
|
||||
}
|
||||
var result = this.evaluate(compressor)
|
||||
if (result.length > 1) {
|
||||
return result[1];
|
||||
}
|
||||
// should never be reached
|
||||
return undefined;
|
||||
});
|
||||
def(AST_Statement, function(){
|
||||
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
|
||||
});
|
||||
@@ -2427,32 +2453,48 @@ merge(Compressor.prototype, {
|
||||
alternative: alternative
|
||||
});
|
||||
}
|
||||
// x=y?1:1 --> x=1
|
||||
if (consequent instanceof AST_Constant
|
||||
&& alternative instanceof AST_Constant
|
||||
// y?1:1 --> 1
|
||||
if (consequent.is_constant(compressor)
|
||||
&& alternative.is_constant(compressor)
|
||||
&& consequent.equivalent_to(alternative)) {
|
||||
var consequent_value = consequent.constant_value();
|
||||
if (self.condition.has_side_effects(compressor)) {
|
||||
return AST_Seq.from_array([self.condition, make_node_from_constant(compressor, consequent.value, self)]);
|
||||
return AST_Seq.from_array([self.condition, make_node_from_constant(compressor, consequent_value, self)]);
|
||||
} else {
|
||||
return make_node_from_constant(compressor, consequent.value, self);
|
||||
|
||||
return make_node_from_constant(compressor, consequent_value, self);
|
||||
}
|
||||
}
|
||||
// x=y?true:false --> x=!!y
|
||||
if (consequent instanceof AST_True
|
||||
&& alternative instanceof AST_False) {
|
||||
|
||||
// y?true:false --> !!y
|
||||
if (is_true(consequent) && is_false(alternative)) {
|
||||
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) {
|
||||
// y?false:true --> !y
|
||||
if (is_false(consequent) && is_true(alternative)) {
|
||||
return self.condition.negate(compressor)
|
||||
}
|
||||
return self;
|
||||
|
||||
// AST_True or !0
|
||||
function is_true(node) {
|
||||
return node instanceof AST_True
|
||||
|| (node instanceof AST_UnaryPrefix
|
||||
&& node.operator == "!"
|
||||
&& node.expression instanceof AST_Constant
|
||||
&& !node.expression.value);
|
||||
}
|
||||
// AST_False or !1
|
||||
function is_false(node) {
|
||||
return node instanceof AST_False
|
||||
|| (node instanceof AST_UnaryPrefix
|
||||
&& node.operator == "!"
|
||||
&& node.expression instanceof AST_Constant
|
||||
&& !!node.expression.value);
|
||||
}
|
||||
});
|
||||
|
||||
OPT(AST_Boolean, function(self, compressor){
|
||||
|
||||
Reference in New Issue
Block a user