minor AST cleanup (AST_BlockStatement may inherit from AST_Block)
authorMihai Bazon <mihai@bazon.net>
Tue, 9 Oct 2012 10:52:32 +0000 (13:52 +0300)
committerMihai Bazon <mihai@bazon.net>
Tue, 9 Oct 2012 10:59:17 +0000 (13:59 +0300)
lib/ast.js
lib/compress.js
lib/transform.js

index fde1ca1..39a84a6 100644 (file)
@@ -123,7 +123,7 @@ var AST_Directive = DEFNODE("Directive", "value scope", {
 }, AST_Statement);
 
 var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", {
-    $documentation: "A statement consisting of an expression, i.e. a = 1 + 2.",
+    $documentation: "A statement consisting of an expression, i.e. a = 1 + 2",
     _walk: function(visitor) {
         return visitor._visit(this, function(){
             this.body._walk(visitor);
@@ -131,17 +131,6 @@ var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", {
     }
 }, AST_Statement);
 
-var AST_BlockStatement = DEFNODE("BlockStatement", "body", {
-    $documentation: "A block statement.",
-    _walk: function(visitor) {
-        return visitor._visit(this, function(){
-            this.body.forEach(function(stat){
-                stat._walk(visitor);
-            });
-        });
-    }
-}, AST_Statement);
-
 function walk_body(node, visitor) {
     if (node.body instanceof AST_Statement) {
         node.body._walk(visitor);
@@ -152,7 +141,7 @@ function walk_body(node, visitor) {
 };
 
 var AST_Block = DEFNODE("Block", "body", {
-    $documentation: "A block of statements (usually always bracketed)",
+    $documentation: "A body of statements (usually bracketed)",
     _walk: function(visitor) {
         return visitor._visit(this, function(){
             walk_body(this, visitor);
@@ -160,15 +149,19 @@ var AST_Block = DEFNODE("Block", "body", {
     }
 }, AST_Statement);
 
+var AST_BlockStatement = DEFNODE("BlockStatement", null, {
+    $documentation: "A block statement",
+}, AST_Block);
+
 var AST_EmptyStatement = DEFNODE("EmptyStatement", null, {
-    $documentation: "The empty statement (empty block or simply a semicolon).",
+    $documentation: "The empty statement (empty block or simply a semicolon)",
     _walk: function(visitor) {
         return visitor._visit(this);
     }
 }, AST_Statement);
 
 var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", {
-    $documentation: "Base class for all statements that contain one nested body: `For`, `ForIn`, `Do`, `While`, `With`.",
+    $documentation: "Base class for all statements that contain one nested body: `For`, `ForIn`, `Do`, `While`, `With`",
     _walk: function(visitor) {
         return visitor._visit(this, function(){
             this.body._walk(visitor);
@@ -187,7 +180,7 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
 }, AST_StatementWithBody);
 
 var AST_DWLoop = DEFNODE("DWLoop", "condition", {
-    $documentation: "Base class for do/while statements.",
+    $documentation: "Base class for do/while statements",
     _walk: function(visitor) {
         return visitor._visit(this, function(){
             this.condition._walk(visitor);
@@ -469,7 +462,7 @@ var AST_Call = DEFNODE("Call", "expression args", {
 });
 
 var AST_New = DEFNODE("New", null, {
-    $documentation: "An object instantiation.  Derives from a function call since it has exactly the same properties."
+    $documentation: "An object instantiation.  Derives from a function call since it has exactly the same properties"
 }, AST_Call);
 
 var AST_Seq = DEFNODE("Seq", "car cdr", {
index 6709d8e..abb00c2 100644 (file)
@@ -682,15 +682,13 @@ merge(Compressor.prototype, {
         def(AST_Constant, function(){ return false });
         def(AST_This, function(){ return false });
 
-        function block(){
+        def(AST_Block, function(){
             for (var i = this.body.length; --i >= 0;) {
                 if (this.body[i].has_side_effects())
                     return true;
             }
             return false;
-        };
-        def(AST_Block, block);
-        def(AST_BlockStatement, block);
+        });
 
         def(AST_SimpleStatement, function(){
             if (this.body instanceof AST_Function) return false;
@@ -780,6 +778,11 @@ merge(Compressor.prototype, {
         return self.label.references.length == 0 ? self.body : self;
     });
 
+    OPT(AST_Block, function(self, compressor){
+        self.body = tighten_body(self.body, compressor);
+        return self;
+    });
+
     OPT(AST_BlockStatement, function(self, compressor){
         self.body = tighten_body(self.body, compressor);
         switch (self.body.length) {
@@ -789,11 +792,6 @@ merge(Compressor.prototype, {
         return self;
     });
 
-    OPT(AST_Block, function(self, compressor){
-        self.body = tighten_body(self.body, compressor);
-        return self;
-    });
-
     AST_Scope.DEFMETHOD("drop_unused", function(compressor){
         var self = this;
         if (compressor.option("unused")
index db228be..e19210f 100644 (file)
@@ -93,10 +93,6 @@ TreeTransformer.prototype = new TreeWalker;
         self.body = self.body.transform(tw);
     });
 
-    _(AST_BlockStatement, function(self, tw){
-        self.body = do_list(self.body, tw);
-    });
-
     _(AST_Block, function(self, tw){
         self.body = do_list(self.body, tw);
     });