From 296fbe6b7fdd3cc5b829d0a144ea060a887dd3ec Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 4 Jun 2026 02:22:52 +0800 Subject: [PATCH] Improve `handleParenthesizedExpressionTrailingComment` (#19281) --- src/language-js/comments/handle-comments.js | 4 +--- .../__snapshots__/format.test.js.snap | 14 ++++++++++++++ .../parenthesized-trailing-comment.js | 7 +++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index c7fc2e80f..9eda655f3 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -1078,7 +1078,6 @@ function handleParenthesizedExpressionTrailingComment({ enclosingNode, precedingNode, followingNode, - text, }) { if (!followingNode && enclosingNode) { if ( @@ -1102,8 +1101,7 @@ function handleParenthesizedExpressionTrailingComment({ enclosingNode.argument === precedingNode) || (isSequence && enclosingNode.type === "AssignmentExpression" && - enclosingNode.right === precedingNode)) && - getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ")" + enclosingNode.right === precedingNode)) ) { addTrailingComment( isSequence ? precedingNode.expressions.at(-1) : precedingNode.right, 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 fbec30c5c..dc6ab13fb 100644 --- a/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap +++ b/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap @@ -133,15 +133,19 @@ parsers: ["babel", "flow", "typescript"] printWidth: 80 (default) | =====================================input====================================== const x1 = (a, b, c /* comment */); +const x12 = (a, b, c) /* comment */; const x2 = (a = c /* comment */); +const x22 = (a = c) /* comment */; (a, b, 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 @@ -155,6 +159,7 @@ const x2 = (a = c /* comment */); ); (foo() /* comment */); +(foo()) /* comment */; (foo() /* comment */ ); @@ -162,23 +167,29 @@ const x2 = (a = c /* comment */); ); assigned = (a, b, c /* comment */); +assigned = (a, b, c) /* comment */; // assigned = (a = c /* comment */); function f() { return (a, b, c /* comment */); + return (a, b, c) /* comment */; return (a = c /* comment */); } =====================================output===================================== const x1 = (a, b, c /* comment */); +const x12 = (a, b, c); /* comment */ const x2 = (a = c /* comment */); +const x22 = (a = c); /* comment */ +(a, b, 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 */ @@ -186,16 +197,19 @@ a = c; /* comment */ ({ a } = c); /* comment */ +foo(); /* comment */ foo(); /* comment */ foo(); /* comment */ foo(); /* comment */ assigned = (a, b, c /* comment */); +assigned = (a, b, c); /* comment */ // assigned = (a = c /* comment */); function f() { return (a, b, c /* comment */); + return (a, b, c); /* comment */ return (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 3db517fe7..0c441f2d2 100644 --- a/tests/format/js/sequence-expression/parenthesized-trailing-comment.js +++ b/tests/format/js/sequence-expression/parenthesized-trailing-comment.js @@ -1,13 +1,17 @@ const x1 = (a, b, c /* comment */); +const x12 = (a, b, c) /* comment */; const x2 = (a = c /* comment */); +const x22 = (a = c) /* comment */; (a, b, 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 @@ -21,6 +25,7 @@ const x2 = (a = c /* comment */); ); (foo() /* comment */); +(foo()) /* comment */; (foo() /* comment */ ); @@ -28,9 +33,11 @@ const x2 = (a = c /* comment */); ); assigned = (a, b, c /* comment */); +assigned = (a, b, c) /* comment */; // assigned = (a = c /* comment */); function f() { return (a, b, c /* comment */); + return (a, b, c) /* comment */; return (a = c /* comment */); }