more small optimizations
authorMihai Bazon <mihai@bazon.net>
Wed, 17 Oct 2012 18:57:08 +0000 (21:57 +0300)
committerMihai Bazon <mihai@bazon.net>
Wed, 17 Oct 2012 18:57:08 +0000 (21:57 +0300)
(unlikely to help for hand-written code)

lib/compress.js

index d2e3ffc..1af4a2d 100644 (file)
@@ -162,6 +162,9 @@ merge(Compressor.prototype, {
             if (val === null) {
                 return make_node(AST_Null, orig).optimize(compressor);
             }
+            if (val instanceof RegExp) {
+                return make_node(AST_RegExp, orig).optimize(compressor);
+            }
             throw new Error(string_template("Can't handle constant of type: {type}", {
                 type: typeof val
             }));
@@ -1109,7 +1112,7 @@ merge(Compressor.prototype, {
                         extract_declarations_from_unreachable_code(compressor, self.alternative, a);
                     }
                     a.push(self.body);
-                    return make_node(AST_BlockStatement, self, { body: a });
+                    return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);
                 }
             } else {
                 compressor.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
@@ -1117,7 +1120,7 @@ merge(Compressor.prototype, {
                     var a = [];
                     extract_declarations_from_unreachable_code(compressor, self.body, a);
                     if (self.alternative) a.push(self.alternative);
-                    return make_node(AST_BlockStatement, self, { body: a });
+                    return make_node(AST_BlockStatement, self, { body: a }).transform(compressor);
                 }
             }
         }
@@ -1134,7 +1137,7 @@ merge(Compressor.prototype, {
         if (is_empty(self.body) && is_empty(self.alternative)) {
             return make_node(AST_SimpleStatement, self.condition, {
                 body: self.condition
-            });
+            }).transform(compressor);
         }
         if (self.body instanceof AST_SimpleStatement
             && self.alternative instanceof AST_SimpleStatement) {
@@ -1144,7 +1147,7 @@ merge(Compressor.prototype, {
                     consequent  : self.body.body,
                     alternative : self.alternative.body
                 })
-            });
+            }).transform(compressor);
         }
         if (is_empty(self.alternative) && self.body instanceof AST_SimpleStatement) {
             if (negated_is_best) return make_node(AST_SimpleStatement, self, {
@@ -1153,14 +1156,14 @@ merge(Compressor.prototype, {
                     left     : negated,
                     right    : self.body.body
                 })
-            });
+            }).transform(compressor);
             return make_node(AST_SimpleStatement, self, {
                 body: make_node(AST_Binary, self, {
                     operator : "&&",
                     left     : self.condition,
                     right    : self.body.body
                 })
-            });
+            }).transform(compressor);
         }
         if (self.body instanceof AST_EmptyStatement
             && self.alternative
@@ -1171,7 +1174,7 @@ merge(Compressor.prototype, {
                     left     : self.condition,
                     right    : self.alternative.body
                 })
-            });
+            }).transform(compressor);
         }
         if (self.body instanceof AST_Exit
             && self.alternative instanceof AST_Exit
@@ -1182,7 +1185,7 @@ merge(Compressor.prototype, {
                     consequent  : self.body.value,
                     alternative : self.alternative.value || make_node(AST_Undefined, self).optimize(compressor)
                 })
-            });
+            }).transform(compressor);
         }
         if (self.body instanceof AST_If
             && !self.body.alternative
@@ -1200,7 +1203,7 @@ merge(Compressor.prototype, {
                 self.alternative = null;
                 return make_node(AST_BlockStatement, self, {
                     body: [ self, alt ]
-                });
+                }).transform(compressor);
             }
         }
         if (aborts(self.alternative)) {
@@ -1210,12 +1213,17 @@ merge(Compressor.prototype, {
             self.alternative = null;
             return make_node(AST_BlockStatement, self, {
                 body: [ self, body ]
-            });
+            }).transform(compressor);
         }
         return self;
     });
 
     OPT(AST_Switch, function(self, compressor){
+        if (self.body.length == 0 && compressor.option("conditionals")) {
+            return make_node(AST_SimpleStatement, self, {
+                body: self.expression
+            }).transform(compressor);
+        }
         var last_branch = self.body[self.body.length - 1];
         if (last_branch) {
             var stat = last_branch.body[last_branch.body.length - 1]; // last statement
@@ -1303,14 +1311,14 @@ merge(Compressor.prototype, {
                     left: exp.expression,
                     operator: "+",
                     right: make_node(AST_String, self, { value: "" })
-                });
+                }).transform(compressor);
             }
         }
         if (compressor.option("side_effects")) {
             if (self.expression instanceof AST_Function
                 && self.args.length == 0
                 && !self.expression.has_side_effects()) {
-                return make_node(AST_Undefined, self);
+                return make_node(AST_Undefined, self).transform(compressor);
             }
         }
         return self;
@@ -1614,4 +1622,14 @@ merge(Compressor.prototype, {
         return self;
     });
 
+    function literals_in_boolean_context(self, compressor) {
+        if (compressor.option("booleans") && compressor.in_boolean_context()) {
+            return make_node(AST_True, self);
+        }
+        return self;
+    };
+    OPT(AST_Array, literals_in_boolean_context);
+    OPT(AST_Object, literals_in_boolean_context);
+    OPT(AST_RegExp, literals_in_boolean_context);
+
 })();