Explicitly label standalone tokens and words.

This commit is contained in:
Logan Smyth
2016-04-30 13:45:29 -07:00
parent e702e67233
commit 4286cb4f2a
12 changed files with 245 additions and 220 deletions

View File

@@ -98,7 +98,7 @@ export default class Buffer {
*/
semicolon() {
this.push(";");
this.token(";");
}
/**
@@ -110,7 +110,7 @@ export default class Buffer {
if (this.format.minified && !this._lastPrintedIsEmptyStatement) {
this._removeLast(";");
}
this.push("}");
this.token("}");
}
/**
@@ -118,7 +118,7 @@ export default class Buffer {
*/
keyword(name: string) {
this.push(name);
this.word(name);
this.space();
}
@@ -134,6 +134,23 @@ export default class Buffer {
}
}
/**
* Writes a token that can't be safely parsed without taking whitespace into account.
*/
word(str: string) {
this.push(str);
}
/**
* Writes a simple token.
*/
token(str: string) {
this.push(str);
}
/**
* Remove the last character.
*/
@@ -180,7 +197,7 @@ export default class Buffer {
if (state.printed) {
this.dedent();
this.newline();
this.push(")");
this.token(")");
}
}

View File

@@ -12,7 +12,7 @@ export function Program(node: Object) {
}
export function BlockStatement(node: Object) {
this.push("{");
this.token("{");
this.printInnerComments(node);
if (node.body.length) {
this.newline();
@@ -27,7 +27,7 @@ export function BlockStatement(node: Object) {
this.rightBrace();
} else {
this.source("end", node.loc);
this.push("}");
this.token("}");
}
}

View File

@@ -1,6 +1,6 @@
export function ClassDeclaration(node: Object) {
this.printJoin(node.decorators, node);
this.push("class");
this.word("class");
if (node.id) {
this.push(" ");
@@ -11,7 +11,7 @@ export function ClassDeclaration(node: Object) {
if (node.superClass) {
this.push(" ");
this.push("extends");
this.word("extends");
this.push(" ");
this.print(node.superClass, node);
this.print(node.superTypeParameters, node);
@@ -19,7 +19,7 @@ export function ClassDeclaration(node: Object) {
if (node.implements) {
this.push(" ");
this.push("implements");
this.word("implements");
this.push(" ");
this.printList(node.implements, node);
}
@@ -31,10 +31,10 @@ export function ClassDeclaration(node: Object) {
export { ClassDeclaration as ClassExpression };
export function ClassBody(node: Object) {
this.push("{");
this.token("{");
this.printInnerComments(node);
if (node.body.length === 0) {
this.push("}");
this.token("}");
} else {
this.newline();
@@ -50,14 +50,14 @@ export function ClassProperty(node: Object) {
this.printJoin(node.decorators, node);
if (node.static) {
this.push("static");
this.word("static");
this.push(" ");
}
this.print(node.key, node);
this.print(node.typeAnnotation, node);
if (node.value) {
this.space();
this.push("=");
this.token("=");
this.space();
this.print(node.value, node);
}
@@ -68,12 +68,12 @@ export function ClassMethod(node: Object) {
this.printJoin(node.decorators, node);
if (node.static) {
this.push("static");
this.word("static");
this.push(" ");
}
if (node.kind === "constructorCall") {
this.push("call");
this.word("call");
this.push(" ");
}

View File

@@ -21,47 +21,51 @@ export function UnaryExpression(node: Object) {
needsSpace = false;
}
this.push(node.operator);
if (node.operator === "void" || node.operator === "delete" || node.operator === "typeof") {
this.word(node.operator);
} else {
this.token(node.operator);
}
if (needsSpace) this.push(" ");
this.print(node.argument, node);
}
export function DoExpression(node: Object) {
this.push("do");
this.word("do");
this.space();
this.print(node.body, node);
}
export function ParenthesizedExpression(node: Object) {
this.push("(");
this.token("(");
this.print(node.expression, node);
this.push(")");
this.token(")");
}
export function UpdateExpression(node: Object) {
if (node.prefix) {
this.push(node.operator);
this.token(node.operator);
this.print(node.argument, node);
} else {
this.print(node.argument, node);
this.push(node.operator);
this.token(node.operator);
}
}
export function ConditionalExpression(node: Object) {
this.print(node.test, node);
this.space();
this.push("?");
this.token("?");
this.space();
this.print(node.consequent, node);
this.space();
this.push(":");
this.token(":");
this.space();
this.print(node.alternate, node);
}
export function NewExpression(node: Object, parent: Object) {
this.push("new");
this.word("new");
this.push(" ");
this.print(node.callee, node);
if (node.arguments.length === 0 && this.format.minified &&
@@ -69,9 +73,9 @@ export function NewExpression(node: Object, parent: Object) {
!t.isMemberExpression(parent) &&
!t.isNewExpression(parent)) return;
this.push("(");
this.token("(");
this.printList(node.arguments, node);
this.push(")");
this.token(")");
}
export function SequenceExpression(node: Object) {
@@ -79,21 +83,21 @@ export function SequenceExpression(node: Object) {
}
export function ThisExpression() {
this.push("this");
this.word("this");
}
export function Super() {
this.push("super");
this.word("super");
}
export function Decorator(node: Object) {
this.push("@");
this.token("@");
this.print(node.expression, node);
this.newline();
}
function commaSeparatorNewline() {
this.push(",");
this.token(",");
this.push("\n");
}
@@ -101,7 +105,7 @@ export function CallExpression(node: Object) {
this.print(node.callee, node);
if (node.loc) this.printAuxAfterComment();
this.push("(");
this.token("(");
let isPrettyCall = node._prettyCall && !this.format.retainLines && !this.format.compact;
@@ -119,15 +123,15 @@ export function CallExpression(node: Object) {
this.dedent();
}
this.push(")");
this.token(")");
}
function buildYieldAwait(keyword: string) {
return function (node: Object) {
this.push(keyword);
this.word(keyword);
if (node.delegate) {
this.push("*");
this.token("*");
}
if (node.argument) {
@@ -155,7 +159,7 @@ export function ExpressionStatement(node: Object) {
export function AssignmentPattern(node: Object) {
this.print(node.left, node);
this.space();
this.push("=");
this.token("=");
this.space();
this.print(node.right, node);
}
@@ -167,7 +171,7 @@ export function AssignmentExpression(node: Object, parent: Object) {
!n.needsParens(node, parent);
if (parens) {
this.push("(");
this.token("(");
}
this.print(node.left, node);
@@ -175,7 +179,11 @@ export function AssignmentExpression(node: Object, parent: Object) {
let spaces = !this.format.compact || node.operator === "in" || node.operator === "instanceof";
if (spaces) this.push(" ");
this.push(node.operator);
if (node.operator === "in" || node.operator === "instanceof") {
this.word(node.operator);
} else {
this.token(node.operator);
}
if (!spaces) {
// space is mandatory to avoid outputting <!--
@@ -197,13 +205,13 @@ export function AssignmentExpression(node: Object, parent: Object) {
this.print(node.right, node);
if (parens) {
this.push(")");
this.token(")");
}
}
export function BindExpression(node: Object) {
this.print(node.object, node);
this.push("::");
this.token("::");
this.print(node.callee, node);
}
@@ -225,9 +233,9 @@ export function MemberExpression(node: Object) {
}
if (computed) {
this.push("[");
this.token("[");
this.print(node.property, node);
this.push("]");
this.token("]");
} else {
if (t.isNumericLiteral(node.object)) {
let val = this.getPossibleRaw(node.object) || node.object.value;
@@ -236,18 +244,18 @@ export function MemberExpression(node: Object) {
!SCIENTIFIC_NOTATION.test(val) &&
!ZERO_DECIMAL_INTEGER.test(val) &&
!this.endsWith(".")) {
this.push(".");
this.token(".");
}
}
this.push(".");
this.token(".");
this.print(node.property, node);
}
}
export function MetaProperty(node: Object) {
this.print(node.meta, node);
this.push(".");
this.token(".");
this.print(node.property, node);
}

View File

@@ -3,39 +3,39 @@
import * as t from "babel-types";
export function AnyTypeAnnotation() {
this.push("any");
this.word("any");
}
export function ArrayTypeAnnotation(node: Object) {
this.print(node.elementType, node);
this.push("[");
this.push("]");
this.token("[");
this.token("]");
}
export function BooleanTypeAnnotation() {
this.push("bool");
this.word("bool");
}
export function BooleanLiteralTypeAnnotation(node: Object) {
this.push(node.value ? "true" : "false");
this.word(node.value ? "true" : "false");
}
export function NullLiteralTypeAnnotation() {
this.push("null");
this.word("null");
}
export function DeclareClass(node: Object) {
this.push("declare");
this.word("declare");
this.push(" ");
this.push("class");
this.word("class");
this.push(" ");
this._interfaceish(node);
}
export function DeclareFunction(node: Object) {
this.push("declare");
this.word("declare");
this.push(" ");
this.push("function");
this.word("function");
this.push(" ");
this.print(node.id, node);
this.print(node.id.typeAnnotation.typeAnnotation, node);
@@ -43,15 +43,15 @@ export function DeclareFunction(node: Object) {
}
export function DeclareInterface(node: Object) {
this.push("declare");
this.word("declare");
this.push(" ");
this.InterfaceDeclaration(node);
}
export function DeclareModule(node: Object) {
this.push("declare");
this.word("declare");
this.push(" ");
this.push("module");
this.word("module");
this.push(" ");
this.print(node.id, node);
this.space();
@@ -59,15 +59,15 @@ export function DeclareModule(node: Object) {
}
export function DeclareTypeAlias(node: Object) {
this.push("declare");
this.word("declare");
this.push(" ");
this.TypeAlias(node);
}
export function DeclareVariable(node: Object) {
this.push("declare");
this.word("declare");
this.push(" ");
this.push("var");
this.word("var");
this.push(" ");
this.print(node.id, node);
this.print(node.id.typeAnnotation, node);
@@ -75,31 +75,31 @@ export function DeclareVariable(node: Object) {
}
export function ExistentialTypeParam() {
this.push("*");
this.token("*");
}
export function FunctionTypeAnnotation(node: Object, parent: Object) {
this.print(node.typeParameters, node);
this.push("(");
this.token("(");
this.printList(node.params, node);
if (node.rest) {
if (node.params.length) {
this.push(",");
this.token(",");
this.space();
}
this.push("...");
this.token("...");
this.print(node.rest, node);
}
this.push(")");
this.token(")");
// this node type is overloaded, not sure why but it makes it EXTREMELY annoying
if (parent.type === "ObjectTypeProperty" || parent.type === "ObjectTypeCallProperty" || parent.type === "DeclareFunction") {
this.push(":");
this.token(":");
} else {
this.space();
this.push("=>");
this.token("=>");
}
this.space();
@@ -108,8 +108,8 @@ export function FunctionTypeAnnotation(node: Object, parent: Object) {
export function FunctionTypeParam(node: Object) {
this.print(node.name, node);
if (node.optional) this.push("?");
this.push(":");
if (node.optional) this.token("?");
this.token(":");
this.space();
this.print(node.typeAnnotation, node);
}
@@ -126,13 +126,13 @@ export function _interfaceish(node: Object) {
this.print(node.typeParameters, node);
if (node.extends.length) {
this.push(" ");
this.push("extends");
this.word("extends");
this.push(" ");
this.printList(node.extends, node);
}
if (node.mixins && node.mixins.length) {
this.push(" ");
this.push("mixins");
this.word("mixins");
this.push(" ");
this.printList(node.mixins, node);
}
@@ -141,14 +141,14 @@ export function _interfaceish(node: Object) {
}
export function InterfaceDeclaration(node: Object) {
this.push("interface");
this.word("interface");
this.push(" ");
this._interfaceish(node);
}
function andSeparator() {
this.push(" ");
this.push("&");
this.token("&");
this.push(" ");
}
@@ -157,11 +157,11 @@ export function IntersectionTypeAnnotation(node: Object) {
}
export function MixedTypeAnnotation() {
this.push("mixed");
this.word("mixed");
}
export function NullableTypeAnnotation(node: Object) {
this.push("?");
this.token("?");
this.print(node.typeAnnotation, node);
}
@@ -171,56 +171,56 @@ export {
} from "./types";
export function NumberTypeAnnotation() {
this.push("number");
this.word("number");
}
export function StringTypeAnnotation() {
this.push("string");
this.word("string");
}
export function ThisTypeAnnotation() {
this.push("this");
this.word("this");
}
export function TupleTypeAnnotation(node: Object) {
this.push("[");
this.token("[");
this.printList(node.types, node);
this.push("]");
this.token("]");
}
export function TypeofTypeAnnotation(node: Object) {
this.push("typeof");
this.word("typeof");
this.push(" ");
this.print(node.argument, node);
}
export function TypeAlias(node: Object) {
this.push("type");
this.word("type");
this.push(" ");
this.print(node.id, node);
this.print(node.typeParameters, node);
this.space();
this.push("=");
this.token("=");
this.space();
this.print(node.right, node);
this.semicolon();
}
export function TypeAnnotation(node: Object) {
this.push(":");
this.token(":");
this.space();
if (node.optional) this.push("?");
if (node.optional) this.token("?");
this.print(node.typeAnnotation, node);
}
export function TypeParameter(node: Object) {
if (node.variance === "plus") {
this.push("+");
this.token("+");
} else if (node.variance === "minus") {
this.push("-");
this.token("-");
}
this.push(node.name);
this.word(node.name);
if (node.bound) {
this.print(node.bound, node);
@@ -228,26 +228,26 @@ export function TypeParameter(node: Object) {
if (node.default) {
this.space();
this.push("=");
this.token("=");
this.space();
this.print(node.default, node);
}
}
export function TypeParameterInstantiation(node: Object) {
this.push("<");
this.token("<");
this.printList(node.params, node, {
iterator: (node: Object) => {
this.print(node.typeAnnotation, node);
}
});
this.push(">");
this.token(">");
}
export { TypeParameterInstantiation as TypeParameterDeclaration };
export function ObjectTypeAnnotation(node: Object) {
this.push("{");
this.token("{");
let props = node.properties.concat(node.callProperties, node.indexers);
if (props.length) {
@@ -266,12 +266,12 @@ export function ObjectTypeAnnotation(node: Object) {
this.space();
}
this.push("}");
this.token("}");
}
export function ObjectTypeCallProperty(node: Object) {
if (node.static) {
this.push("static");
this.word("static");
this.push(" ");
}
this.print(node.value, node);
@@ -279,29 +279,29 @@ export function ObjectTypeCallProperty(node: Object) {
export function ObjectTypeIndexer(node: Object) {
if (node.static) {
this.push("static");
this.word("static");
this.push(" ");
}
this.push("[");
this.token("[");
this.print(node.id, node);
this.push(":");
this.token(":");
this.space();
this.print(node.key, node);
this.push("]");
this.push(":");
this.token("]");
this.token(":");
this.space();
this.print(node.value, node);
}
export function ObjectTypeProperty(node: Object) {
if (node.static) {
this.push("static");
this.word("static");
this.push(" ");
}
this.print(node.key, node);
if (node.optional) this.push("?");
if (node.optional) this.token("?");
if (!t.isFunctionTypeAnnotation(node.value)) {
this.push(":");
this.token(":");
this.space();
}
this.print(node.value, node);
@@ -309,13 +309,13 @@ export function ObjectTypeProperty(node: Object) {
export function QualifiedTypeIdentifier(node: Object) {
this.print(node.qualification, node);
this.push(".");
this.token(".");
this.print(node.id, node);
}
function orSeparator() {
this.push(" ");
this.push("|");
this.token("|");
this.push(" ");
}
@@ -324,12 +324,12 @@ export function UnionTypeAnnotation(node: Object) {
}
export function TypeCastExpression(node: Object) {
this.push("(");
this.token("(");
this.print(node.expression, node);
this.print(node.typeAnnotation, node);
this.push(")");
this.token(")");
}
export function VoidTypeAnnotation() {
this.push("void");
this.word("void");
}

View File

@@ -1,42 +1,42 @@
export function JSXAttribute(node: Object) {
this.print(node.name, node);
if (node.value) {
this.push("=");
this.token("=");
this.print(node.value, node);
}
}
export function JSXIdentifier(node: Object) {
this.push(node.name);
this.word(node.name);
}
export function JSXNamespacedName(node: Object) {
this.print(node.namespace, node);
this.push(":");
this.token(":");
this.print(node.name, node);
}
export function JSXMemberExpression(node: Object) {
this.print(node.object, node);
this.push(".");
this.token(".");
this.print(node.property, node);
}
export function JSXSpreadAttribute(node: Object) {
this.push("{");
this.push("...");
this.token("{");
this.token("...");
this.print(node.argument, node);
this.push("}");
this.token("}");
}
export function JSXExpressionContainer(node: Object) {
this.push("{");
this.token("{");
this.print(node.expression, node);
this.push("}");
this.token("}");
}
export function JSXText(node: Object) {
this.push(node.value);
this.token(node.value);
}
export function JSXElement(node: Object) {
@@ -58,7 +58,7 @@ function spaceSeparator() {
}
export function JSXOpeningElement(node: Object) {
this.push("<");
this.token("<");
this.print(node.name, node);
if (node.attributes.length > 0) {
this.push(" ");
@@ -66,16 +66,16 @@ export function JSXOpeningElement(node: Object) {
}
if (node.selfClosing) {
this.push(" ");
this.push("/>");
this.token("/>");
} else {
this.push(">");
this.token(">");
}
}
export function JSXClosingElement(node: Object) {
this.push("</");
this.token("</");
this.print(node.name, node);
this.push(">");
this.token(">");
}
export function JSXEmptyExpression() {}

View File

@@ -2,14 +2,14 @@ import * as t from "babel-types";
export function _params(node: Object) {
this.print(node.typeParameters, node);
this.push("(");
this.token("(");
this.printList(node.params, node, {
iterator: (node) => {
if (node.optional) this.push("?");
if (node.optional) this.token("?");
this.print(node.typeAnnotation, node);
}
});
this.push(")");
this.token(")");
if (node.returnType) {
this.print(node.returnType, node);
@@ -22,24 +22,24 @@ export function _method(node: Object) {
if (kind === "method" || kind === "init") {
if (node.generator) {
this.push("*");
this.token("*");
}
}
if (kind === "get" || kind === "set") {
this.push(kind);
this.word(kind);
this.push(" ");
}
if (node.async) {
this.push("async");
this.word("async");
this.push(" ");
}
if (node.computed) {
this.push("[");
this.token("[");
this.print(key, node);
this.push("]");
this.token("]");
} else {
this.print(key, node);
}
@@ -51,11 +51,11 @@ export function _method(node: Object) {
export function FunctionExpression(node: Object) {
if (node.async) {
this.push("async");
this.word("async");
this.push(" ");
}
this.push("function");
if (node.generator) this.push("*");
this.word("function");
if (node.generator) this.token("*");
if (node.id) {
this.push(" ");
@@ -73,7 +73,7 @@ export { FunctionExpression as FunctionDeclaration };
export function ArrowFunctionExpression(node: Object) {
if (node.async) {
this.push("async");
this.word("async");
this.push(" ");
}
@@ -84,7 +84,7 @@ export function ArrowFunctionExpression(node: Object) {
}
this.push(" ");
this.push("=>");
this.token("=>");
this.push(" ");
this.print(node.body, node);

View File

@@ -4,7 +4,7 @@ export function ImportSpecifier(node: Object) {
this.print(node.imported, node);
if (node.local && node.local.name !== node.imported.name) {
this.push(" ");
this.push("as");
this.word("as");
this.push(" ");
this.print(node.local, node);
}
@@ -22,47 +22,47 @@ export function ExportSpecifier(node: Object) {
this.print(node.local, node);
if (node.exported && node.local.name !== node.exported.name) {
this.push(" ");
this.push("as");
this.word("as");
this.push(" ");
this.print(node.exported, node);
}
}
export function ExportNamespaceSpecifier(node: Object) {
this.push("*");
this.token("*");
this.push(" ");
this.push("as");
this.word("as");
this.push(" ");
this.print(node.exported, node);
}
export function ExportAllDeclaration(node: Object) {
this.push("export");
this.word("export");
this.push(" ");
this.push("*");
this.token("*");
if (node.exported) {
this.push(" ");
this.push("as");
this.word("as");
this.push(" ");
this.print(node.exported, node);
}
this.push(" ");
this.push("from");
this.word("from");
this.push(" ");
this.print(node.source, node);
this.semicolon();
}
export function ExportNamedDeclaration() {
this.push("export");
this.word("export");
this.push(" ");
ExportDeclaration.apply(this, arguments);
}
export function ExportDefaultDeclaration() {
this.push("export");
this.word("export");
this.push(" ");
this.push("default");
this.word("default");
this.push(" ");
ExportDeclaration.apply(this, arguments);
}
@@ -74,7 +74,7 @@ function ExportDeclaration(node: Object) {
if (!t.isStatement(declar)) this.semicolon();
} else {
if (node.exportKind === "type") {
this.push("type");
this.word("type");
this.push(" ");
}
@@ -88,7 +88,7 @@ function ExportDeclaration(node: Object) {
hasSpecial = true;
this.print(specifiers.shift(), node);
if (specifiers.length) {
this.push(",");
this.token(",");
this.push(" ");
}
} else {
@@ -97,18 +97,18 @@ function ExportDeclaration(node: Object) {
}
if (specifiers.length || (!specifiers.length && !hasSpecial)) {
this.push("{");
this.token("{");
if (specifiers.length) {
this.space();
this.printList(specifiers, node);
this.space();
}
this.push("}");
this.token("}");
}
if (node.source) {
this.push(" ");
this.push("from");
this.word("from");
this.push(" ");
this.print(node.source, node);
}
@@ -118,11 +118,11 @@ function ExportDeclaration(node: Object) {
}
export function ImportDeclaration(node: Object) {
this.push("import");
this.word("import");
this.push(" ");
if (node.importKind === "type" || node.importKind === "typeof") {
this.push(node.importKind);
this.word(node.importKind);
this.push(" ");
}
@@ -134,7 +134,7 @@ export function ImportDeclaration(node: Object) {
if (t.isImportDefaultSpecifier(first) || t.isImportNamespaceSpecifier(first)) {
this.print(specifiers.shift(), node);
if (specifiers.length) {
this.push(",");
this.token(",");
this.push(" ");
}
} else {
@@ -143,15 +143,15 @@ export function ImportDeclaration(node: Object) {
}
if (specifiers.length) {
this.push("{");
this.token("{");
this.space();
this.printList(specifiers, node);
this.space();
this.push("}");
this.token("}");
}
this.push(" ");
this.push("from");
this.word("from");
this.push(" ");
}
@@ -160,9 +160,9 @@ export function ImportDeclaration(node: Object) {
}
export function ImportNamespaceSpecifier(node: Object) {
this.push("*");
this.token("*");
this.push(" ");
this.push("as");
this.word("as");
this.push(" ");
this.print(node.local, node);
}

View File

@@ -4,22 +4,22 @@ const NON_ALPHABETIC_UNARY_OPERATORS = t.UPDATE_OPERATORS.concat(t.NUMBER_UNARY_
export function WithStatement(node: Object) {
this.keyword("with");
this.push("(");
this.token("(");
this.print(node.object, node);
this.push(")");
this.token(")");
this.printBlock(node);
}
export function IfStatement(node: Object) {
this.keyword("if");
this.push("(");
this.token("(");
this.print(node.test, node);
this.push(")");
this.token(")");
this.space();
let needsBlock = node.alternate && t.isIfStatement(getLastStatement(node.consequent));
if (needsBlock) {
this.push("{");
this.token("{");
this.newline();
this.indent();
}
@@ -29,12 +29,12 @@ export function IfStatement(node: Object) {
if (needsBlock) {
this.dedent();
this.newline();
this.push("}");
this.token("}");
}
if (node.alternate) {
if (this.endsWith("}")) this.space();
this.push("else");
this.word("else");
this.push(" ");
this.printAndIndentOnComments(node.alternate, node);
}
@@ -48,46 +48,46 @@ function getLastStatement(statement) {
export function ForStatement(node: Object) {
this.keyword("for");
this.push("(");
this.token("(");
this._inForStatementInitCounter++;
this.print(node.init, node);
this._inForStatementInitCounter--;
this.push(";");
this.token(";");
if (node.test) {
this.space();
this.print(node.test, node);
}
this.push(";");
this.token(";");
if (node.update) {
this.space();
this.print(node.update, node);
}
this.push(")");
this.token(")");
this.printBlock(node);
}
export function WhileStatement(node: Object) {
this.keyword("while");
this.push("(");
this.token("(");
this.print(node.test, node);
this.push(")");
this.token(")");
this.printBlock(node);
}
let buildForXStatement = function (op) {
return function (node: Object) {
this.keyword("for");
this.push("(");
this.token("(");
this.print(node.left, node);
this.push(" ");
this.push(op);
this.word(op);
this.push(" ");
this.print(node.right, node);
this.push(")");
this.token(")");
this.printBlock(node);
};
};
@@ -96,20 +96,20 @@ export let ForInStatement = buildForXStatement("in");
export let ForOfStatement = buildForXStatement("of");
export function DoWhileStatement(node: Object) {
this.push("do");
this.word("do");
this.push(" ");
this.print(node.body, node);
this.space();
this.keyword("while");
this.push("(");
this.token("(");
this.print(node.test, node);
this.push(")");
this.token(")");
this.semicolon();
}
function buildLabelStatement(prefix, key = "label") {
return function (node: Object) {
this.push(prefix);
this.word(prefix);
let label = node[key];
if (label) {
@@ -136,7 +136,7 @@ export let ThrowStatement = buildLabelStatement("throw", "argument");
export function LabeledStatement(node: Object) {
this.print(node.label, node);
this.push(":");
this.token(":");
this.push(" ");
this.print(node.body, node);
}
@@ -157,7 +157,7 @@ export function TryStatement(node: Object) {
if (node.finalizer) {
this.space();
this.push("finally");
this.word("finally");
this.push(" ");
this.print(node.finalizer, node);
}
@@ -165,20 +165,20 @@ export function TryStatement(node: Object) {
export function CatchClause(node: Object) {
this.keyword("catch");
this.push("(");
this.token("(");
this.print(node.param, node);
this.push(")");
this.token(")");
this.space();
this.print(node.body, node);
}
export function SwitchStatement(node: Object) {
this.keyword("switch");
this.push("(");
this.token("(");
this.print(node.discriminant, node);
this.push(")");
this.token(")");
this.space();
this.push("{");
this.token("{");
this.printSequence(node.cases, node, {
indent: true,
@@ -187,18 +187,18 @@ export function SwitchStatement(node: Object) {
}
});
this.push("}");
this.token("}");
}
export function SwitchCase(node: Object) {
if (node.test) {
this.push("case");
this.word("case");
this.push(" ");
this.print(node.test, node);
this.push(":");
this.token(":");
} else {
this.push("default");
this.push(":");
this.word("default");
this.token(":");
}
if (node.consequent.length) {
@@ -208,26 +208,26 @@ export function SwitchCase(node: Object) {
}
export function DebuggerStatement() {
this.push("debugger");
this.word("debugger");
this.semicolon();
}
function variableDeclarationIdent() {
// "let " or "var " indentation.
this.push(",");
this.token(",");
this.push("\n");
for (let i = 0; i < 4; i++) this.push(" ");
}
function constDeclarationIdent() {
// "const " indentation.
this.push(",");
this.token(",");
this.push("\n");
for (let i = 0; i < 6; i++) this.push(" ");
}
export function VariableDeclaration(node: Object, parent: Object) {
this.push(node.kind);
this.word(node.kind);
this.push(" ");
let hasInits = false;
@@ -275,7 +275,7 @@ export function VariableDeclarator(node: Object) {
this.print(node.id.typeAnnotation, node);
if (node.init) {
this.space();
this.push("=");
this.token("=");
this.space();
this.print(node.init, node);
}

View File

@@ -10,7 +10,7 @@ export function TemplateElement(node: Object, parent: Object) {
let value = (isFirst ? "`" : "}") + node.value.raw + (isLast ? "`" : "${");
if (!isFirst) this.push(" ");
this.push(value);
this.token(value);
if (!isLast) this.push(" ");
}

View File

@@ -10,17 +10,17 @@ export function Identifier(node: Object) {
// the next major.
if (node.variance) {
if (node.variance === "plus") {
this.push("+");
this.token("+");
} else if (node.variance === "minus") {
this.push("-");
this.token("-");
}
}
this.push(node.name);
this.word(node.name);
}
export function RestElement(node: Object) {
this.push("...");
this.token("...");
this.print(node.argument, node);
}
@@ -33,7 +33,7 @@ export {
export function ObjectExpression(node: Object) {
let props = node.properties;
this.push("{");
this.token("{");
this.printInnerComments(node);
if (props.length) {
@@ -42,7 +42,7 @@ export function ObjectExpression(node: Object) {
this.space();
}
this.push("}");
this.token("}");
}
export { ObjectExpression as ObjectPattern };
@@ -56,9 +56,9 @@ export function ObjectProperty(node: Object) {
this.printJoin(node.decorators, node);
if (node.computed) {
this.push("[");
this.token("[");
this.print(node.key, node);
this.push("]");
this.token("]");
} else {
// print `({ foo: foo = 5 } = {})` as `({ foo = 5 } = {});`
if (t.isAssignmentPattern(node.value) && t.isIdentifier(node.key) && node.key.name === node.value.left.name) {
@@ -77,7 +77,7 @@ export function ObjectProperty(node: Object) {
}
}
this.push(":");
this.token(":");
this.space();
this.print(node.value, node);
}
@@ -86,7 +86,7 @@ export function ArrayExpression(node: Object) {
let elems = node.elements;
let len = elems.length;
this.push("[");
this.token("[");
this.printInnerComments(node);
for (let i = 0; i < elems.length; i++) {
@@ -94,48 +94,48 @@ export function ArrayExpression(node: Object) {
if (elem) {
if (i > 0) this.space();
this.print(elem, node);
if (i < len - 1) this.push(",");
if (i < len - 1) this.token(",");
} else {
// If the array expression ends with a hole, that hole
// will be ignored by the interpreter, but if it ends with
// two (or more) holes, we need to write out two (or more)
// commas so that the resulting code is interpreted with
// both (all) of the holes.
this.push(",");
this.token(",");
}
}
this.push("]");
this.token("]");
}
export { ArrayExpression as ArrayPattern };
export function RegExpLiteral(node: Object) {
this.push(`/${node.pattern}/${node.flags}`);
this.word(`/${node.pattern}/${node.flags}`);
}
export function BooleanLiteral(node: Object) {
this.push(node.value ? "true" : "false");
this.word(node.value ? "true" : "false");
}
export function NullLiteral() {
this.push("null");
this.word("null");
}
export function NumericLiteral(node: Object) {
let raw = this.getPossibleRaw(node);
if (raw != null) {
this.push(raw);
this.word(raw);
return;
}
this.push(node.value + "");
this.word(node.value + "");
}
export function StringLiteral(node: Object, parent: Object) {
let raw = this.getPossibleRaw(node);
if (raw != null) {
this.push(raw);
this.token(raw);
return;
}
@@ -160,5 +160,5 @@ export function StringLiteral(node: Object, parent: Object) {
val = `'${val}'`;
}
return this.push(val);
return this.token(val);
}

View File

@@ -41,7 +41,7 @@ export default class Printer extends Buffer {
this.printAuxBeforeComment(oldInAux);
let needsParens = n.needsParens(node, parent, this._printStack);
if (needsParens) this.push("(");
if (needsParens) this.token("(");
this.printLeadingComments(node, parent);
@@ -61,7 +61,7 @@ export default class Printer extends Buffer {
this.printTrailingComments(node, parent);
if (needsParens) this.push(")");
if (needsParens) this.token(")");
// end
this._printStack.pop();
@@ -312,7 +312,7 @@ export default class Printer extends Buffer {
}
function commaSeparator() {
this.push(",");
this.token(",");
this.space();
}