Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Types containing comments generate invalid code #14762

Merged
merged 2 commits into from Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 1 addition & 9 deletions packages/babel-generator/src/generators/methods.ts
Expand Up @@ -11,15 +11,7 @@ export function _params(
this._parameters(node.params, node);
this.token(")");

if (node.returnType) {
if (node.type === "ArrowFunctionExpression") {
this._noLineTerminator = true;
this.print(node.returnType, node);
this._noLineTerminator = false;
} else {
this.print(node.returnType, node);
}
}
this.print(node.returnType, node);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted here #14758.


export function _parameters(
Expand Down
38 changes: 25 additions & 13 deletions packages/babel-generator/src/printer.ts
@@ -1,7 +1,6 @@
import Buffer from "./buffer";
import type { Loc } from "./buffer";
import * as n from "./node";
import { isProgram, isFile, isEmptyStatement } from "@babel/types";
import type * as t from "@babel/types";
import type {
RecordAndTuplePluginOptions,
Expand Down Expand Up @@ -507,42 +506,45 @@ class Printer {
print(node: t.Node | null, parent?: t.Node) {
if (!node) return;

const oldConcise = this.format.concise;
const nodeType = node.type;
const format = this.format;

const oldConcise = format.concise;
if (
// @ts-expect-error document _compact AST properties
node._compact
) {
this.format.concise = true;
format.concise = true;
}

const printMethod =
this[
node.type as Exclude<
nodeType as Exclude<
t.Node["type"],
// removed
| "Noop"
// renamed
| t.DeprecatedAliases["type"]
>
];
if (!printMethod) {
if (printMethod === undefined) {
throw new ReferenceError(
`unknown node of type ${JSON.stringify(
node.type,
)} with constructor ${JSON.stringify(node?.constructor.name)}`,
nodeType,
)} with constructor ${JSON.stringify(node.constructor.name)}`,
);
}

this._printStack.push(node);

const oldInAux = this._insideAux;
this._insideAux = !node.loc;
this._insideAux = node.loc == undefined;
this._maybeAddAuxComment(this._insideAux && !oldInAux);

let shouldPrintParens: boolean;
if (
this.format.retainFunctionParens &&
node.type === "FunctionExpression" &&
format.retainFunctionParens &&
nodeType === "FunctionExpression" &&
node.extra &&
node.extra.parenthesized
) {
Expand All @@ -552,20 +554,30 @@ class Printer {
}
if (shouldPrintParens) this.token("(");

let noLineTerminator;
if (nodeType === "TSTypeAnnotation" || nodeType === "TypeAnnotation") {
noLineTerminator = this._noLineTerminator;
this._noLineTerminator = true;
}

this._printLeadingComments(node);

const loc = isProgram(node) || isFile(node) ? null : node.loc;
const loc = nodeType === "Program" || nodeType === "File" ? null : node.loc;

this.withSource("start", loc, printMethod.bind(this, node, parent));

this._printTrailingComments(node);

if (noLineTerminator !== undefined) {
this._noLineTerminator = noLineTerminator;
}

if (shouldPrintParens) this.token(")");

// end
this._printStack.pop();

this.format.concise = oldConcise;
format.concise = oldConcise;
this._insideAux = oldInAux;
}

Expand Down Expand Up @@ -667,7 +679,7 @@ class Printer {
printBlock(parent: Extract<t.Node, { body: t.Statement }>) {
const node = parent.body;

if (!isEmptyStatement(node)) {
if (node.type !== "EmptyStatement") {
this.space();
}

Expand Down
@@ -1 +1,3 @@
export default (): void /* hi! */ => {};

const foo: any /* Hi! */[] = [];
@@ -0,0 +1,2 @@
export default ((): void /* hi! */ => {});
const foo: any /* Hi! */[] = [];
@@ -0,0 +1,3 @@
export default (): void /* hi! */ => {};

const foo: any /* Hi! */[] = [];
@@ -0,0 +1,2 @@
export default ((): void /* hi! */ => {});
const foo: any /* Hi! */[] = [];

This file was deleted.