Skip to content

Commit

Permalink
Automatically print inner comments (#15080)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 28, 2022
1 parent 3e094bd commit bea7a43
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 72 deletions.
6 changes: 4 additions & 2 deletions packages/babel-generator/src/generators/base.ts
Expand Up @@ -12,7 +12,10 @@ export function File(this: Printer, node: t.File) {
}

export function Program(this: Printer, node: t.Program) {
this.printInnerComments(node, false);
// An empty Program doesn't have any inner tokens, so
// we must explicitly print its inner comments.
this.noIndentInnerCommentsHere();
this.printInnerComments();

const directivesLen = node.directives?.length;
if (directivesLen) {
Expand All @@ -30,7 +33,6 @@ export function Program(this: Printer, node: t.Program) {

export function BlockStatement(this: Printer, node: t.BlockStatement) {
this.token("{");
this.printInnerComments(node);

const directivesLen = node.directives?.length;
if (directivesLen) {
Expand Down
3 changes: 0 additions & 3 deletions packages/babel-generator/src/generators/classes.ts
Expand Up @@ -35,7 +35,6 @@ export function ClassDeclaration(
}

this.word("class");
this.printInnerComments(node);

if (node.id) {
this.space();
Expand Down Expand Up @@ -67,7 +66,6 @@ export { ClassDeclaration as ClassExpression };

export function ClassBody(this: Printer, node: t.ClassBody) {
this.token("{");
this.printInnerComments(node);
if (node.body.length === 0) {
this.token("}");
} else {
Expand Down Expand Up @@ -137,7 +135,6 @@ export function ClassAccessorProperty(
this.tsPrintClassMemberModifiers(node);

this.word("accessor");
this.printInnerComments(node);
this.space();

if (node.computed) {
Expand Down
22 changes: 10 additions & 12 deletions packages/babel-generator/src/generators/expressions.ts
Expand Up @@ -26,14 +26,14 @@ export function UnaryExpression(this: Printer, node: t.UnaryExpression) {
}

export function DoExpression(this: Printer, node: t.DoExpression) {
if (node.async) {
this.word("async");
this.ensureNoLineTerminator(() => {
this.printInnerComments(node);
// ensure no line terminator between `async` and `do`
this.ensureNoLineTerminator(() => {
if (node.async) {
this.word("async");
this.space();
});
}
this.word("do");
}
this.word("do");
});
this.space();
this.print(node.body, node);
}
Expand Down Expand Up @@ -234,9 +234,8 @@ export function YieldExpression(this: Printer, node: t.YieldExpression) {

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

export function ModuleExpression(this: Printer, node: t.ModuleExpression) {
this.word("module");
this.space();
// ensure no line terminator between `module` and `{`
this.ensureNoLineTerminator(() => {
this.printInnerComments(node);
this.space();
this.token("{");
});
this.token("{");
this.indent();
const { body } = node;
if (body.body.length || body.directives.length) {
Expand Down
5 changes: 3 additions & 2 deletions packages/babel-generator/src/generators/jsx.ts
Expand Up @@ -101,8 +101,9 @@ export function JSXClosingElement(this: Printer, node: t.JSXClosingElement) {
this.token(">");
}

export function JSXEmptyExpression(this: Printer, node: t.JSXEmptyExpression) {
this.printInnerComments(node);
export function JSXEmptyExpression(this: Printer) {
// This node is empty, so forcefully print its inner comments.
this.printInnerComments();
}

export function JSXFragment(this: Printer, node: t.JSXFragment) {
Expand Down
25 changes: 15 additions & 10 deletions packages/babel-generator/src/generators/methods.ts
Expand Up @@ -34,9 +34,6 @@ export function _parameters(
this.space();
}
}
if (paramLength === 0) {
this.printInnerComments(parent);
}
}

export function _param(
Expand Down Expand Up @@ -89,9 +86,6 @@ export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {
kind === "init"
) {
if (node.generator) {
if (node.async) {
this.printInnerComments(node);
}
this.token("*");
this._noLineTerminator = _noLineTerminator;
}
Expand All @@ -102,7 +96,6 @@ export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {
this._noLineTerminator = _noLineTerminator;
this.print(key, node);
this.token("]");
this.printInnerComments(node);
} else {
this.print(key, node);
this._noLineTerminator = _noLineTerminator;
Expand Down Expand Up @@ -141,11 +134,20 @@ export function _functionHead(
) {
if (node.async) {
this.word("async");
// We prevent inner comments from being printed here,
// so that they are always consistently printed in the
// same place regardless of the function type.
this._endsWithInnerRaw = false;
this.space();
}
this.word("function");
if (node.generator) this.token("*");
this.printInnerComments(node);
if (node.generator) {
// We prevent inner comments from being printed here,
// so that they are always consistently printed in the
// same place regardless of the function type.
this._endsWithInnerRaw = false;
this.token("*");
}

this.space();
if (node.id) {
Expand Down Expand Up @@ -196,7 +198,10 @@ export function ArrowFunctionExpression(
this._predicate(node);
this.ensureNoLineTerminator(() => {
this.space();
this.printInnerComments(node);
// 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("=>");
});

Expand Down
24 changes: 18 additions & 6 deletions packages/babel-generator/src/generators/modules.ts
Expand Up @@ -66,6 +66,18 @@ export function ExportNamespaceSpecifier(
this.print(node.exported, node);
}

export function _printAssertions(
this: Printer,
node: Extract<t.Node, { assertions?: t.ImportAttribute[] }>,
) {
this.space();
this.token("{");
this.space();
this.printList(node.assertions, node);
this.space();
this.token("}");
}

export function ExportAllDeclaration(
this: Printer,
node: t.ExportAllDeclaration | t.DeclareExportAllDeclaration,
Expand All @@ -88,7 +100,7 @@ export function ExportAllDeclaration(
this.word("assert");
});
// @ts-expect-error Fixme: assertions is not defined in DeclareExportAllDeclaration
this.printAssertions(node);
this._printAssertions(node);
} else {
this.print(node.source, node);
}
Expand Down Expand Up @@ -162,7 +174,7 @@ export function ExportNamedDeclaration(
this.space();
this.word("assert");
});
this.printAssertions(node);
this._printAssertions(node);
} else {
this.print(node.source, node);
}
Expand All @@ -186,7 +198,7 @@ export function ExportDefaultDeclaration(
}

this.word("export");
this.printInnerComments(node);
this.noIndentInnerCommentsHere();
this.space();
this.word("default");
this.space();
Expand All @@ -201,11 +213,11 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) {

const isTypeKind = node.importKind === "type" || node.importKind === "typeof";
if (isTypeKind) {
this.printInnerComments(node, false);
this.noIndentInnerCommentsHere();
this.word(node.importKind);
this.space();
} else if (node.module) {
this.printInnerComments(node, false);
this.noIndentInnerCommentsHere();
this.word("module");
this.space();
}
Expand Down Expand Up @@ -250,7 +262,7 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) {
this.space();
this.word("assert");
});
this.printAssertions(node);
this._printAssertions(node);
} else {
this.print(node.source, node);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/statements.ts
Expand Up @@ -103,7 +103,7 @@ function ForXStatement(this: Printer, node: t.ForXStatement) {
this.word("await");
this.space();
}
this.printInnerComments(node, false);
this.noIndentInnerCommentsHere();
this.token("(");
this.print(node.left, node);
this.space();
Expand Down
4 changes: 0 additions & 4 deletions packages/babel-generator/src/generators/types.ts
Expand Up @@ -22,7 +22,6 @@ export function ObjectExpression(this: Printer, node: t.ObjectExpression) {
const props = node.properties;

this.token("{");
this.printInnerComments(node);

if (props.length) {
this.space();
Expand Down Expand Up @@ -86,7 +85,6 @@ export function ArrayExpression(this: Printer, node: t.ArrayExpression) {
const len = elems.length;

this.token("[");
this.printInnerComments(node);

for (let i = 0; i < elems.length; i++) {
const elem = elems[i];
Expand Down Expand Up @@ -132,7 +130,6 @@ export function RecordExpression(this: Printer, node: t.RecordExpression) {
}

this.token(startToken);
this.printInnerComments(node);

if (props.length) {
this.space();
Expand Down Expand Up @@ -161,7 +158,6 @@ export function TupleExpression(this: Printer, node: t.TupleExpression) {
}

this.token(startToken);
this.printInnerComments(node);

for (let i = 0; i < elems.length; i++) {
const elem = elems[i];
Expand Down

0 comments on commit bea7a43

Please sign in to comment.