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