diff --git a/changelog_unreleased/flow/18630.md b/changelog_unreleased/flow/18630.md new file mode 100644 index 000000000..4159fa992 --- /dev/null +++ b/changelog_unreleased/flow/18630.md @@ -0,0 +1,32 @@ +#### Fix dangling comment print in hooks (#18630 by @fisker) + + +```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; +``` diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index a9cc371ec..b2c245c94 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -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 && diff --git a/tests/format/flow/hook/__snapshots__/format.test.js.snap b/tests/format/flow/hook/__snapshots__/format.test.js.snap index e6be549fb..be2fc7195 100644 --- a/tests/format/flow/hook/__snapshots__/format.test.js.snap +++ b/tests/format/flow/hook/__snapshots__/format.test.js.snap @@ -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"] diff --git a/tests/format/flow/hook/dangling-comments.js b/tests/format/flow/hook/dangling-comments.js new file mode 100644 index 000000000..7e2f7aff9 --- /dev/null +++ b/tests/format/flow/hook/dangling-comments.js @@ -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