generator: move BlockStatement to base generator

This commit is contained in:
Sebastian McKenzie
2014-11-01 23:08:16 +11:00
parent 1a5ee7d5da
commit 5351057557
2 changed files with 33 additions and 23 deletions

View File

@@ -5,3 +5,15 @@ exports.File = function (node, print) {
exports.Program = function (node, print) {
print.sequence(node.body);
};
exports.BlockStatement = function (node, print) {
if (node.body.length === 0) {
this.push("{}");
} else {
this.push("{");
this.newline();
print.sequence(node.body, { indent: true });
this.newline();
this.push("}");
}
};

View File

@@ -1,3 +1,4 @@
var t = require("../types");
var _ = require("lodash");
exports.WithStatement = function (node, print) {
@@ -13,26 +14,15 @@ exports.IfStatement = function (node, print) {
this.push("(");
print(node.test);
this.push(") ");
print(node.consequent);
var consequent = node.consequent;
if (node.alternate) consequent = t.toBlock(consequent);
print(consequent);
if (node.alternate) {
if (this.isLast("}")) this.push(" ");
this.keyword("else");
print(node.alternate);
}
};
exports.BlockStatement = function (node, print) {
if (node.body.length === 0) {
this.push("{}");
} else {
this.push("{");
this.newline();
this.indent();
print.sequence(node.body);
this.dedent();
this.newline();
this.push("}");
print(t.toBlock(node.alternate));
}
};
@@ -41,12 +31,19 @@ exports.ForStatement = function (node, print) {
this.push("(");
print(node.init);
this.push("; ");
this.push(";");
print(node.test);
this.push("; ");
if (node.test) {
this.push(" ");
print(node.test);
}
this.push(";");
if (node.update) {
this.push(" ");
print(node.update);
}
print(node.update);
this.push(") ");
print(node.body);
@@ -109,8 +106,7 @@ exports.ContinueStatement = function (node, print) {
exports.LabeledStatement = function (node, print) {
print(node.label);
this.push(":");
this.newline();
this.push(": ");
print(node.body);
};
@@ -144,11 +140,13 @@ exports.SwitchStatement = function (node, print) {
this.push("(");
print(node.discriminant);
this.push(") {");
if (node.cases.length > 0) {
this.newline();
this.printJoin(print, node.cases, "\n");
print.sequence(node.cases, { indent: true });
this.newline();
}
this.push("}");
};