Fix unstable trailing comment in parenthesized sequence or assignment expression (#19263)

Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
Sarath Francisandfisker authored and GitHub committed 2026-06-03 05:52:34 +08:00
1 parent 0926c3e9c4
commit 37255cd58b
6 files changed
+122 -33

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
#### Keep trailing comments inside the parentheses of a sequence or assignment expression (#19263 by @sarathfrancis90)
<!-- prettier-ignore -->
```jsx
// Input
const x = (a, b, c /* comment */);
const y = (a = b /* comment */);
// Prettier stable
const x = (a, b, c) /* comment */;
const y = (a = b) /* comment */;
// Prettier main
const x = (a, b, c /* comment */);
const y = (a = b /* comment */);
```
+29 -33
View File
@@ -112,8 +112,7 @@ function handleEndOfLineComment(context) {
handleLastBinaryOperatorOperand,
handleTSMappedTypeComments,
handleArrowExpressionComments,
handleArrowBodySequenceExpressionTrailingComment,
handleArrowBodyAssignmentExpressionTrailingComment,
handleParenthesizedExpressionTrailingComment,
handlePropertySignatureComments,
handleBinaryCastExpressionComment,
].some((fn) => fn(context));
@@ -136,8 +135,7 @@ function handleRemainingComment(context) {
handleCommentAfterArrowParams,
handleFunctionNameComments,
handleTSFunctionTrailingComments,
handleArrowBodySequenceExpressionTrailingComment,
handleArrowBodyAssignmentExpressionTrailingComment,
handleParenthesizedExpressionTrailingComment,
handleBinaryCastExpressionComment,
].some((fn) => fn(context));
}
@@ -1075,43 +1073,41 @@ function handleArrowExpressionComments({
return false;
}
function handleArrowBodySequenceExpressionTrailingComment({
function handleParenthesizedExpressionTrailingComment({
comment,
enclosingNode,
precedingNode,
followingNode,
text,
}) {
if (
!followingNode &&
enclosingNode?.type === "ArrowFunctionExpression" &&
precedingNode?.type === "SequenceExpression" &&
enclosingNode.body === precedingNode &&
getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ")"
) {
addTrailingComment(precedingNode.expressions.at(-1), comment);
return true;
}
return false;
}
if (!followingNode && enclosingNode) {
const isSequence = precedingNode?.type === "SequenceExpression";
const isAssignment = precedingNode?.type === "AssignmentExpression";
function handleArrowBodyAssignmentExpressionTrailingComment({
comment,
enclosingNode,
precedingNode,
followingNode,
text,
}) {
if (
!followingNode &&
enclosingNode?.type === "ArrowFunctionExpression" &&
precedingNode?.type === "AssignmentExpression" &&
enclosingNode.body === precedingNode &&
getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ")"
) {
addTrailingComment(precedingNode.right, comment);
return true;
if (
(isSequence || isAssignment) &&
((enclosingNode.type === "ArrowFunctionExpression" &&
enclosingNode.body === precedingNode) ||
(enclosingNode.type === "VariableDeclarator" &&
enclosingNode.init === precedingNode) ||
(enclosingNode.type === "ReturnStatement" &&
enclosingNode.argument === precedingNode) ||
(isSequence &&
enclosingNode.type === "ExpressionStatement" &&
enclosingNode.expression === precedingNode) ||
(isSequence &&
enclosingNode.type === "AssignmentExpression" &&
enclosingNode.right === precedingNode)) &&
getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ")"
) {
addTrailingComment(
isSequence ? precedingNode.expressions.at(-1) : precedingNode.right,
comment,
);
return true;
}
}
return false;
}
@@ -33,6 +33,7 @@ const unstableTests = new Map(
"typescript/call/callee-comments.ts",
"js/arrows/arrow-chain-with-trailing-comments.js",
"typescript/as/comments/18160.ts",
"js/sequence-expression/parenthesized-trailing-comment-unstable.js",
].map((fixture) => {
const [file, isUnstable = () => true] = Array.isArray(fixture)
? fixture
@@ -127,6 +127,64 @@ console.log(
================================================================================
`;
exports[`parenthesized-trailing-comment.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80 (default) |
=====================================input======================================
const x1 = (a, b, c /* comment */);
const x2 = (a = c /* comment */);
(a, b, c /* comment */);
// (a = c /* comment */);
assigned = (a, b, c /* comment */);
// assigned = (a = c /* comment */);
function f() {
return (a, b, c /* comment */);
return (a = c /* comment */);
}
=====================================output=====================================
const x1 = (a, b, c /* comment */);
const x2 = (a = c /* comment */);
(a, b, c /* comment */);
// (a = c /* comment */);
assigned = (a, b, c /* comment */);
// assigned = (a = c /* comment */);
function f() {
return (a, b, c /* comment */);
return (a = c /* comment */);
}
================================================================================
`;
exports[`parenthesized-trailing-comment-unstable.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80 (default) |
=====================================input======================================
// TODO: Enable tests in parenthesized-trailing-comment.js when they are stable
(a = c /* comment */);
assigned = (a = c /* comment */);
=====================================output=====================================
// TODO: Enable tests in parenthesized-trailing-comment.js when they are stable
a = c /* comment */;
assigned = a = c /* comment */;
================================================================================
`;
exports[`return.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
@@ -0,0 +1,5 @@
// TODO: Enable tests in parenthesized-trailing-comment.js when they are stable
(a = c /* comment */);
assigned = (a = c /* comment */);
@@ -0,0 +1,13 @@
const x1 = (a, b, c /* comment */);
const x2 = (a = c /* comment */);
(a, b, c /* comment */);
// (a = c /* comment */);
assigned = (a, b, c /* comment */);
// assigned = (a = c /* comment */);
function f() {
return (a, b, c /* comment */);
return (a = c /* comment */);
}