Skip to content

Commit

Permalink
Improve printing of [no LineTerminator here] with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 2, 2022
1 parent 9ad669f commit d8856fe
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 94 deletions.
26 changes: 9 additions & 17 deletions packages/babel-generator/src/generators/expressions.ts
Expand Up @@ -26,14 +26,11 @@ export function UnaryExpression(this: Printer, node: t.UnaryExpression) {
}

export function DoExpression(this: Printer, node: t.DoExpression) {
// ensure no line terminator between `async` and `do`
this.ensureNoLineTerminator(() => {
if (node.async) {
this.word("async");
this.space();
}
this.word("do");
});
if (node.async) {
this.word("async", true);
this.space();
}
this.word("do");
this.space();
this.print(node.body, node);
}
Expand Down Expand Up @@ -230,12 +227,10 @@ export function AwaitExpression(this: Printer, node: t.AwaitExpression) {
}

export function YieldExpression(this: Printer, node: t.YieldExpression) {
this.word("yield");
this.word("yield", node.delegate);

if (node.delegate) {
this.ensureNoLineTerminator(() => {
this.token("*");
});
this.token("*");
if (node.argument) {
this.space();
// line terminators are allowed after yield*
Expand Down Expand Up @@ -360,12 +355,9 @@ export function V8IntrinsicIdentifier(
}

export function ModuleExpression(this: Printer, node: t.ModuleExpression) {
this.word("module");
this.word("module", true);
this.space();
// ensure no line terminator between `module` and `{`
this.ensureNoLineTerminator(() => {
this.token("{");
});
this.token("{");
this.indent();
const { body } = node;
if (body.body.length || body.directives.length) {
Expand Down
36 changes: 13 additions & 23 deletions packages/babel-generator/src/generators/methods.ts
Expand Up @@ -10,6 +10,7 @@ export function _params(
this.token("(");
this._parameters(node.params, node);
this.token(")");
this._noLineTerminator = true;

this.print(node.returnType, node, node.type === "ArrowFunctionExpression");
}
Expand Down Expand Up @@ -72,11 +73,8 @@ export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {
this.space();
}

const { _noLineTerminator } = this;
if (node.async) {
// ensure no line terminator between async and class element name / *
this._noLineTerminator = true;
this.word("async");
this.word("async", true);
this.space();
}

Expand All @@ -87,18 +85,15 @@ export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {
) {
if (node.generator) {
this.token("*");
this._noLineTerminator = _noLineTerminator;
}
}

if (node.computed) {
this.token("[");
this._noLineTerminator = _noLineTerminator;
this.print(key, node);
this.token("]");
} else {
this.print(key, node);
this._noLineTerminator = _noLineTerminator;
}

if (
Expand All @@ -118,13 +113,14 @@ export function _predicate(
| t.FunctionDeclaration
| t.FunctionExpression
| t.ArrowFunctionExpression,
noLineTerminatorAfter?: boolean,
) {
if (node.predicate) {
if (!node.returnType) {
this.token(":");
}
this.space();
this.print(node.predicate, node);
this.print(node.predicate, node, noLineTerminatorAfter);
}
}

Expand Down Expand Up @@ -172,10 +168,8 @@ export function ArrowFunctionExpression(
this: Printer,
node: t.ArrowFunctionExpression,
) {
const { _noLineTerminator } = this;
if (node.async) {
this._noLineTerminator = true;
this.word("async");
this.word("async", true);
this.space();
}

Expand All @@ -188,22 +182,18 @@ export function ArrowFunctionExpression(
isIdentifier((firstParam = node.params[0])) &&
!hasTypesOrComments(node, firstParam)
) {
this.print(firstParam, node);
this._noLineTerminator = _noLineTerminator;
this.print(firstParam, node, true);
} else {
this._noLineTerminator = _noLineTerminator;
this._params(node);
}

this._predicate(node);
this.ensureNoLineTerminator(() => {
this.space();
// When printing (x)/*1*/=>{}, we remove the parentheses
// and thus there aren't two contiguous inner tokens.
// We forcefully print inner comments here.
this.printInnerComments();
this.token("=>");
});
this._predicate(node, true);
this.space();
// When printing (x)/*1*/=>{}, we remove the parentheses
// and thus there aren't two contiguous inner tokens.
// We forcefully print inner comments here.
this.printInnerComments();
this.token("=>");

this.space();

Expand Down
20 changes: 6 additions & 14 deletions packages/babel-generator/src/generators/modules.ts
Expand Up @@ -70,6 +70,7 @@ export function _printAssertions(
this: Printer,
node: Extract<t.Node, { assertions?: t.ImportAttribute[] }>,
) {
this.word("assert");
this.space();
this.token("{");
this.space();
Expand All @@ -94,11 +95,8 @@ export function ExportAllDeclaration(
this.space();
// @ts-expect-error Fixme: assertions is not defined in DeclareExportAllDeclaration
if (node.assertions?.length) {
this.ensureNoLineTerminator(() => {
this.print(node.source, node);
this.space();
this.word("assert");
});
this.print(node.source, node, true);
this.space();
// @ts-expect-error Fixme: assertions is not defined in DeclareExportAllDeclaration
this._printAssertions(node);
} else {
Expand Down Expand Up @@ -169,11 +167,8 @@ export function ExportNamedDeclaration(
this.word("from");
this.space();
if (node.assertions?.length) {
this.ensureNoLineTerminator(() => {
this.print(node.source, node);
this.space();
this.word("assert");
});
this.print(node.source, node, true);
this.space();
this._printAssertions(node);
} else {
this.print(node.source, node);
Expand Down Expand Up @@ -258,10 +253,7 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) {

if (node.assertions?.length) {
this.print(node.source, node, true);
this.ensureNoLineTerminator(() => {
this.space();
this.word("assert");
});
this.space();
this._printAssertions(node);
} else {
this.print(node.source, node);
Expand Down
19 changes: 1 addition & 18 deletions packages/babel-generator/src/generators/statements.ts
@@ -1,5 +1,4 @@
import type Printer from "../printer";
import type { PrintJoinOptions } from "../printer";
import {
isFor,
isForStatement,
Expand Down Expand Up @@ -262,12 +261,7 @@ export function VariableDeclaration(
}

const { kind } = node;
this.word(kind);
const { _noLineTerminator } = this;
if (kind === "using") {
// ensure no line break after `using`
this._noLineTerminator = true;
}
this.word(kind, kind === "using");
this.space();

let hasInits = false;
Expand All @@ -293,24 +287,13 @@ export function VariableDeclaration(
// bar = "foo";
//

let iterator: PrintJoinOptions["iterator"] | undefined;
if (kind === "using") {
// Ensure no line break between `using` and the first declarator
iterator = (_, i: number) => {
if (i === 0) {
this._noLineTerminator = _noLineTerminator;
}
};
}

this.printList(node.declarations, node, {
separator: hasInits
? function (this: Printer) {
this.token(",");
this.newline();
}
: undefined,
iterator,
indent: node.declarations.length > 1 ? true : false,
});

Expand Down

0 comments on commit d8856fe

Please sign in to comment.