Fix comment around nullish coalescing (#19652)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-07-17 13:52:03 +08:00
1 parent 50afb31f2b
commit d9e922cabd
7 files changed
+249 -3

No files matched your search

+17 -2
View File
@@ -15,6 +15,7 @@ import { isBlockComment, isLineComment } from "../utilities/comment-types.js";
import { createTypeCheckFunction } from "../utilities/create-type-check-function.js";
import { getFunctionParameters } from "../utilities/function-parameters.js";
import { isMethod } from "../utilities/is-method.js";
import { isNullishCoalescing } from "../utilities/is-nullish-coalescing.js";
import { isObjectProperty } from "../utilities/is-object-property.js";
import { isPrettierIgnoreComment } from "../utilities/is-prettier-ignore-comment.js";
import { isTypeCastComment } from "../utilities/is-type-cast-comment.js";
@@ -70,6 +71,7 @@ function handleOwnLineComment(context) {
return [
handleCommentInEmptyParens,
handleIgnoreComments,
handleClosureTypeCastComments,
handleConditionalExpressionComments,
handleLastFunctionParameterComments,
handleMemberExpressionComments,
@@ -135,6 +137,7 @@ function handleRemainingComment(context) {
return [
handleCommentInEmptyParens,
handleIgnoreComments,
handleClosureTypeCastComments,
handleIfStatementComments,
handleWhileLikeComments,
handleSwitchStatementComments,
@@ -152,9 +155,21 @@ function handleRemainingComment(context) {
].some((fn) => fn(context));
}
function handleClosureTypeCastComments({ comment, followingNode }) {
function handleClosureTypeCastComments({
comment,
followingNode,
enclosingNode,
}) {
if (followingNode && isTypeCastComment(comment)) {
addLeadingComment(followingNode, comment);
addLeadingComment(
// We'll add parentheses to the nullish coalescing expression,
// The comment need attach to the left side, so the comments can print inside it
enclosingNode?.type === "ConditionalExpression" &&
isNullishCoalescing(followingNode)
? followingNode.left
: followingNode,
comment,
);
return true;
}
return false;