aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-12-01 06:18:31 +0800
committerGitHub <noreply@github.com>2017-12-01 06:18:31 +0800
commit5a1e99d713fd0ca4ca4a012422a767c138a75606 (patch)
tree6eb8b3981cb445c5f12bd58e268473672efe93fb /lib
parentb762f2d6f4e81dcbd49ffb4db4b1933953942999 (diff)
downloadtracifyjs-5a1e99d713fd0ca4ca4a012422a767c138a75606.tar.gz
tracifyjs-5a1e99d713fd0ca4ca4a012422a767c138a75606.zip
improve compression of `if` conditions (#2544)
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js44
1 files changed, 25 insertions, 19 deletions
diff --git a/lib/compress.js b/lib/compress.js
index e6215f66..395c4ed0 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3147,28 +3147,34 @@ merge(Compressor.prototype, {
// “has no side effects”; also it doesn't work for cases like
// `x && true`, though it probably should.
var cond = self.condition.evaluate(compressor);
- if (cond !== self.condition) {
- if (cond) {
- compressor.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
- if (compressor.option("dead_code")) {
- var a = [];
- if (self.alternative) {
- extract_declarations_from_unreachable_code(compressor, self.alternative, a);
- }
- a.push(self.body);
- return make_node(AST_BlockStatement, self, { body: a }).optimize(compressor);
- }
- } else {
+ if (!compressor.option("dead_code") && !(cond instanceof AST_Node)) {
+ var orig = self.condition;
+ self.condition = make_node_from_constant(cond, orig);
+ self.condition = best_of_expression(self.condition.transform(compressor), orig);
+ }
+ if (compressor.option("dead_code")) {
+ if (cond instanceof AST_Node) cond = self.condition.tail_node().evaluate(compressor);
+ if (!cond) {
compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
- if (compressor.option("dead_code")) {
- var a = [];
- extract_declarations_from_unreachable_code(compressor, self.body, a);
- if (self.alternative) a.push(self.alternative);
- return make_node(AST_BlockStatement, self, { body: a }).optimize(compressor);
+ var body = [];
+ extract_declarations_from_unreachable_code(compressor, self.body, body);
+ body.push(make_node(AST_SimpleStatement, self.condition, {
+ body: self.condition
+ }));
+ 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 self_condition_length = self.condition.print_to_string().length;