minor cleanups (#5549)

This commit is contained in:
Alex Lam S.L
2022-07-07 20:14:54 +01:00
committed by GitHub
parent b92a89f325
commit 80787ff7ef

View File

@@ -4766,9 +4766,9 @@ Compressor.prototype.compress = function(node) {
function best_of_statement(ast1, ast2, threshold) {
return best_of_expression(make_node(AST_SimpleStatement, ast1, {
body: ast1
body: ast1,
}), make_node(AST_SimpleStatement, ast2, {
body: ast2
body: ast2,
}), threshold).body;
}
@@ -5457,17 +5457,14 @@ Compressor.prototype.compress = function(node) {
function basic_negation(exp) {
return make_node(AST_UnaryPrefix, exp, {
operator: "!",
expression: exp
expression: exp,
});
}
function best(orig, alt, first_in_statement) {
var negated = basic_negation(orig);
if (first_in_statement) {
var stat = make_node(AST_SimpleStatement, alt, {
body: alt
});
return best_of_expression(negated, stat) === stat ? alt : negated;
}
if (first_in_statement) return best_of_expression(negated, make_node(AST_SimpleStatement, alt, {
body: alt,
})) === negated ? negated : alt;
return best_of_expression(negated, alt);
}
def(AST_Node, function() {
@@ -11229,7 +11226,8 @@ Compressor.prototype.compress = function(node) {
// !!foo ---> foo, if we're in boolean context
if (exp instanceof AST_UnaryPrefix && exp.operator == "!") return exp.expression;
if (exp instanceof AST_Binary) {
self = best_of(compressor, self, exp.negate(compressor, first_in_statement(compressor)));
var first = first_in_statement(compressor);
self = (first ? best_of_statement : best_of_expression)(self, exp.negate(compressor, first));
}
}
break;
@@ -11593,12 +11591,12 @@ Compressor.prototype.compress = function(node) {
break;
}
if (compressor.option("comparisons") && self.is_boolean(compressor)) {
if (!(parent instanceof AST_Binary) || parent instanceof AST_Assign) {
var negated = best_of(compressor, self, make_node(AST_UnaryPrefix, self, {
if (parent.TYPE != "Binary") {
var negated = make_node(AST_UnaryPrefix, self, {
operator: "!",
expression: self.negate(compressor, first_in_statement(compressor))
}));
if (negated !== self) return negated;
expression: self.negate(compressor),
});
if (best_of(compressor, self, negated) === negated) return negated;
}
switch (self.operator) {
case ">": reverse("<"); break;
@@ -11842,17 +11840,11 @@ Compressor.prototype.compress = function(node) {
&& !(self.left instanceof AST_Binary
&& self.left.operator != self.operator
&& PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
var reversed = make_node(AST_Binary, self, {
self = best_of(compressor, self, make_node(AST_Binary, self, {
operator: self.operator,
left: self.right,
right: self.left
});
if (self.right instanceof AST_Constant
&& !(self.left instanceof AST_Constant)) {
self = best_of(compressor, reversed, self);
} else {
self = best_of(compressor, self, reversed);
}
right: self.left,
}), self.right instanceof AST_Constant && !(self.left instanceof AST_Constant));
}
if (!associative || !self.is_number(compressor)) break;
// a + (b + c) ---> (a + b) + c
@@ -12377,7 +12369,6 @@ Compressor.prototype.compress = function(node) {
left: make_node(AST_Template, self, {
expressions: exprs.slice(0, -1),
strings: strs.slice(0, -1),
tag: tag,
}).transform(compressor),
right: exprs[exprs.length - 1],
}).optimize(compressor);
@@ -12400,7 +12391,6 @@ Compressor.prototype.compress = function(node) {
right: make_node(AST_Template, self, {
expressions: exprs.slice(i),
strings: strs.slice(i),
tag: tag,
}).transform(compressor),
}).optimize(compressor));
}
@@ -12697,12 +12687,13 @@ Compressor.prototype.compress = function(node) {
AST_Node.warn("Condition always true [{file}:{line},{col}]", self.start);
return make_sequence(self, [ self.condition, self.consequent ]).optimize(compressor);
}
var negated = condition.negate(compressor, first_in_statement(compressor));
if (best_of(compressor, condition, negated) === negated) {
var first = first_in_statement(compressor);
var negated = condition.negate(compressor, first);
if ((first ? best_of_statement : best_of_expression)(condition, negated) === negated) {
self = make_node(AST_Conditional, self, {
condition: negated,
consequent: self.alternative,
alternative: self.consequent
alternative: self.consequent,
});
negated = condition;
condition = self.condition;