Skip to content

Commit

Permalink
Fix printing of comments before => (#15160)
Browse files Browse the repository at this point in the history
Fixes #15161
  • Loading branch information
nicolo-ribaudo committed Nov 8, 2022
1 parent 156b608 commit 4285d5f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
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

0 comments on commit 4285d5f

Please sign in to comment.