Fix comment moved before : in property signature (#19630)

Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
Konv Suuandfisker authored and GitHub committed 2026-07-22 09:37:03 +08:00
1 parent 57f6882d2b
commit 53334538ac
3 files changed
+84 -8

No files matched your search

+31 -8
View File
@@ -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({
@@ -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;
}
================================================================================
`;
@@ -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
}