some cleanup
authorMihai Bazon <mihai@bazon.net>
Sun, 16 Sep 2012 15:05:15 +0000 (18:05 +0300)
committerMihai Bazon <mihai@bazon.net>
Sun, 16 Sep 2012 15:10:54 +0000 (18:10 +0300)
lib/compress.js

index bd14529..67f3726 100644 (file)
@@ -189,7 +189,6 @@ function Compressor(options, false_by_default) {
             }
             if (compressor.option("sequences")) {
                 statements = sequencesize(statements, compressor);
-                statements = sequencesize_2(statements, compressor);
             }
             if (compressor.option("if_return")) {
                 statements = handle_if_return(statements, compressor);
@@ -348,73 +347,51 @@ function Compressor(options, false_by_default) {
                 else push_seq(), ret.push(stat);
             });
             push_seq();
-
-            // if the last node is return or throw, we can mix in the
-            // previous sequence which might help reducing the list to
-            // a single statement.
-            var exit = ret[ret.length - 1], prev = ret[ret.length - 2];
-            if (prev instanceof AST_SimpleStatement
-                && exit instanceof AST_Exit && exit.value) {
-                ret.pop();
-                ret.pop();
-                if (prev.body instanceof AST_Seq) {
-                    prev.body.add(exit.value);
-                    prev.body = prev.body.optimize(compressor);
-                    exit.value = prev.body;
-                }
-                else {
-                    exit.value = AST_Seq.cons(prev.body, exit.value).optimize(compressor);
-                }
-                ret.push(exit);
-            }
-
+            ret = sequencesize_2(ret, compressor);
             CHANGED = ret.length != statements.length;
             return ret;
         };
 
         function sequencesize_2(statements, compressor) {
+            function cons_seq(right) {
+                ret.pop();
+                var left = prev.body;
+                if (left instanceof AST_Seq) {
+                    left.add(right);
+                } else {
+                    left = AST_Seq.cons(left, right);
+                }
+                return left.optimize(compressor);
+            };
             var ret = [], prev = null;
             statements.forEach(function(stat){
                 if (prev) {
                     if (stat instanceof AST_For && stat.init && !(stat.init instanceof AST_Definitions)) {
-                        if (prev.body instanceof AST_Seq) {
-                            prev.body.add(stat.init);
-                            prev.body = prev.body.optimize(compressor);
-                            stat.init = prev.body;
-                        } else {
-                            stat.init = AST_Seq.cons(prev.body, stat.init).optimize(compressor);
-                        }
-                        ret.pop();
+                        stat.init = cons_seq(stat.init);
                     }
                     else if (stat instanceof AST_For && !stat.init) {
                         stat.init = prev.body;
                         ret.pop();
                     }
                     else if (stat instanceof AST_If) {
-                        if (prev.body instanceof AST_Seq) {
-                            prev.body.add(stat.condition);
-                            prev.body = prev.body.optimize(compressor);
-                            stat.condition = prev.body;
-                        } else {
-                            stat.condition = AST_Seq.cons(prev.body, stat.condition).optimize(compressor);
-                        }
-                        ret.pop();
+                        stat.condition = cons_seq(stat.condition);
                     }
                     else if (stat instanceof AST_With) {
-                        if (prev.body instanceof AST_Seq) {
-                            prev.body.add(stat.expression);
-                            prev.body = prev.body.optimize(compressor);
-                            stat.expression = prev.body;
-                        } else {
-                            stat.expression = AST_Seq.cons(prev.body, stat.expression).optimize(compressor);
-                        }
-                        ret.pop();
+                        stat.expression = cons_seq(stat.expression);
+                    }
+                    else if (stat instanceof AST_Exit && stat.value) {
+                        stat.value = cons_seq(stat.value);
+                    }
+                    else if (stat instanceof AST_Exit) {
+                        stat.value = cons_seq(make_node(AST_Undefined, stat));
+                    }
+                    else if (stat instanceof AST_Switch) {
+                        stat.expression = cons_seq(stat.expression);
                     }
                 }
                 ret.push(stat);
                 prev = stat instanceof AST_SimpleStatement ? stat : null;
             });
-            CHANGED = ret.length != statements.length;
             return ret;
         };