Split all of the separators into functions.

This commit is contained in:
Logan Smyth 2016-04-25 22:13:06 -07:00
parent 3e1a661eb6
commit 8336aa52e8
5 changed files with 40 additions and 11 deletions

View File

@ -91,6 +91,10 @@ export function Decorator(node: Object) {
this.newline(); this.newline();
} }
function commaSeparatorNewline() {
this.push(",\n");
}
export function CallExpression(node: Object) { export function CallExpression(node: Object) {
this.print(node.callee, node); this.print(node.callee, node);
if (node.loc) this.printAuxAfterComment(); if (node.loc) this.printAuxAfterComment();
@ -101,7 +105,7 @@ export function CallExpression(node: Object) {
let separator; let separator;
if (isPrettyCall) { if (isPrettyCall) {
separator = ",\n"; separator = commaSeparatorNewline;
this.newline(); this.newline();
this.indent(); this.indent();
} }

View File

@ -127,8 +127,12 @@ export function InterfaceDeclaration(node: Object) {
this._interfaceish(node); this._interfaceish(node);
} }
function andSeparator() {
this.push(" & ");
}
export function IntersectionTypeAnnotation(node: Object) { export function IntersectionTypeAnnotation(node: Object) {
this.printJoin(node.types, node, { separator: " & " }); this.printJoin(node.types, node, { separator: andSeparator });
} }
export function MixedTypeAnnotation() { export function MixedTypeAnnotation() {
@ -277,8 +281,12 @@ export function QualifiedTypeIdentifier(node: Object) {
this.print(node.id, node); this.print(node.id, node);
} }
function orSeparator() {
this.push(" | ");
}
export function UnionTypeAnnotation(node: Object) { export function UnionTypeAnnotation(node: Object) {
this.printJoin(node.types, node, { separator: " | " }); this.printJoin(node.types, node, { separator: orSeparator });
} }
export function TypeCastExpression(node: Object) { export function TypeCastExpression(node: Object) {

View File

@ -52,12 +52,16 @@ export function JSXElement(node: Object) {
this.print(node.closingElement, node); this.print(node.closingElement, node);
} }
function spaceSeparator() {
this.push(" ");
}
export function JSXOpeningElement(node: Object) { export function JSXOpeningElement(node: Object) {
this.push("<"); this.push("<");
this.print(node.name, node); this.print(node.name, node);
if (node.attributes.length > 0) { if (node.attributes.length > 0) {
this.push(" "); this.push(" ");
this.printJoin(node.attributes, node, { separator: " " }); this.printJoin(node.attributes, node, { separator: spaceSeparator });
} }
this.push(node.selfClosing ? " />" : ">"); this.push(node.selfClosing ? " />" : ">");
} }

View File

@ -1,4 +1,3 @@
import repeat from "lodash/repeat";
import * as t from "babel-types"; import * as t from "babel-types";
const NON_ALPHABETIC_UNARY_OPERATORS = t.UPDATE_OPERATORS.concat(t.NUMBER_UNARY_OPERATORS).concat(["!"]); const NON_ALPHABETIC_UNARY_OPERATORS = t.UPDATE_OPERATORS.concat(t.NUMBER_UNARY_OPERATORS).concat(["!"]);
@ -205,6 +204,16 @@ export function DebuggerStatement() {
this.semicolon(); this.semicolon();
} }
function variableDeclarationIdent() {
// "let " or "var " indentation.
this.push(",\n ");
}
function constDeclarationIdent() {
// "const " indentation.
this.push(",\n ");
}
export function VariableDeclaration(node: Object, parent: Object) { export function VariableDeclaration(node: Object, parent: Object) {
this.push(node.kind + " "); this.push(node.kind + " ");
@ -231,14 +240,14 @@ export function VariableDeclaration(node: Object, parent: Object) {
// bar = "foo"; // bar = "foo";
// //
let sep; let separator;
if (!this.format.compact && !this.format.concise && hasInits && !this.format.retainLines) { if (!this.format.compact && !this.format.concise && hasInits && !this.format.retainLines) {
sep = `,\n${repeat(" ", node.kind.length + 1)}`; separator = node.kind === "const" ? constDeclarationIdent : variableDeclarationIdent;
} }
// //
this.printList(node.declarations, node, { separator: sep }); this.printList(node.declarations, node, { separator });
if (t.isFor(parent)) { if (t.isFor(parent)) {
// don't give semicolons to these nodes since they'll be inserted in the parent generator // don't give semicolons to these nodes since they'll be inserted in the parent generator

View File

@ -125,7 +125,7 @@ export default class Printer extends Buffer {
} }
if (opts.separator && i < len - 1) { if (opts.separator && i < len - 1) {
this.push(opts.separator); opts.separator.call(this);
} }
} }
}; };
@ -187,8 +187,7 @@ export default class Printer extends Buffer {
printList(items, parent, opts = {}) { printList(items, parent, opts = {}) {
if (opts.separator == null) { if (opts.separator == null) {
opts.separator = ","; opts.separator = commaSeparator;
if (!this.format.compact) opts.separator += " ";
} }
return this.printJoin(items, parent, opts); return this.printJoin(items, parent, opts);
@ -316,6 +315,11 @@ export default class Printer extends Buffer {
} }
} }
function commaSeparator() {
this.push(",");
this.space();
}
for (let generator of [ for (let generator of [
require("./generators/template-literals"), require("./generators/template-literals"),
require("./generators/expressions"), require("./generators/expressions"),