From eb0155289f3e2e46ea551b624426e48a5bbc048d Mon Sep 17 00:00:00 2001 From: Sarath Francis Date: Wed, 3 Jun 2026 13:20:50 -0400 Subject: [PATCH] Fix unstable trailing comment on a parenthesized assignment statement (#19276) Co-authored-by: fisker --- src/language-js/comments/handle-comments.js | 11 +++-- .../__snapshots__/format.test.js.snap | 48 ++++++++++++++++--- ...parenthesized-trailing-comment-unstable.js | 2 - .../parenthesized-trailing-comment.js | 25 +++++++++- 4 files changed, 73 insertions(+), 13 deletions(-) diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 0fe5dc995..c7fc2e80f 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -1081,6 +1081,14 @@ function handleParenthesizedExpressionTrailingComment({ text, }) { if (!followingNode && enclosingNode) { + if ( + enclosingNode.type === "ExpressionStatement" && + enclosingNode.expression === precedingNode + ) { + addTrailingComment(enclosingNode, comment); + return true; + } + const isSequence = precedingNode?.type === "SequenceExpression"; const isAssignment = precedingNode?.type === "AssignmentExpression"; @@ -1092,9 +1100,6 @@ function handleParenthesizedExpressionTrailingComment({ enclosingNode.init === precedingNode) || (enclosingNode.type === "ReturnStatement" && enclosingNode.argument === precedingNode) || - (isSequence && - enclosingNode.type === "ExpressionStatement" && - enclosingNode.expression === precedingNode) || (isSequence && enclosingNode.type === "AssignmentExpression" && enclosingNode.right === precedingNode)) && diff --git a/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap b/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap index 2aedc70df..fbec30c5c 100644 --- a/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap +++ b/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap @@ -136,7 +136,30 @@ const x1 = (a, b, c /* comment */); const x2 = (a = c /* comment */); (a, b, c /* comment */); -// (a = c /* comment */); +(a, b, c /* comment */ +); +(a, b, c +/* comment */ +); +(a = c /* comment */); +(a = c /* comment */ +); +(a = c +/* comment */ +); +({a} = c /* comment */); +({a} = c +/* comment */ +); +({a} = c /* comment */ +); + +(foo() /* comment */); +(foo() +/* comment */ +); +(foo() /* comment */ +); assigned = (a, b, c /* comment */); // assigned = (a = c /* comment */); @@ -150,8 +173,23 @@ function f() { const x1 = (a, b, c /* comment */); const x2 = (a = c /* comment */); -(a, b, c /* comment */); -// (a = c /* comment */); +(a, b, c); /* comment */ +(a, b, c); /* comment */ +(a, b, c); +/* comment */ +a = c; /* comment */ +a = c; /* comment */ +a = c; +/* comment */ +({ a } = c); /* comment */ +({ a } = c); +/* comment */ +({ a } = c); /* comment */ + +foo(); /* comment */ +foo(); +/* comment */ +foo(); /* comment */ assigned = (a, b, c /* comment */); // assigned = (a = c /* comment */); @@ -171,15 +209,11 @@ parsers: ["babel", "flow", "typescript"] =====================================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 */; ================================================================================ diff --git a/tests/format/js/sequence-expression/parenthesized-trailing-comment-unstable.js b/tests/format/js/sequence-expression/parenthesized-trailing-comment-unstable.js index 659db507c..9cb146df4 100644 --- a/tests/format/js/sequence-expression/parenthesized-trailing-comment-unstable.js +++ b/tests/format/js/sequence-expression/parenthesized-trailing-comment-unstable.js @@ -1,5 +1,3 @@ // TODO: Enable tests in parenthesized-trailing-comment.js when they are stable -(a = c /* comment */); - assigned = (a = c /* comment */); diff --git a/tests/format/js/sequence-expression/parenthesized-trailing-comment.js b/tests/format/js/sequence-expression/parenthesized-trailing-comment.js index 92ac2f5b4..3db517fe7 100644 --- a/tests/format/js/sequence-expression/parenthesized-trailing-comment.js +++ b/tests/format/js/sequence-expression/parenthesized-trailing-comment.js @@ -2,7 +2,30 @@ const x1 = (a, b, c /* comment */); const x2 = (a = c /* comment */); (a, b, c /* comment */); -// (a = c /* comment */); +(a, b, c /* comment */ +); +(a, b, c +/* comment */ +); +(a = c /* comment */); +(a = c /* comment */ +); +(a = c +/* comment */ +); +({a} = c /* comment */); +({a} = c +/* comment */ +); +({a} = c /* comment */ +); + +(foo() /* comment */); +(foo() +/* comment */ +); +(foo() /* comment */ +); assigned = (a, b, c /* comment */); // assigned = (a = c /* comment */);