improve compression of if conditions (#2544)
This commit is contained in:
@@ -3147,28 +3147,34 @@ merge(Compressor.prototype, {
|
|||||||
// “has no side effects”; also it doesn't work for cases like
|
// “has no side effects”; also it doesn't work for cases like
|
||||||
// `x && true`, though it probably should.
|
// `x && true`, though it probably should.
|
||||||
var cond = self.condition.evaluate(compressor);
|
var cond = self.condition.evaluate(compressor);
|
||||||
if (cond !== self.condition) {
|
if (!compressor.option("dead_code") && !(cond instanceof AST_Node)) {
|
||||||
if (cond) {
|
var orig = self.condition;
|
||||||
compressor.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
|
self.condition = make_node_from_constant(cond, orig);
|
||||||
|
self.condition = best_of_expression(self.condition.transform(compressor), orig);
|
||||||
|
}
|
||||||
if (compressor.option("dead_code")) {
|
if (compressor.option("dead_code")) {
|
||||||
var a = [];
|
if (cond instanceof AST_Node) cond = self.condition.tail_node().evaluate(compressor);
|
||||||
if (self.alternative) {
|
if (!cond) {
|
||||||
extract_declarations_from_unreachable_code(compressor, self.alternative, a);
|
|
||||||
}
|
|
||||||
a.push(self.body);
|
|
||||||
return make_node(AST_BlockStatement, self, { body: a }).optimize(compressor);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
|
compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
|
||||||
if (compressor.option("dead_code")) {
|
var body = [];
|
||||||
var a = [];
|
extract_declarations_from_unreachable_code(compressor, self.body, body);
|
||||||
extract_declarations_from_unreachable_code(compressor, self.body, a);
|
body.push(make_node(AST_SimpleStatement, self.condition, {
|
||||||
if (self.alternative) a.push(self.alternative);
|
body: self.condition
|
||||||
return make_node(AST_BlockStatement, self, { body: a }).optimize(compressor);
|
}));
|
||||||
|
if (self.alternative) body.push(self.alternative);
|
||||||
|
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
|
||||||
|
} else if (!(cond instanceof AST_Node)) {
|
||||||
|
compressor.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
|
||||||
|
var body = [];
|
||||||
|
if (self.alternative) {
|
||||||
|
extract_declarations_from_unreachable_code(compressor, self.alternative, body);
|
||||||
}
|
}
|
||||||
|
body.push(make_node(AST_SimpleStatement, self.condition, {
|
||||||
|
body: self.condition
|
||||||
|
}));
|
||||||
|
body.push(self.body);
|
||||||
|
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
|
||||||
}
|
}
|
||||||
cond = make_node_from_constant(cond, self.condition).transform(compressor);
|
|
||||||
self.condition = best_of_expression(cond, self.condition);
|
|
||||||
}
|
}
|
||||||
var negated = self.condition.negate(compressor);
|
var negated = self.condition.negate(compressor);
|
||||||
var self_condition_length = self.condition.print_to_string().length;
|
var self_condition_length = self.condition.print_to_string().length;
|
||||||
|
|||||||
@@ -1015,3 +1015,32 @@ delete_conditional_2: {
|
|||||||
}
|
}
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_2535: {
|
||||||
|
options = {
|
||||||
|
booleans: true,
|
||||||
|
conditionals: true,
|
||||||
|
evaluate: true,
|
||||||
|
passes: 2,
|
||||||
|
side_effects: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
if (true || x()) y();
|
||||||
|
if (true && x()) y();
|
||||||
|
if (x() || true) y();
|
||||||
|
if (x() && true) y();
|
||||||
|
if (false || x()) y();
|
||||||
|
if (false && x()) y();
|
||||||
|
if (x() || false) y();
|
||||||
|
if (x() && false) y();
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
y();
|
||||||
|
x() && y();
|
||||||
|
(x(), 0) || y();
|
||||||
|
x() && y();
|
||||||
|
x() && y();
|
||||||
|
x() && y();
|
||||||
|
(x(), 1) || y();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -178,6 +178,8 @@ try_catch_finally: {
|
|||||||
conditionals: true,
|
conditionals: true,
|
||||||
dead_code: true,
|
dead_code: true,
|
||||||
evaluate: true,
|
evaluate: true,
|
||||||
|
passes: 2,
|
||||||
|
side_effects: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
var a = 1;
|
var a = 1;
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ issue_485_crashing_1530: {
|
|||||||
dead_code: true,
|
dead_code: true,
|
||||||
evaluate: true,
|
evaluate: true,
|
||||||
inline: true,
|
inline: true,
|
||||||
|
side_effects: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
(function(a) {
|
(function(a) {
|
||||||
@@ -94,9 +95,7 @@ issue_485_crashing_1530: {
|
|||||||
var b = 42;
|
var b = 42;
|
||||||
})(this);
|
})(this);
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {}
|
||||||
this, void 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
issue_1841_1: {
|
issue_1841_1: {
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ issue_2167: {
|
|||||||
global_defs: {
|
global_defs: {
|
||||||
"@isDevMode": "function(){}",
|
"@isDevMode": "function(){}",
|
||||||
},
|
},
|
||||||
|
passes: 2,
|
||||||
side_effects: true,
|
side_effects: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ label_if_break: {
|
|||||||
conditionals: true,
|
conditionals: true,
|
||||||
dead_code: true,
|
dead_code: true,
|
||||||
evaluate: true,
|
evaluate: true,
|
||||||
|
side_effects: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
L: if (true) {
|
L: if (true) {
|
||||||
|
|||||||
Reference in New Issue
Block a user