boolean and if/exit optimizations
authorMihai Bazon <mihai@bazon.net>
Mon, 3 Sep 2012 20:49:57 +0000 (23:49 +0300)
committerMihai Bazon <mihai@bazon.net>
Mon, 3 Sep 2012 20:49:57 +0000 (23:49 +0300)
lib/compress.js

index 7e8ad3b..20af470 100644 (file)
@@ -62,6 +62,7 @@ function Compressor(options, false_by_default) {
         conditionals  : !false_by_default,
         comparations  : !false_by_default,
         evaluate      : !false_by_default,
+        booleans      : !false_by_default,
 
         warnings      : true
     });
@@ -560,6 +561,17 @@ function Compressor(options, false_by_default) {
                 }).optimize(compressor)
             });
         }
+        if (self.body instanceof AST_Exit
+            && self.alternative instanceof AST_Exit
+            && self.body.TYPE == self.alternative.TYPE) {
+            return make_node(self.body.CTOR, self, {
+                value: make_node(AST_Conditional, self, {
+                    condition   : self.condition,
+                    consequent  : self.body.value,
+                    alternative : self.alternative.value
+                }).optimize(compressor)
+            });
+        }
         return self;
     });
 
@@ -709,4 +721,22 @@ function Compressor(options, false_by_default) {
         return self;
     });
 
+    SQUEEZE(AST_True, function(self, compressor){
+        if (compressor.option("booleans")) return make_node(AST_UnaryPrefix, self, {
+            operator: "!",
+            expression: make_node(AST_Number, self, {
+                value: 0
+            })
+        });
+    });
+
+    SQUEEZE(AST_False, function(self, compressor){
+        if (compressor.option("booleans")) return make_node(AST_UnaryPrefix, self, {
+            operator: "!",
+            expression: make_node(AST_Number, self, {
+                value: 1
+            })
+        });
+    });
+
 })();