aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-10-26 01:16:12 +0800
committerGitHub <noreply@github.com>2017-10-26 01:16:12 +0800
commitee082ace1b69bff228ff43065333b8703c0505dc (patch)
tree60f980684c2895b70177b6e8c5f080e21e2dc50b /lib
parentae67a4985073dcdaa2788c86e576202923514e0d (diff)
downloadtracifyjs-ee082ace1b69bff228ff43065333b8703c0505dc.tar.gz
tracifyjs-ee082ace1b69bff228ff43065333b8703c0505dc.zip
compress self comparisons (#2398)
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js17
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 9f410718..073399b5 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -581,7 +581,7 @@ merge(Compressor.prototype, {
if (!value && props[i].key === key) value = props[i].value;
}
}
- return value instanceof AST_SymbolRef ? value.fixed_value() : value;
+ return value instanceof AST_SymbolRef && value.fixed_value() || value;
}
function is_modified(node, value, level, immutable) {
@@ -3824,6 +3824,11 @@ merge(Compressor.prototype, {
});
var commutativeOperators = makePredicate("== === != !== * & | ^");
+ function is_object(node) {
+ return node instanceof AST_Array
+ || node instanceof AST_Lambda
+ || node instanceof AST_Object;
+ }
OPT(AST_Binary, function(self, compressor){
function reversible() {
@@ -3859,7 +3864,8 @@ merge(Compressor.prototype, {
case "!==":
if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
(self.left.is_number(compressor) && self.right.is_number(compressor)) ||
- (self.left.is_boolean() && self.right.is_boolean())) {
+ (self.left.is_boolean() && self.right.is_boolean()) ||
+ self.left.equivalent_to(self.right)) {
self.operator = self.operator.substr(0, 2);
}
// XXX: intentionally falling down to the next case
@@ -3879,6 +3885,13 @@ merge(Compressor.prototype, {
if (self.operator.length == 2) self.operator += "=";
}
}
+ // obj !== obj => false
+ else if (self.left instanceof AST_SymbolRef
+ && self.right instanceof AST_SymbolRef
+ && self.left.definition() === self.right.definition()
+ && is_object(self.left.fixed_value())) {
+ return make_node(self.operator[0] == "=" ? AST_True : AST_False, self);
+ }
break;
}
if (compressor.option("booleans") && self.operator == "+" && compressor.in_boolean_context()) {