diff --git a/lib/6to5/generators/array-comprehensions.js b/lib/6to5/generators/array-comprehensions.js index a7c6e9a37d..d7af03e074 100644 --- a/lib/6to5/generators/array-comprehensions.js +++ b/lib/6to5/generators/array-comprehensions.js @@ -9,15 +9,15 @@ exports.ComprehensionBlock = function (node, print) { exports.ComprehensionExpression = function (node, print) { this.push("["); - this.printJoin(print, node.blocks, " "); - this.push(" "); + this.printJoin(print, node.blocks, { separator: " " }); + this.space(); if (node.filter) { this.keyword("if"); this.push("("); print(node.filter); this.push(")"); - this.push(" "); + this.space(); } print(node.body); diff --git a/lib/6to5/generators/base.js b/lib/6to5/generators/base.js index 2fdecb1e61..5d7eb6f412 100644 --- a/lib/6to5/generators/base.js +++ b/lib/6to5/generators/base.js @@ -13,7 +13,7 @@ exports.BlockStatement = function (node, print) { this.push("{"); this.newline(); print.sequence(node.body, { indent: true }); - this.newline(true); - this.push("}"); + this.removeLast("\n"); + this.rightBrace(); } }; diff --git a/lib/6to5/generators/classes.js b/lib/6to5/generators/classes.js index 06265e57bb..80bf6b784a 100644 --- a/lib/6to5/generators/classes.js +++ b/lib/6to5/generators/classes.js @@ -3,7 +3,7 @@ exports.ClassDeclaration = function (node, print) { this.push("class"); if (node.id) { - this.push(" "); + this.space(); print(node.id); } @@ -12,7 +12,7 @@ exports.ClassDeclaration = function (node, print) { print(node.superClass); } - this.push(" "); + this.space(); print(node.body); }; @@ -27,7 +27,6 @@ exports.ClassBody = function (node, print) { print.sequence(node.body); this.dedent(); - this.newline(true); - this.push("}"); + this.rightBrace(); } }; diff --git a/lib/6to5/generators/expressions.js b/lib/6to5/generators/expressions.js index 0c32498fea..4d81a81f5f 100644 --- a/lib/6to5/generators/expressions.js +++ b/lib/6to5/generators/expressions.js @@ -1,13 +1,19 @@ var t = require("../types"); exports.UnaryExpression = function (node, print) { - this.push(node.operator); - + var hasSpace = /[a-z]$/.test(node.operator); var arg = node.argument; - if (/[a-z]$/.test(node.operator) || t.isUpdateExpression(arg) || t.isUnaryExpression(arg)) { - this.push(" "); + + if (t.isUpdateExpression(arg) || t.isUnaryExpression(arg)) { + hasSpace = true; } + if (t.isUnaryExpression(arg) && arg.operator === "!") { + hasSpace = false; + } + + this.push(node.operator); + if (hasSpace) this.space(); print(node.argument); }; @@ -29,18 +35,16 @@ exports.ConditionalExpression = function (node, print) { print(node.alternate); }; -exports.NewExpression = function (node, print) { +exports.NewExpression = function (node, print, parent) { this.push("new "); print(node.callee); - if (node.arguments.length) { - this.push("("); - this.printJoin(print, node.arguments, ", "); - this.push(")"); - } + this.push("("); + this.printJoin(print, node.arguments, { separator: ", " }); + this.push(")"); }; exports.SequenceExpression = function (node, print) { - this.printJoin(print, node.expressions, ", "); + this.printJoin(print, node.expressions, { separator: ", " }); }; exports.ThisExpression = function () { @@ -50,7 +54,7 @@ exports.ThisExpression = function () { exports.CallExpression = function (node, print) { print(node.callee); this.push("("); - this.printJoin(print, node.arguments, ", "); + this.printJoin(print, node.arguments, { separator: ", " }); this.push(")"); }; @@ -58,7 +62,7 @@ exports.YieldExpression = function (node, print) { this.push("yield"); if (node.delegate) this.push("*"); if (node.argument) { - this.push(" "); + this.space(); print(node.argument); } }; diff --git a/lib/6to5/generators/jsx.js b/lib/6to5/generators/jsx.js index 25cd8b1cad..dfd672b888 100644 --- a/lib/6to5/generators/jsx.js +++ b/lib/6to5/generators/jsx.js @@ -65,8 +65,8 @@ exports.XJSOpeningElement = function (node, print) { this.push("<"); print(node.name); if (node.attributes.length > 0) { - this.push(" "); - this.printJoin(print, node.attributes, " "); + this.space(); + this.printJoin(print, node.attributes, { separator: " " }); } this.push(node.selfClosing ? " />" : ">"); }; diff --git a/lib/6to5/generators/methods.js b/lib/6to5/generators/methods.js index d741ffb75e..1004f193dd 100644 --- a/lib/6to5/generators/methods.js +++ b/lib/6to5/generators/methods.js @@ -5,7 +5,8 @@ exports._params = function (node, print) { this.push("("); - this.printJoin(print, node.params, ", ", { + this.printJoin(print, node.params, { + separator: ", ", iterator: function (param, i) { var def = node.defaults && node.defaults[i]; if (def) { @@ -49,7 +50,7 @@ exports._method = function (node, print) { } this._params(value, print); - this.push(" "); + this.space(); print(value.body); }; @@ -64,7 +65,7 @@ exports.MethodDefinition = function (node, print) { exports.ReturnStatement = function (node, print) { this.push("return"); if (node.argument) { - this.push(" "); + this.space(); print(node.argument); } this.semicolon(); @@ -74,10 +75,10 @@ exports.FunctionDeclaration = exports.FunctionExpression = function (node, print) { this.push("function"); if (node.generator) this.push("*"); - this.push(" "); + this.space(); if (node.id) print(node.id); this._params(node, print); - this.push(" "); + this.space(); print(node.body); }; diff --git a/lib/6to5/generators/modules.js b/lib/6to5/generators/modules.js index 14a10e53bb..c55d6d5f71 100644 --- a/lib/6to5/generators/modules.js +++ b/lib/6to5/generators/modules.js @@ -31,9 +31,9 @@ exports.ExportDeclaration = function (node, print) { } else { this.push("{"); if (specifiers.length) { - this.push(" "); - this.printJoin(print, specifiers, ", "); - this.push(" "); + this.space(); + this.printJoin(print, specifiers, { separator: ", " }); + this.space(); } this.push("}"); } diff --git a/lib/6to5/generators/statements.js b/lib/6to5/generators/statements.js index 84b4f28846..0ae4576f63 100644 --- a/lib/6to5/generators/statements.js +++ b/lib/6to5/generators/statements.js @@ -13,12 +13,12 @@ exports.IfStatement = function (node, print) { print(node.test); this.push(") "); - print(node.consequent); + this.printAndIndentOnComments(print, node.consequent); if (node.alternate) { - if (this.isLast("}")) this.push(" "); + if (this.isLast("}")) this.space(); this.keyword("else"); - print(node.alternate); + this.printAndIndentOnComments(print, node.alternate); } }; @@ -30,13 +30,13 @@ exports.ForStatement = function (node, print) { this.push(";"); if (node.test) { - this.push(" "); + this.space(); print(node.test); } this.push(";"); if (node.update) { - this.push(" "); + this.space(); print(node.update); } @@ -73,10 +73,11 @@ exports.ForOfStatement = function (node, print) { }; exports.DoWhileStatement = function (node, print) { - this.push("do "); + this.keyword("do"); print(node.body); - this.push(" while"); - this.push(" ("); + this.space(); + this.keyword("while"); + this.push("("); print(node.test); this.push(");"); }; @@ -84,7 +85,7 @@ exports.DoWhileStatement = function (node, print) { exports.BreakStatement = function (node, print) { this.push("break"); if (node.label) { - this.push(" "); + this.space(); print(node.label); } this.semicolon(); @@ -93,7 +94,7 @@ exports.BreakStatement = function (node, print) { exports.ContinueStatement = function (node, print) { this.push("continue"); if (node.label) { - this.push(" "); + this.space(); print(node.label); } this.semicolon(); @@ -108,10 +109,11 @@ exports.LabeledStatement = function (node, print) { exports.TryStatement = function (node, print) { this.keyword("try"); print(node.block); - this.push(" "); + this.space(); print(node.handler); if (node.finalizer) { - this.push(" finally "); + this.space(); + this.push("finally "); print(node.finalizer); } }; @@ -135,14 +137,17 @@ exports.SwitchStatement = function (node, print) { this.push("("); print(node.discriminant); this.push(") {"); - - if (node.cases.length > 0) { - this.newline(); - print.sequence(node.cases, { indent: true }); - this.newline(); - } - + print.sequence(node.cases, { indent: true }); this.push("}"); + + //if (node.cases.length) { + // this.newline(); + // print.sequence(node.cases, { indent: true }); + // this.newline(); + // this.rightBrace(); + //} else { + // this.push("}"); + //} }; exports.SwitchCase = function (node, print) { @@ -155,7 +160,7 @@ exports.SwitchCase = function (node, print) { } if (node.consequent.length === 1) { - this.push(" "); + this.space(); print(node.consequent[0]); } else if (node.consequent.length > 1) { this.newline(); @@ -170,7 +175,7 @@ exports.DebuggerStatement = function () { exports.VariableDeclaration = function (node, print, parent) { this.push(node.kind + " "); - this.printJoin(print, node.declarations, ", "); + this.printJoin(print, node.declarations, { separator: ", " }); if (!t.isFor(parent)) { this.semicolon(); diff --git a/lib/6to5/generators/template-literals.js b/lib/6to5/generators/template-literals.js index 87b1b6c2e3..b723a2b600 100644 --- a/lib/6to5/generators/template-literals.js +++ b/lib/6to5/generators/template-literals.js @@ -6,7 +6,7 @@ exports.TaggedTemplateExpression = function (node, print) { }; exports.TemplateElement = function (node) { - this.push(node.value.raw, true); + this._push(node.value.raw); }; exports.TemplateLiteral = function (node, print) { diff --git a/lib/6to5/generators/types.js b/lib/6to5/generators/types.js index bce2bb6aa8..a1e39628a4 100644 --- a/lib/6to5/generators/types.js +++ b/lib/6to5/generators/types.js @@ -15,17 +15,11 @@ exports.ObjectPattern = function (node, print) { if (props.length) { this.push("{"); + this.space(); - if (props.length === 1) { - this.push(" "); - this.print(props[0]); - this.push(" "); - } else { - this.newline(); - this.printJoin(print, props, ",\n", { indent: true }); - this.newline(); - } + this.printJoin(print, props, { separator: ", ", indent: true }); + this.space(); this.push("}"); } else { this.push("{}");