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 printing of comments before => #15160

Merged
merged 2 commits into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion packages/babel-generator/src/generators/methods.ts
Expand Up @@ -11,7 +11,10 @@ export function _params(
this._parameters(node.params, node);
this.token(")");

this.print(node.returnType, node, node.type === "ArrowFunctionExpression");
const noLineTerminator = node.type === "ArrowFunctionExpression";
this.print(node.returnType, node, noLineTerminator);

this._noLineTerminator = noLineTerminator;
}

export function _parameters(
Expand Down
@@ -0,0 +1,3 @@
var test = (/* placeholder */) => {
/* Unused */
}
@@ -0,0 +1,3 @@
{
"comments": false
}
@@ -0,0 +1,3 @@
var test = (
) => {
};
19 changes: 18 additions & 1 deletion packages/babel-generator/test/index.js
Expand Up @@ -1151,7 +1151,7 @@ describe("programmatic generation", function () {
expect(generate(expr).code).toMatchInlineSnapshot(`"1 + 2 /*foo*/"`);
});

it("comment skipped because of newlines", () => {
it("comment skipped in arrow function because of newlines", () => {
const arrow = t.arrowFunctionExpression(
[t.identifier("x"), t.identifier("y")],
t.identifier("z"),
Expand All @@ -1166,6 +1166,23 @@ describe("programmatic generation", function () {
line*/"
`);
});

it("comment in arrow function with return type", () => {
const arrow = t.arrowFunctionExpression(
[t.identifier("x"), t.identifier("y")],
t.identifier("z"),
);
arrow.returnType = t.tsTypeAnnotation(t.tsAnyKeyword());
arrow.returnType.trailingComments = [
{ type: "CommentBlock", value: "foo" },
// This comment is dropped. There is no way to safely print it
// as a trailingComment of the return type.
{ type: "CommentBlock", value: "new\nline" },
];
expect(generate(arrow).code).toMatchInlineSnapshot(
`"(x, y): any /*foo*/ => z"`,
);
});
});
});

Expand Down