an AST_If is too a StatementWithBody

This commit is contained in:
Mihai Bazon
2012-09-03 12:11:44 +03:00
parent d7c1dc6c05
commit 6d0db4ce14
4 changed files with 21 additions and 17 deletions

View File

@@ -286,16 +286,16 @@ var AST_Continue = DEFNODE("Continue", null, {
/* -----[ IF ]----- */ /* -----[ IF ]----- */
var AST_If = DEFNODE("If", "condition consequent alternative", { var AST_If = DEFNODE("If", "condition alternative", {
$documentation: "A `if` statement", $documentation: "A `if` statement",
_walk: function(visitor) { _walk: function(visitor) {
return visitor._visit(this, function(){ return visitor._visit(this, function(){
this.condition._walk(visitor); this.condition._walk(visitor);
this.consequent._walk(visitor); this.body._walk(visitor);
if (this.alternative) this.alternative._walk(visitor); if (this.alternative) this.alternative._walk(visitor);
}); });
} }
}); }, AST_StatementWithBody);
/* -----[ SWITCH ]----- */ /* -----[ SWITCH ]----- */

View File

@@ -276,7 +276,7 @@ function Compressor(options, false_by_default) {
SQUEEZE(AST_If, function(self, compressor){ SQUEEZE(AST_If, function(self, compressor){
self = self.clone(); self = self.clone();
self.condition = self.condition.squeeze(compressor); self.condition = self.condition.squeeze(compressor);
self.consequent = self.consequent.squeeze(compressor); self.body = self.body.squeeze(compressor);
if (self.alternative) if (self.alternative)
self.alternative = self.alternative.squeeze(compressor); self.alternative = self.alternative.squeeze(compressor);
return self; return self;

View File

@@ -447,6 +447,10 @@ function OutputStream(options) {
}); });
}; };
AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output){
force_statement(this.body, output);
});
DEFPRINT(AST_Statement, function(self, output){ DEFPRINT(AST_Statement, function(self, output){
self.body.print(output); self.body.print(output);
output.semicolon(); output.semicolon();
@@ -476,7 +480,7 @@ function OutputStream(options) {
DEFPRINT(AST_Do, function(self, output){ DEFPRINT(AST_Do, function(self, output){
output.print("do"); output.print("do");
output.space(); output.space();
force_statement(self.body, output); self._do_print_body(output);
output.space(); output.space();
output.print("while"); output.print("while");
output.space(); output.space();
@@ -492,7 +496,7 @@ function OutputStream(options) {
self.condition.print(output); self.condition.print(output);
}); });
output.space(); output.space();
force_statement(self.body, output); self._do_print_body(output);
}); });
DEFPRINT(AST_For, function(self, output){ DEFPRINT(AST_For, function(self, output){
output.print("for"); output.print("for");
@@ -517,7 +521,7 @@ function OutputStream(options) {
} }
}); });
output.space(); output.space();
force_statement(self.body, output); self._do_print_body(output);
}); });
DEFPRINT(AST_ForIn, function(self, output){ DEFPRINT(AST_ForIn, function(self, output){
output.print("for"); output.print("for");
@@ -530,7 +534,7 @@ function OutputStream(options) {
self.object.print(output); self.object.print(output);
}); });
output.space(); output.space();
force_statement(self.body, output); self._do_print_body(output);
}); });
DEFPRINT(AST_With, function(self, output){ DEFPRINT(AST_With, function(self, output){
output.print("with"); output.print("with");
@@ -539,7 +543,7 @@ function OutputStream(options) {
self.expression.print(output); self.expression.print(output);
}); });
output.space(); output.space();
force_statement(self.body, output); self._do_print_body(output);
}); });
/* -----[ functions ]----- */ /* -----[ functions ]----- */
@@ -606,22 +610,22 @@ function OutputStream(options) {
// IF *without* an ELSE block (then the outer ELSE would refer // IF *without* an ELSE block (then the outer ELSE would refer
// to the inner IF). This function checks for this case and // to the inner IF). This function checks for this case and
// adds the block brackets if needed. // adds the block brackets if needed.
if (!self.consequent) if (!self.body)
return output.semicolon(); return output.semicolon();
if (self.consequent instanceof AST_Do if (self.body instanceof AST_Do
&& output.option("ie_proof")) { && output.option("ie_proof")) {
// https://github.com/mishoo/UglifyJS/issues/#issue/57 IE // https://github.com/mishoo/UglifyJS/issues/#issue/57 IE
// croaks with "syntax error" on code like this: if (foo) // croaks with "syntax error" on code like this: if (foo)
// do ... while(cond); else ... we need block brackets // do ... while(cond); else ... we need block brackets
// around do/while // around do/while
make_block(self.consequent, output); make_block(self.body, output);
return; return;
} }
var b = self.consequent; var b = self.body;
while (true) { while (true) {
if (b instanceof AST_If) { if (b instanceof AST_If) {
if (!b.alternative) { if (!b.alternative) {
make_block(self.consequent, output); make_block(self.body, output);
return; return;
} }
b = b.alternative; b = b.alternative;
@@ -631,7 +635,7 @@ function OutputStream(options) {
} }
else break; else break;
} }
self.consequent.print(output); self.body.print(output);
}; };
DEFPRINT(AST_If, function(self, output){ DEFPRINT(AST_If, function(self, output){
output.print("if"); output.print("if");
@@ -647,7 +651,7 @@ function OutputStream(options) {
output.space(); output.space();
self.alternative.print(output); self.alternative.print(output);
} else { } else {
force_statement(self.consequent, output); self._do_print_body(output);
} }
}); });

View File

@@ -999,7 +999,7 @@ function parse($TEXT, exigent_mode) {
} }
return new AST_If({ return new AST_If({
condition : cond, condition : cond,
consequent : body, body : body,
alternative : belse alternative : belse
}); });
}; };