Improve dangling comment handling in flow hooks (#18630)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-01-15 03:01:56 +08:00
1 parent 48f7598e57
commit 4cfb8bc9b8
4 files changed
+156 -7

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
#### Fix dangling comment print in hooks (#18630 by @fisker)
<!-- prettier-ignore -->
```jsx
// Input
hook A(
// comment
) {}
declare hook B(// comment
): void
type C = hook (// comment
)=> void
// Prettier stable
hook A() // comment
{}
declare hook B(): // comment
void;
type C = hook () => // comment
void;
// Prettier main
hook A(
// comment
) {}
declare hook B(
// comment
): void;
type C = hook (
// comment
) => void;
```
+10 -7
View File
@@ -662,13 +662,16 @@ function handleCommentInEmptyParens({ comment, enclosingNode, options }) {
// Only add dangling comments to fix the case when no params are present,
// i.e. a function without any argument.
const functionNode = isRealFunctionLikeNode(enclosingNode)
? enclosingNode
: enclosingNode.type === "MethodDefinition" ||
enclosingNode.type === "TSAbstractMethodDefinition" ||
(enclosingNode.type === "Property" && isMethod(enclosingNode))
? enclosingNode.value
: undefined;
const functionNode =
isRealFunctionLikeNode(enclosingNode) ||
enclosingNode.type === "HookDeclaration" ||
enclosingNode.type === "HookTypeAnnotation"
? enclosingNode
: enclosingNode.type === "MethodDefinition" ||
enclosingNode.type === "TSAbstractMethodDefinition" ||
(enclosingNode.type === "Property" && isMethod(enclosingNode))
? enclosingNode.value
: undefined;
if (
functionNode &&
@@ -1,5 +1,82 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`dangling-comments.js format 1`] = `
====================================options=====================================
parsers: ["flow"]
printWidth: 80
| printWidth
=====================================input======================================
hook C11(// 11
) {}
hook C12(
// 12
) {}
declare hook H13(// 13
): void
declare hook H14(
// 14
): void
type T11 = hook (//15
) => void
type T12 = hook(
//16
) => void
hook C21(/* 21 */
) {}
hook C22(
/* 22*/
) {}
hook C23(
/* 23*/){}
declare hook H24(/* 24 */
): void
declare hook H25(
/* 25*/
): void
declare hook H26(
/* 26*/): void
type T21 = hook(/* 27*/
) => void
type T22 = hook(
/* 28*/
) => void
type T23 = hook(
/* 29*/) => void
=====================================output=====================================
hook C11(
// 11
) {}
hook C12(
// 12
) {}
declare hook H13(
// 13
): void;
declare hook H14(
// 14
): void;
type T11 = hook (
//15
) => void;
type T12 = hook (
//16
) => void;
hook C21(/* 21 */) {}
hook C22(/* 22*/) {}
hook C23(/* 23*/) {}
declare hook H24(/* 24 */): void;
declare hook H25(/* 25*/): void;
declare hook H26(/* 26*/): void;
type T21 = hook (/* 27*/) => void;
type T22 = hook (/* 28*/) => void;
type T23 = hook (/* 29*/) => void;
================================================================================
`;
exports[`declare-hook.js format 1`] = `
====================================options=====================================
parsers: ["flow"]
@@ -0,0 +1,37 @@
hook C11(// 11
) {}
hook C12(
// 12
) {}
declare hook H13(// 13
): void
declare hook H14(
// 14
): void
type T11 = hook (//15
) => void
type T12 = hook(
//16
) => void
hook C21(/* 21 */
) {}
hook C22(
/* 22*/
) {}
hook C23(
/* 23*/){}
declare hook H24(/* 24 */
): void
declare hook H25(
/* 25*/
): void
declare hook H26(
/* 26*/): void
type T21 = hook(/* 27*/
) => void
type T22 = hook(
/* 28*/
) => void
type T23 = hook(
/* 29*/) => void