diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 25d2a4293..7b3e5951f 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -151,6 +151,7 @@ function handleRemainingComment(context) { handleFunctionNameComments, handleTSFunctionTrailingComments, handleParenthesizedExpressionTrailingComment, + handlePropertySignatureComments, handleBinaryCastExpressionComment, handleUnionTypeLeadingComments, ].some((fn) => fn(context)); @@ -1034,20 +1035,42 @@ function handleLastBinaryOperatorOperand({ return false; } -function handlePropertySignatureComments({ - enclosingNode, - followingNode, - comment, -}) { +/** @param {CommentContext} context */ +function handlePropertySignatureComments(context) { + const { enclosingNode, followingNode, comment, options, placement } = context; + if ( - enclosingNode && - (enclosingNode.type === "TSPropertySignature" || - enclosingNode.type === "ObjectTypeProperty") && + !followingNode || + (enclosingNode?.type !== "TSPropertySignature" && + enclosingNode?.type !== "ObjectTypeProperty") + ) { + return false; + } + + if ( + placement === "endOfLine" && (isUnionType(followingNode) || isIntersectionType(followingNode)) ) { addLeadingComment(followingNode, comment); return true; } + + if ( + ((enclosingNode.type === "TSPropertySignature" && + enclosingNode.typeAnnotation) || + (enclosingNode.type === "ObjectTypeProperty" && enclosingNode.value)) && + isBlockComment(comment) + ) { + const colonTokenIndex = stripComments(options).indexOf( + ":", + locEnd(enclosingNode.key), + ); + if (colonTokenIndex < locStart(comment)) { + return addLeadingCommentToPossibleUnionType(followingNode, context); + } + } + + return false; } function handleBinaryCastExpressionComment({ diff --git a/tests/format/typescript/property-signature/consistent-with-flow/__snapshots__/format.test.js.snap b/tests/format/typescript/property-signature/consistent-with-flow/__snapshots__/format.test.js.snap index 974ff037a..e7036273d 100644 --- a/tests/format/typescript/property-signature/consistent-with-flow/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/property-signature/consistent-with-flow/__snapshots__/format.test.js.snap @@ -14,6 +14,24 @@ interface A { property: /* Comment */ B } +interface A { + property: /** Comment */ + B +} + +interface A { + property /** Comment */ + : B +} + +interface A { + property: /** Comment */ | B +} + +interface A { + property /** Comment */: B | C +} + =====================================output===================================== interface A { property: B; // Comment @@ -23,6 +41,23 @@ interface A { property: /* Comment */ B; } +interface A { + property: /** Comment */ + B; +} + +interface A { + property /** Comment */: B; +} + +interface A { + property: /** Comment */ B; +} + +interface A { + property /** Comment */: B | C; +} + ================================================================================ `; diff --git a/tests/format/typescript/property-signature/consistent-with-flow/comments.ts b/tests/format/typescript/property-signature/consistent-with-flow/comments.ts index f4c22795b..c2aa0a5f6 100644 --- a/tests/format/typescript/property-signature/consistent-with-flow/comments.ts +++ b/tests/format/typescript/property-signature/consistent-with-flow/comments.ts @@ -6,3 +6,21 @@ interface A { interface A { property: /* Comment */ B } + +interface A { + property: /** Comment */ + B +} + +interface A { + property /** Comment */ + : B +} + +interface A { + property: /** Comment */ | B +} + +interface A { + property /** Comment */: B | C +}