fix output for certain edge cases

the statements if, for, do, while and with might have an AST_EmptyStatement
as body; if that's the case, we need to make sure that the semicolon gets in
the output.
This commit is contained in:
Mihai Bazon
2012-08-22 13:20:05 +03:00
parent fb8c9e3a48
commit f53e139d3c

View File

@@ -144,6 +144,11 @@ function OutputStream(options) {
might_need_semicolon = true; might_need_semicolon = true;
}; };
function force_semicolon() {
might_need_semicolon = false;
print(";");
};
function next_indent() { function next_indent() {
return indentation + options.indent_level; return indentation + options.indent_level;
}; };
@@ -189,29 +194,30 @@ function OutputStream(options) {
var stack = []; var stack = [];
return { return {
get : function() { return OUTPUT }, get : function() { return OUTPUT },
indent : indent, indent : indent,
newline : newline, newline : newline,
print : print, print : print,
space : space, space : space,
comma : comma, comma : comma,
colon : colon, colon : colon,
last : function() { return last }, last : function() { return last },
semicolon : semicolon, semicolon : semicolon,
print_name : function(name) { print(make_name(name)) }, force_semicolon : force_semicolon,
print_string : function(str) { print(encode_string(str)) }, print_name : function(name) { print(make_name(name)) },
with_indent : with_indent, print_string : function(str) { print(encode_string(str)) },
with_block : with_block, with_indent : with_indent,
with_parens : with_parens, with_block : with_block,
with_square : with_square, with_parens : with_parens,
option : function(opt) { return options[opt] }, with_square : with_square,
line : function() { return current_line }, option : function(opt) { return options[opt] },
col : function() { return current_col }, line : function() { return current_line },
pos : function() { return current_pos }, col : function() { return current_col },
push_node : function(node) { stack.push(node) }, pos : function() { return current_pos },
pop_node : function() { return stack.pop() }, push_node : function(node) { stack.push(node) },
stack : function() { return stack }, pop_node : function() { return stack.pop() },
parent : function(n) { stack : function() { return stack },
parent : function(n) {
return stack[stack.length - 2 - (n || 0)]; return stack[stack.length - 2 - (n || 0)];
} }
}; };
@@ -398,7 +404,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();
self.body.print(output); force_statement(self.body, output);
output.space(); output.space();
output.print("while"); output.print("while");
output.space(); output.space();
@@ -414,7 +420,7 @@ function OutputStream(options) {
self.condition.print(output); self.condition.print(output);
}); });
output.space(); output.space();
self.body.print(output); force_statement(self.body, output);
}); });
DEFPRINT(AST_For, function(self, output){ DEFPRINT(AST_For, function(self, output){
output.print("for"); output.print("for");
@@ -439,7 +445,7 @@ function OutputStream(options) {
} }
}); });
output.space(); output.space();
self.body.print(output); force_statement(self.body, output);
}); });
DEFPRINT(AST_ForIn, function(self, output){ DEFPRINT(AST_ForIn, function(self, output){
output.print("for"); output.print("for");
@@ -452,7 +458,7 @@ function OutputStream(options) {
self.object.print(output); self.object.print(output);
}); });
output.space(); output.space();
self.body.print(output); force_statement(self.body, output);
}); });
DEFPRINT(AST_With, function(self, output){ DEFPRINT(AST_With, function(self, output){
output.print("with"); output.print("with");
@@ -461,7 +467,7 @@ function OutputStream(options) {
self.expression.print(output); self.expression.print(output);
}); });
output.space(); output.space();
self.body.print(output); force_statement(self.body, output);
}); });
/* -----[ functions ]----- */ /* -----[ functions ]----- */
@@ -572,7 +578,7 @@ function OutputStream(options) {
output.space(); output.space();
self.alternative.print(output); self.alternative.print(output);
} else { } else {
self.consequent.print(output); force_statement(self.consequent, output);
} }
}); });
@@ -832,6 +838,13 @@ function OutputStream(options) {
if (self.mods) output.print(self.mods); if (self.mods) output.print(self.mods);
}); });
function force_statement(stat, output) {
if (stat instanceof AST_EmptyStatement)
output.force_semicolon();
else
stat.print(output);
};
// return true if the node at the top of the stack (that means the // return true if the node at the top of the stack (that means the
// innermost node in the current output) is lexically the first in // innermost node in the current output) is lexically the first in
// a statement. // a statement.