From e702e672331ad4c3130ccb62f6f06d82db65bab5 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sat, 30 Apr 2016 13:19:21 -0700 Subject: [PATCH] Split all of the pushes into individual tokens. --- packages/babel-generator/src/buffer.js | 5 +- .../babel-generator/src/generators/classes.js | 19 ++++-- .../src/generators/expressions.js | 6 +- .../babel-generator/src/generators/flow.js | 66 ++++++++++++++----- .../babel-generator/src/generators/jsx.js | 10 ++- .../babel-generator/src/generators/methods.js | 22 +++++-- .../babel-generator/src/generators/modules.js | 61 ++++++++++++----- .../src/generators/statements.js | 35 ++++++---- 8 files changed, 165 insertions(+), 59 deletions(-) diff --git a/packages/babel-generator/src/buffer.js b/packages/babel-generator/src/buffer.js index a87185c1a2..e9eaeb4dcd 100644 --- a/packages/babel-generator/src/buffer.js +++ b/packages/babel-generator/src/buffer.js @@ -213,7 +213,10 @@ export default class Buffer { } this.removeLast(" "); - this.push(repeat("\n", i)); + this._removeSpacesAfterLastNewline(); + for (let j = 0; j < i; j++) { + this.push("\n"); + } } /** diff --git a/packages/babel-generator/src/generators/classes.js b/packages/babel-generator/src/generators/classes.js index 9d4e5cf24f..5c418ff97c 100644 --- a/packages/babel-generator/src/generators/classes.js +++ b/packages/babel-generator/src/generators/classes.js @@ -10,13 +10,17 @@ export function ClassDeclaration(node: Object) { this.print(node.typeParameters, node); if (node.superClass) { - this.push(" extends "); + this.push(" "); + this.push("extends"); + this.push(" "); this.print(node.superClass, node); this.print(node.superTypeParameters, node); } if (node.implements) { - this.push(" implements "); + this.push(" "); + this.push("implements"); + this.push(" "); this.printList(node.implements, node); } @@ -45,7 +49,10 @@ export function ClassBody(node: Object) { export function ClassProperty(node: Object) { this.printJoin(node.decorators, node); - if (node.static) this.push("static "); + if (node.static) { + this.push("static"); + this.push(" "); + } this.print(node.key, node); this.print(node.typeAnnotation, node); if (node.value) { @@ -61,11 +68,13 @@ export function ClassMethod(node: Object) { this.printJoin(node.decorators, node); if (node.static) { - this.push("static "); + this.push("static"); + this.push(" "); } if (node.kind === "constructorCall") { - this.push("call "); + this.push("call"); + this.push(" "); } this._method(node); diff --git a/packages/babel-generator/src/generators/expressions.js b/packages/babel-generator/src/generators/expressions.js index 9c42733ca9..b663e905b2 100644 --- a/packages/babel-generator/src/generators/expressions.js +++ b/packages/babel-generator/src/generators/expressions.js @@ -61,7 +61,8 @@ export function ConditionalExpression(node: Object) { } export function NewExpression(node: Object, parent: Object) { - this.push("new "); + this.push("new"); + this.push(" "); this.print(node.callee, node); if (node.arguments.length === 0 && this.format.minified && !t.isCallExpression(parent, { callee: node }) && @@ -92,7 +93,8 @@ export function Decorator(node: Object) { } function commaSeparatorNewline() { - this.push(",\n"); + this.push(","); + this.push("\n"); } export function CallExpression(node: Object) { diff --git a/packages/babel-generator/src/generators/flow.js b/packages/babel-generator/src/generators/flow.js index ed461d8640..d1df30a1aa 100644 --- a/packages/babel-generator/src/generators/flow.js +++ b/packages/babel-generator/src/generators/flow.js @@ -25,36 +25,50 @@ export function NullLiteralTypeAnnotation() { } export function DeclareClass(node: Object) { - this.push("declare class "); + this.push("declare"); + this.push(" "); + this.push("class"); + this.push(" "); this._interfaceish(node); } export function DeclareFunction(node: Object) { - this.push("declare function "); + this.push("declare"); + this.push(" "); + this.push("function"); + this.push(" "); this.print(node.id, node); this.print(node.id.typeAnnotation.typeAnnotation, node); this.semicolon(); } export function DeclareInterface(node: Object) { - this.push("declare "); + this.push("declare"); + this.push(" "); this.InterfaceDeclaration(node); } export function DeclareModule(node: Object) { - this.push("declare module "); + this.push("declare"); + this.push(" "); + this.push("module"); + this.push(" "); this.print(node.id, node); this.space(); this.print(node.body, node); } export function DeclareTypeAlias(node: Object) { - this.push("declare "); + this.push("declare"); + this.push(" "); this.TypeAlias(node); } export function DeclareVariable(node: Object) { - this.push("declare var "); + this.push("declare"); + this.push(" "); + this.push("var"); + this.push(" "); this.print(node.id, node); this.print(node.id.typeAnnotation, node); this.semicolon(); @@ -111,11 +125,15 @@ export function _interfaceish(node: Object) { this.print(node.id, node); this.print(node.typeParameters, node); if (node.extends.length) { - this.push(" extends "); + this.push(" "); + this.push("extends"); + this.push(" "); this.printList(node.extends, node); } if (node.mixins && node.mixins.length) { - this.push(" mixins "); + this.push(" "); + this.push("mixins"); + this.push(" "); this.printList(node.mixins, node); } this.space(); @@ -123,12 +141,15 @@ export function _interfaceish(node: Object) { } export function InterfaceDeclaration(node: Object) { - this.push("interface "); + this.push("interface"); + this.push(" "); this._interfaceish(node); } function andSeparator() { - this.push(" & "); + this.push(" "); + this.push("&"); + this.push(" "); } export function IntersectionTypeAnnotation(node: Object) { @@ -168,12 +189,14 @@ export function TupleTypeAnnotation(node: Object) { } export function TypeofTypeAnnotation(node: Object) { - this.push("typeof "); + this.push("typeof"); + this.push(" "); this.print(node.argument, node); } export function TypeAlias(node: Object) { - this.push("type "); + this.push("type"); + this.push(" "); this.print(node.id, node); this.print(node.typeParameters, node); this.space(); @@ -247,12 +270,18 @@ export function ObjectTypeAnnotation(node: Object) { } export function ObjectTypeCallProperty(node: Object) { - if (node.static) this.push("static "); + if (node.static) { + this.push("static"); + this.push(" "); + } this.print(node.value, node); } export function ObjectTypeIndexer(node: Object) { - if (node.static) this.push("static "); + if (node.static) { + this.push("static"); + this.push(" "); + } this.push("["); this.print(node.id, node); this.push(":"); @@ -265,7 +294,10 @@ export function ObjectTypeIndexer(node: Object) { } export function ObjectTypeProperty(node: Object) { - if (node.static) this.push("static "); + if (node.static) { + this.push("static"); + this.push(" "); + } this.print(node.key, node); if (node.optional) this.push("?"); if (!t.isFunctionTypeAnnotation(node.value)) { @@ -282,7 +314,9 @@ export function QualifiedTypeIdentifier(node: Object) { } function orSeparator() { - this.push(" | "); + this.push(" "); + this.push("|"); + this.push(" "); } export function UnionTypeAnnotation(node: Object) { diff --git a/packages/babel-generator/src/generators/jsx.js b/packages/babel-generator/src/generators/jsx.js index 3dbb72a4ef..4ae406547b 100644 --- a/packages/babel-generator/src/generators/jsx.js +++ b/packages/babel-generator/src/generators/jsx.js @@ -23,7 +23,8 @@ export function JSXMemberExpression(node: Object) { } export function JSXSpreadAttribute(node: Object) { - this.push("{..."); + this.push("{"); + this.push("..."); this.print(node.argument, node); this.push("}"); } @@ -63,7 +64,12 @@ export function JSXOpeningElement(node: Object) { this.push(" "); this.printJoin(node.attributes, node, { separator: spaceSeparator }); } - this.push(node.selfClosing ? " />" : ">"); + if (node.selfClosing) { + this.push(" "); + this.push("/>"); + } else { + this.push(">"); + } } export function JSXClosingElement(node: Object) { diff --git a/packages/babel-generator/src/generators/methods.js b/packages/babel-generator/src/generators/methods.js index 76552736e4..328d45def3 100644 --- a/packages/babel-generator/src/generators/methods.js +++ b/packages/babel-generator/src/generators/methods.js @@ -27,10 +27,14 @@ export function _method(node: Object) { } if (kind === "get" || kind === "set") { - this.push(kind + " "); + this.push(kind); + this.push(" "); } - if (node.async) this.push("async "); + if (node.async) { + this.push("async"); + this.push(" "); + } if (node.computed) { this.push("["); @@ -46,7 +50,10 @@ export function _method(node: Object) { } export function FunctionExpression(node: Object) { - if (node.async) this.push("async "); + if (node.async) { + this.push("async"); + this.push(" "); + } this.push("function"); if (node.generator) this.push("*"); @@ -65,7 +72,10 @@ export function FunctionExpression(node: Object) { export { FunctionExpression as FunctionDeclaration }; export function ArrowFunctionExpression(node: Object) { - if (node.async) this.push("async "); + if (node.async) { + this.push("async"); + this.push(" "); + } if (node.params.length === 1 && t.isIdentifier(node.params[0])) { this.print(node.params[0], node); @@ -73,7 +83,9 @@ export function ArrowFunctionExpression(node: Object) { this._params(node); } - this.push(" => "); + this.push(" "); + this.push("=>"); + this.push(" "); this.print(node.body, node); } diff --git a/packages/babel-generator/src/generators/modules.js b/packages/babel-generator/src/generators/modules.js index 904e7093bf..8dc9a5ff83 100644 --- a/packages/babel-generator/src/generators/modules.js +++ b/packages/babel-generator/src/generators/modules.js @@ -3,7 +3,9 @@ import * as t from "babel-types"; export function ImportSpecifier(node: Object) { this.print(node.imported, node); if (node.local && node.local.name !== node.imported.name) { - this.push(" as "); + this.push(" "); + this.push("as"); + this.push(" "); this.print(node.local, node); } } @@ -19,34 +21,49 @@ export function ExportDefaultSpecifier(node: Object) { export function ExportSpecifier(node: Object) { this.print(node.local, node); if (node.exported && node.local.name !== node.exported.name) { - this.push(" as "); + this.push(" "); + this.push("as"); + this.push(" "); this.print(node.exported, node); } } export function ExportNamespaceSpecifier(node: Object) { - this.push("* as "); + this.push("*"); + this.push(" "); + this.push("as"); + this.push(" "); this.print(node.exported, node); } export function ExportAllDeclaration(node: Object) { - this.push("export *"); + this.push("export"); + this.push(" "); + this.push("*"); if (node.exported) { - this.push(" as "); + this.push(" "); + this.push("as"); + this.push(" "); this.print(node.exported, node); } - this.push(" from "); + this.push(" "); + this.push("from"); + this.push(" "); this.print(node.source, node); this.semicolon(); } export function ExportNamedDeclaration() { - this.push("export "); + this.push("export"); + this.push(" "); ExportDeclaration.apply(this, arguments); } export function ExportDefaultDeclaration() { - this.push("export default "); + this.push("export"); + this.push(" "); + this.push("default"); + this.push(" "); ExportDeclaration.apply(this, arguments); } @@ -57,7 +74,8 @@ function ExportDeclaration(node: Object) { if (!t.isStatement(declar)) this.semicolon(); } else { if (node.exportKind === "type") { - this.push("type "); + this.push("type"); + this.push(" "); } let specifiers = node.specifiers.slice(0); @@ -70,7 +88,8 @@ function ExportDeclaration(node: Object) { hasSpecial = true; this.print(specifiers.shift(), node); if (specifiers.length) { - this.push(", "); + this.push(","); + this.push(" "); } } else { break; @@ -88,7 +107,9 @@ function ExportDeclaration(node: Object) { } if (node.source) { - this.push(" from "); + this.push(" "); + this.push("from"); + this.push(" "); this.print(node.source, node); } @@ -97,10 +118,12 @@ function ExportDeclaration(node: Object) { } export function ImportDeclaration(node: Object) { - this.push("import "); + this.push("import"); + this.push(" "); if (node.importKind === "type" || node.importKind === "typeof") { - this.push(node.importKind + " "); + this.push(node.importKind); + this.push(" "); } let specifiers = node.specifiers.slice(0); @@ -111,7 +134,8 @@ export function ImportDeclaration(node: Object) { if (t.isImportDefaultSpecifier(first) || t.isImportNamespaceSpecifier(first)) { this.print(specifiers.shift(), node); if (specifiers.length) { - this.push(", "); + this.push(","); + this.push(" "); } } else { break; @@ -126,7 +150,9 @@ export function ImportDeclaration(node: Object) { this.push("}"); } - this.push(" from "); + this.push(" "); + this.push("from"); + this.push(" "); } this.print(node.source, node); @@ -134,6 +160,9 @@ export function ImportDeclaration(node: Object) { } export function ImportNamespaceSpecifier(node: Object) { - this.push("* as "); + this.push("*"); + this.push(" "); + this.push("as"); + this.push(" "); this.print(node.local, node); } diff --git a/packages/babel-generator/src/generators/statements.js b/packages/babel-generator/src/generators/statements.js index 7bcc5a57da..1f2ab63f3f 100644 --- a/packages/babel-generator/src/generators/statements.js +++ b/packages/babel-generator/src/generators/statements.js @@ -34,7 +34,8 @@ export function IfStatement(node: Object) { if (node.alternate) { if (this.endsWith("}")) this.space(); - this.push("else "); + this.push("else"); + this.push(" "); this.printAndIndentOnComments(node.alternate, node); } } @@ -82,7 +83,9 @@ let buildForXStatement = function (op) { this.keyword("for"); this.push("("); this.print(node.left, node); - this.push(` ${op} `); + this.push(" "); + this.push(op); + this.push(" "); this.print(node.right, node); this.push(")"); this.printBlock(node); @@ -93,7 +96,8 @@ export let ForInStatement = buildForXStatement("in"); export let ForOfStatement = buildForXStatement("of"); export function DoWhileStatement(node: Object) { - this.push("do "); + this.push("do"); + this.push(" "); this.print(node.body, node); this.space(); this.keyword("while"); @@ -132,7 +136,8 @@ export let ThrowStatement = buildLabelStatement("throw", "argument"); export function LabeledStatement(node: Object) { this.print(node.label, node); - this.push(": "); + this.push(":"); + this.push(" "); this.print(node.body, node); } @@ -152,7 +157,8 @@ export function TryStatement(node: Object) { if (node.finalizer) { this.space(); - this.push("finally "); + this.push("finally"); + this.push(" "); this.print(node.finalizer, node); } } @@ -186,11 +192,13 @@ export function SwitchStatement(node: Object) { export function SwitchCase(node: Object) { if (node.test) { - this.push("case "); + this.push("case"); + this.push(" "); this.print(node.test, node); this.push(":"); } else { - this.push("default:"); + this.push("default"); + this.push(":"); } if (node.consequent.length) { @@ -206,18 +214,21 @@ export function DebuggerStatement() { function variableDeclarationIdent() { // "let " or "var " indentation. - this.push(",\n"); - this.push(" "); + this.push(","); + this.push("\n"); + for (let i = 0; i < 4; i++) this.push(" "); } function constDeclarationIdent() { // "const " indentation. - this.push(",\n"); - this.push(" "); + this.push(","); + this.push("\n"); + for (let i = 0; i < 6; i++) this.push(" "); } export function VariableDeclaration(node: Object, parent: Object) { - this.push(node.kind + " "); + this.push(node.kind); + this.push(" "); let hasInits = false; // don't add whitespace to loop heads