diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-10-09 23:45:41 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-09 23:45:41 +0800 |
commit | 6db880e16db4ce5354cea75907b96a9ba37d4363 (patch) | |
tree | 07714259e7abb056022cc940f9202b26480d656f /lib | |
parent | a82003d6ac33cdfa3f9ffeadb7809bd850c958ff (diff) | |
download | tracifyjs-6db880e16db4ce5354cea75907b96a9ba37d4363.tar.gz tracifyjs-6db880e16db4ce5354cea75907b96a9ba37d4363.zip |
clean up AST_Binary optimisation logic (#3458)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/lib/compress.js b/lib/compress.js index bffb6f74..29ef07c3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5696,7 +5696,8 @@ merge(Compressor.prototype, { } break; } - if (compressor.option("booleans") && compressor.in_boolean_context()) switch (self.operator) { + var in_bool = compressor.option("booleans") && compressor.in_boolean_context(); + if (in_bool) switch (self.operator) { case "+": var ll = self.left.evaluate(compressor); var rr = self.right.evaluate(compressor); @@ -5729,9 +5730,9 @@ merge(Compressor.prototype, { } break; } + var parent = compressor.parent(); if (compressor.option("comparisons") && self.is_boolean(compressor)) { - if (!(compressor.parent() instanceof AST_Binary) - || compressor.parent() instanceof AST_Assign) { + if (!(parent instanceof AST_Binary) || parent instanceof AST_Assign) { var negated = make_node(AST_UnaryPrefix, self, { operator: "!", expression: self.negate(compressor, first_in_statement(compressor)) @@ -5769,14 +5770,14 @@ merge(Compressor.prototype, { var ll = fuzzy_eval(self.left); if (!ll) { AST_Node.warn("Condition left of && always false [{file}:{line},{col}]", self.start); - return maintain_this_binding(compressor, compressor.parent(), compressor.self(), self.left).optimize(compressor); + return maintain_this_binding(compressor, parent, compressor.self(), self.left).optimize(compressor); } else if (!(ll instanceof AST_Node)) { AST_Node.warn("Condition left of && always true [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, self.right ]).optimize(compressor); } var rr = self.right.evaluate(compressor); if (!rr) { - if (compressor.option("booleans") && compressor.in_boolean_context()) { + if (in_bool) { AST_Node.warn("Boolean && always false [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, @@ -5784,9 +5785,7 @@ merge(Compressor.prototype, { ]).optimize(compressor); } else self.falsy = true; } else if (!(rr instanceof AST_Node)) { - var parent = compressor.parent(); - if (parent.operator == "&&" && parent.left === compressor.self() - || compressor.option("booleans") && compressor.in_boolean_context()) { + if (in_bool || parent.operator == "&&" && parent.left === compressor.self()) { AST_Node.warn("Dropping side-effect-free && [{file}:{line},{col}]", self.start); return self.left.optimize(compressor); } @@ -5808,18 +5807,16 @@ merge(Compressor.prototype, { return make_sequence(self, [ self.left, self.right ]).optimize(compressor); } else if (!(ll instanceof AST_Node)) { AST_Node.warn("Condition left of || always true [{file}:{line},{col}]", self.start); - return maintain_this_binding(compressor, compressor.parent(), compressor.self(), self.left).optimize(compressor); + return maintain_this_binding(compressor, parent, compressor.self(), self.left).optimize(compressor); } var rr = self.right.evaluate(compressor); if (!rr) { - var parent = compressor.parent(); - if (parent.operator == "||" && parent.left === compressor.self() - || compressor.option("booleans") && compressor.in_boolean_context()) { + if (in_bool || parent.operator == "||" && parent.left === compressor.self()) { AST_Node.warn("Dropping side-effect-free || [{file}:{line},{col}]", self.start); return self.left.optimize(compressor); } } else if (!(rr instanceof AST_Node)) { - if (compressor.option("booleans") && compressor.in_boolean_context()) { + if (in_bool) { AST_Node.warn("Boolean || always true [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, @@ -6017,12 +6014,11 @@ merge(Compressor.prototype, { } if (compressor.option("unsafe")) { var indexRight = is_indexFn(self.right); - if (compressor.option("booleans") + if (in_bool && indexRight && (self.operator == "==" || self.operator == "!=") && self.left instanceof AST_Number - && self.left.getValue() == 0 - && compressor.in_boolean_context()) { + && self.left.getValue() == 0) { return (self.operator == "==" ? make_node(AST_UnaryPrefix, self, { operator: "!", expression: self.right |