minor AST cleanup (AST_BlockStatement may inherit from AST_Block)

This commit is contained in:
Mihai Bazon
2012-10-09 13:52:32 +03:00
parent e1862cd36f
commit 9ead49641d
3 changed files with 17 additions and 30 deletions

View File

@@ -123,7 +123,7 @@ var AST_Directive = DEFNODE("Directive", "value scope", {
}, AST_Statement); }, AST_Statement);
var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", { 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) { _walk: function(visitor) {
return visitor._visit(this, function(){ return visitor._visit(this, function(){
this.body._walk(visitor); this.body._walk(visitor);
@@ -131,17 +131,6 @@ var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", {
} }
}, AST_Statement); }, 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) { function walk_body(node, visitor) {
if (node.body instanceof AST_Statement) { if (node.body instanceof AST_Statement) {
node.body._walk(visitor); node.body._walk(visitor);
@@ -152,7 +141,7 @@ function walk_body(node, visitor) {
}; };
var AST_Block = DEFNODE("Block", "body", { var AST_Block = DEFNODE("Block", "body", {
$documentation: "A block of statements (usually always bracketed)", $documentation: "A body of statements (usually bracketed)",
_walk: function(visitor) { _walk: function(visitor) {
return visitor._visit(this, function(){ return visitor._visit(this, function(){
walk_body(this, visitor); walk_body(this, visitor);
@@ -160,15 +149,19 @@ var AST_Block = DEFNODE("Block", "body", {
} }
}, AST_Statement); }, AST_Statement);
var AST_BlockStatement = DEFNODE("BlockStatement", null, {
$documentation: "A block statement",
}, AST_Block);
var AST_EmptyStatement = DEFNODE("EmptyStatement", null, { 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) { _walk: function(visitor) {
return visitor._visit(this); return visitor._visit(this);
} }
}, AST_Statement); }, AST_Statement);
var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", { 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) { _walk: function(visitor) {
return visitor._visit(this, function(){ return visitor._visit(this, function(){
this.body._walk(visitor); this.body._walk(visitor);
@@ -187,7 +180,7 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
}, AST_StatementWithBody); }, AST_StatementWithBody);
var AST_DWLoop = DEFNODE("DWLoop", "condition", { var AST_DWLoop = DEFNODE("DWLoop", "condition", {
$documentation: "Base class for do/while statements.", $documentation: "Base class for do/while statements",
_walk: function(visitor) { _walk: function(visitor) {
return visitor._visit(this, function(){ return visitor._visit(this, function(){
this.condition._walk(visitor); this.condition._walk(visitor);
@@ -469,7 +462,7 @@ var AST_Call = DEFNODE("Call", "expression args", {
}); });
var AST_New = DEFNODE("New", null, { 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); }, AST_Call);
var AST_Seq = DEFNODE("Seq", "car cdr", { var AST_Seq = DEFNODE("Seq", "car cdr", {

View File

@@ -682,15 +682,13 @@ merge(Compressor.prototype, {
def(AST_Constant, function(){ return false }); def(AST_Constant, function(){ return false });
def(AST_This, function(){ return false }); def(AST_This, function(){ return false });
function block(){ def(AST_Block, function(){
for (var i = this.body.length; --i >= 0;) { for (var i = this.body.length; --i >= 0;) {
if (this.body[i].has_side_effects()) if (this.body[i].has_side_effects())
return true; return true;
} }
return false; return false;
}; });
def(AST_Block, block);
def(AST_BlockStatement, block);
def(AST_SimpleStatement, function(){ def(AST_SimpleStatement, function(){
if (this.body instanceof AST_Function) return false; if (this.body instanceof AST_Function) return false;
@@ -780,6 +778,11 @@ merge(Compressor.prototype, {
return self.label.references.length == 0 ? self.body : self; 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){ OPT(AST_BlockStatement, function(self, compressor){
self.body = tighten_body(self.body, compressor); self.body = tighten_body(self.body, compressor);
switch (self.body.length) { switch (self.body.length) {
@@ -789,11 +792,6 @@ merge(Compressor.prototype, {
return self; return self;
}); });
OPT(AST_Block, function(self, compressor){
self.body = tighten_body(self.body, compressor);
return self;
});
AST_Scope.DEFMETHOD("drop_unused", function(compressor){ AST_Scope.DEFMETHOD("drop_unused", function(compressor){
var self = this; var self = this;
if (compressor.option("unused") if (compressor.option("unused")

View File

@@ -93,10 +93,6 @@ TreeTransformer.prototype = new TreeWalker;
self.body = self.body.transform(tw); self.body = self.body.transform(tw);
}); });
_(AST_BlockStatement, function(self, tw){
self.body = do_list(self.body, tw);
});
_(AST_Block, function(self, tw){ _(AST_Block, function(self, tw){
self.body = do_list(self.body, tw); self.body = do_list(self.body, tw);
}); });