side effect fixes and small optimization for gzip
authorMihai Bazon <mihai@bazon.net>
Wed, 12 Sep 2012 10:23:24 +0000 (13:23 +0300)
committerMihai Bazon <mihai@bazon.net>
Wed, 12 Sep 2012 10:41:46 +0000 (13:41 +0300)
prefer to always use > and >= operators (idea from Closure)

lib/compress.js

index 7faf2df..06b133f 100644 (file)
@@ -478,6 +478,7 @@ function Compressor(options, false_by_default) {
             return this.left.has_side_effects()
                 || this.right.has_side_effects ();
         });
+        def(AST_Assign, function(){ return true });
         def(AST_Conditional, function(){
             return this.condition.has_side_effects()
                 || this.consequent.has_side_effects()
@@ -488,6 +489,7 @@ function Compressor(options, false_by_default) {
                 || this.operator == "++"
                 || this.operator == "--";
         });
+        def(AST_SymbolRef, function(){ return false });
     })(function(node, func){
         node.DEFMETHOD("has_side_effects", func);
     });
@@ -1100,7 +1102,25 @@ function Compressor(options, false_by_default) {
             }
             break;
         }
-        return this.evaluate(compressor)[0];
+        var exp = this.evaluate(compressor);
+        if (exp.length == 2) {
+            if (best_of(exp[0], this) !== this)
+                return exp[0];
+        }
+        var self = this;
+        if (compressor.option("comparations")) {
+            var reverse = function(op) {
+                self.operator = op;
+                var tmp = self.left;
+                self.left = self.right;
+                self.right = tmp;
+            };
+            if (self instanceof AST_Binary) switch (self.operator) {
+              case "<": reverse(">"); break;
+              case "<=": reverse(">="); break;
+            }
+        }
+        return self;
     });
 
     SQUEEZE(AST_Assign, function(self, compressor){