Skip to content

Commit 73e0a08

Browse files
committed
fix(js): preserve line breaks for line comments in empty params
1 parent df0c5a3 commit 73e0a08

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/language-js/print/function-parameters.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import getNextNonSpaceNonCommentCharacter from "../../utilities/get-next-non-spa
1414
import isNonEmptyArray from "../../utilities/is-non-empty-array.js";
1515
import { locEnd } from "../loc.js";
1616
import {
17+
CommentCheckFlags,
1718
getFunctionParameters,
1819
hasComment,
1920
hasRestParameter,
@@ -46,16 +47,28 @@ function printFunctionParameters(
4647
: "";
4748

4849
if (parameters.length === 0) {
50+
const commentFilter = (comment) =>
51+
getNextNonSpaceNonCommentCharacter(
52+
options.originalText,
53+
locEnd(comment),
54+
) === ")";
55+
56+
const hasDanglingLineComment = hasComment(
57+
functionNode,
58+
CommentCheckFlags.Dangling | CommentCheckFlags.Line,
59+
commentFilter,
60+
);
61+
62+
const danglingComments = printDanglingComments(path, options, {
63+
filter: commentFilter,
64+
indent: hasDanglingLineComment,
65+
});
66+
4967
return [
5068
typeParametersDoc,
5169
"(",
52-
printDanglingComments(path, options, {
53-
filter: (comment) =>
54-
getNextNonSpaceNonCommentCharacter(
55-
options.originalText,
56-
locEnd(comment),
57-
) === ")",
58-
}),
70+
danglingComments,
71+
hasDanglingLineComment && danglingComments ? softline : "",
5972
")",
6073
];
6174
}

tests/format/js/last-argument-expansion/__snapshots__/format.test.js.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,9 @@ foo(
146146
);
147147
148148
=====================================output=====================================
149-
foo(() =>
149+
foo((
150150
// foo
151-
{},
152-
);
151+
) => {});
153152
154153
================================================================================
155154
`;

0 commit comments

Comments
 (0)