@@ -2658,24 +2658,58 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
// y?true:false --> !!y
|
||||
if (is_true(consequent) && is_false(alternative)) {
|
||||
if (self.condition.is_boolean()) {
|
||||
// boolean_expression ? true : false --> boolean_expression
|
||||
return self.condition;
|
||||
if (is_true(self.consequent)) {
|
||||
if (is_false(self.alternative)) {
|
||||
// c ? true : false ---> !!c
|
||||
return booleanize(self.condition);
|
||||
}
|
||||
self.condition = self.condition.negate(compressor);
|
||||
return make_node(AST_UnaryPrefix, self.condition, {
|
||||
operator: "!",
|
||||
expression: self.condition
|
||||
// c ? true : x ---> !!c || x
|
||||
return make_node(AST_Binary, self, {
|
||||
operator: "||",
|
||||
left: booleanize(self.condition),
|
||||
right: self.alternative
|
||||
});
|
||||
}
|
||||
// y?false:true --> !y
|
||||
if (is_false(consequent) && is_true(alternative)) {
|
||||
return self.condition.negate(compressor)
|
||||
if (is_false(self.consequent)) {
|
||||
if (is_true(self.alternative)) {
|
||||
// c ? false : true ---> !c
|
||||
return booleanize(self.condition.negate(compressor));
|
||||
}
|
||||
// c ? false : x ---> !c && x
|
||||
return make_node(AST_Binary, self, {
|
||||
operator: "&&",
|
||||
left: booleanize(self.condition.negate(compressor)),
|
||||
right: self.alternative
|
||||
});
|
||||
}
|
||||
if (is_true(self.alternative)) {
|
||||
// c ? x : true ---> !c || x
|
||||
return make_node(AST_Binary, self, {
|
||||
operator: "||",
|
||||
left: booleanize(self.condition.negate(compressor)),
|
||||
right: self.consequent
|
||||
});
|
||||
}
|
||||
if (is_false(self.alternative)) {
|
||||
// c ? x : false ---> !!c && x
|
||||
return make_node(AST_Binary, self, {
|
||||
operator: "&&",
|
||||
left: booleanize(self.condition),
|
||||
right: self.consequent
|
||||
});
|
||||
}
|
||||
|
||||
return self;
|
||||
|
||||
function booleanize(node) {
|
||||
if (node.is_boolean()) return node;
|
||||
// !!expression
|
||||
return make_node(AST_UnaryPrefix, node, {
|
||||
operator: "!",
|
||||
expression: node.negate(compressor)
|
||||
});
|
||||
}
|
||||
|
||||
// AST_True or !0
|
||||
function is_true(node) {
|
||||
return node instanceof AST_True
|
||||
|
||||
Reference in New Issue
Block a user