more smallish optimizations
authorMihai Bazon <mihai@bazon.net>
Mon, 17 Sep 2012 08:50:35 +0000 (11:50 +0300)
committerMihai Bazon <mihai@bazon.net>
Mon, 17 Sep 2012 08:50:35 +0000 (11:50 +0300)
lib/compress.js

index 09dc180..ddb4437 100644 (file)
@@ -1321,9 +1321,10 @@ function Compressor(options, false_by_default) {
     });
 
     AST_UnaryPrefix.DEFMETHOD("optimize", function(compressor){
+        var self = this;
+        var e = self.expression;
         if (compressor.option("booleans") && compressor.in_boolean_context()) {
-            var e = this.expression;
-            switch (this.operator) {
+            switch (self.operator) {
               case "!":
                 if (e instanceof AST_UnaryPrefix && e.operator == "!") {
                     // !!foo ==> foo, if we're in boolean context
@@ -1333,11 +1334,14 @@ function Compressor(options, false_by_default) {
               case "typeof":
                 // typeof always returns a non-empty string, thus it's
                 // always true in booleans
-                compressor.warn("Boolean expression always true [{line},{col}]", this.start);
-                return make_node(AST_True, this).optimize(compressor);
+                compressor.warn("Boolean expression always true [{line},{col}]", self.start);
+                return make_node(AST_True, self).optimize(compressor);
             }
         }
-        return this.evaluate(compressor)[0];
+        if (e instanceof AST_Binary) {
+            self = best_of(self, e.negate(compressor));
+        }
+        return self.evaluate(compressor)[0];
     });
 
     SQUEEZE(AST_Binary, function(self, compressor){
@@ -1531,6 +1535,11 @@ function Compressor(options, false_by_default) {
     AST_Conditional.DEFMETHOD("optimize", function(compressor){
         var self = this;
         if (!compressor.option("conditionals")) return self;
+        if (self.condition instanceof AST_Seq) {
+            var car = self.condition.car;
+            self.condition = self.condition.cdr;
+            return AST_Seq.cons(car, self.optimize(compressor)).optimize(compressor);
+        }
         var cond = self.condition.evaluate(compressor);
         if (cond.length == 2) {
             if (cond[1]) {