fix output for certain edge cases
authorMihai Bazon <mihai@bazon.net>
Wed, 22 Aug 2012 10:20:05 +0000 (13:20 +0300)
committerMihai Bazon <mihai@bazon.net>
Wed, 22 Aug 2012 10:20:05 +0000 (13:20 +0300)
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.

lib/output.js

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