diff --git a/packages/babel-generator/src/buffer.js b/packages/babel-generator/src/buffer.js index e9eaeb4dcd..bd9dc55fac 100644 --- a/packages/babel-generator/src/buffer.js +++ b/packages/babel-generator/src/buffer.js @@ -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(")"); } } diff --git a/packages/babel-generator/src/generators/base.js b/packages/babel-generator/src/generators/base.js index c5063741a2..6765bc762e 100644 --- a/packages/babel-generator/src/generators/base.js +++ b/packages/babel-generator/src/generators/base.js @@ -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("}"); } } diff --git a/packages/babel-generator/src/generators/classes.js b/packages/babel-generator/src/generators/classes.js index 5c418ff97c..a7f6675baa 100644 --- a/packages/babel-generator/src/generators/classes.js +++ b/packages/babel-generator/src/generators/classes.js @@ -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(" "); } diff --git a/packages/babel-generator/src/generators/expressions.js b/packages/babel-generator/src/generators/expressions.js index b663e905b2..a9cbce45f4 100644 --- a/packages/babel-generator/src/generators/expressions.js +++ b/packages/babel-generator/src/generators/expressions.js @@ -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