Split all of the pushes into individual tokens.

This commit is contained in:
Logan Smyth
2016-04-30 13:19:21 -07:00
parent 22e2c88269
commit e702e67233
8 changed files with 165 additions and 59 deletions

View File

@@ -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");
}
}
/**

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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