From 5351057557432913a70dbd0b8a44807b8db23b87 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 1 Nov 2014 23:08:16 +1100 Subject: [PATCH] generator: move BlockStatement to base generator --- lib/6to5/generators/base.js | 12 +++++++++ lib/6to5/generators/statements.js | 44 +++++++++++++++---------------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/lib/6to5/generators/base.js b/lib/6to5/generators/base.js index 2753113550..00a36b426f 100644 --- a/lib/6to5/generators/base.js +++ b/lib/6to5/generators/base.js @@ -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("}"); + } +}; diff --git a/lib/6to5/generators/statements.js b/lib/6to5/generators/statements.js index b11a7b9868..4e32b73123 100644 --- a/lib/6to5/generators/statements.js +++ b/lib/6to5/generators/statements.js @@ -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("}"); };