From 11d55846a0eac912abe5f3e9a97811064ce9affa Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Sun, 23 Aug 2026 07:48:09 +0500 Subject: [PATCH] Keep comment after destructuring pattern out of the pattern (#19905) Co-authored-by: fisker --- changelog_unreleased/typescript/19905.md | 16 +++++ src/language-js/comments/handle-comments.js | 28 ++++++-- .../comments/consistent-with-flow/15707.ts | 12 ++++ .../comments/consistent-with-flow/19903.ts | 15 +++++ .../__snapshots__/format.test.js.snap | 67 +++++++++++++++++++ 5 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 changelog_unreleased/typescript/19905.md create mode 100644 tests/format/typescript/comments/consistent-with-flow/19903.ts diff --git a/changelog_unreleased/typescript/19905.md b/changelog_unreleased/typescript/19905.md new file mode 100644 index 000000000..abd0b4cf9 --- /dev/null +++ b/changelog_unreleased/typescript/19905.md @@ -0,0 +1,16 @@ +#### Keep comment after destructuring pattern out of the pattern (#19905 by @lazerg) + + +```tsx +// Input +const { + foo +} /* comment */ + : Foo = bar; + +// Prettier stable +const { foo /* comment */ }: Foo = bar; + +// Prettier main +const { foo } /* comment */ : Foo = bar; +``` diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index a1199cc65..019ec7f91 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -123,6 +123,7 @@ function handleEndOfLineComment(context) { handleSwitchDefaultCaseComments, handleLastUnionElementInExpression, handleLastBinaryOperatorOperand, + handleCommentsInDestructuringPattern, handleTSMappedTypeComments, handleArrowExpressionComments, handleParenthesizedExpressionTrailingComment, @@ -985,22 +986,35 @@ function handleCommentsInDestructuringPattern({ enclosingNode, precedingNode, followingNode, + text, }) { if ( - (enclosingNode?.type === "ObjectPattern" || - enclosingNode?.type === "ArrayPattern") && - (followingNode?.type === "TSTypeAnnotation" || - followingNode?.type === "TypeAnnotation") + enclosingNode && + followingNode && + (enclosingNode.type === "ObjectPattern" || + enclosingNode.type === "ArrayPattern") && + enclosingNode.typeAnnotation === followingNode && + (followingNode.type === "TSTypeAnnotation" || + followingNode.type === "TypeAnnotation") ) { - if (precedingNode) { - addTrailingComment(precedingNode, comment); - } else { + if ( + getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === + (enclosingNode.type === "ObjectPattern" ? "}" : "]") + ) { + if (precedingNode) { + addTrailingComment(precedingNode, comment); + return true; + } + // const { // // bar // // baz // }: Foo = expr; addDanglingComment(enclosingNode, comment); + return true; } + + addLeadingComment(followingNode, comment); return true; } } diff --git a/tests/format/typescript/comments/consistent-with-flow/15707.ts b/tests/format/typescript/comments/consistent-with-flow/15707.ts index 976f03cdb..957ba97a5 100644 --- a/tests/format/typescript/comments/consistent-with-flow/15707.ts +++ b/tests/format/typescript/comments/consistent-with-flow/15707.ts @@ -53,3 +53,15 @@ const [ // bar // baz ]: Foo = expr; + +const { // foo +}: Foo = expr; +const { // foo + bar61 +}: Foo = expr; + +const [ // bar +]: Foo = expr; +const [ // bar + bar71 +]: Foo = expr; diff --git a/tests/format/typescript/comments/consistent-with-flow/19903.ts b/tests/format/typescript/comments/consistent-with-flow/19903.ts new file mode 100644 index 000000000..5b9d386b8 --- /dev/null +++ b/tests/format/typescript/comments/consistent-with-flow/19903.ts @@ -0,0 +1,15 @@ +const [] + // bar + : Foo = expr; + +const {} + // bar + : Foo = expr; + +const [foo1] + // bar + : Foo = expr; + +const {foo2} + // bar + : Foo = expr; diff --git a/tests/format/typescript/comments/consistent-with-flow/__snapshots__/format.test.js.snap b/tests/format/typescript/comments/consistent-with-flow/__snapshots__/format.test.js.snap index 73776dc78..3a93c9db0 100644 --- a/tests/format/typescript/comments/consistent-with-flow/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/comments/consistent-with-flow/__snapshots__/format.test.js.snap @@ -101,6 +101,18 @@ const [ // baz ]: Foo = expr; +const { // foo +}: Foo = expr; +const { // foo + bar61 +}: Foo = expr; + +const [ // bar +]: Foo = expr; +const [ // bar + bar71 +]: Foo = expr; + =====================================output===================================== const { foo11, @@ -158,6 +170,61 @@ const [ // baz ]: Foo = expr; +const { + // foo +}: Foo = expr; +const { + // foo + bar61, +}: Foo = expr; + +const [ + // bar +]: Foo = expr; +const [ + // bar + bar71, +]: Foo = expr; + +================================================================================ +`; + +exports[`19903.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] + printWidth: 80 (default) | +=====================================input====================================== +const [] + // bar + : Foo = expr; + +const {} + // bar + : Foo = expr; + +const [foo1] + // bar + : Foo = expr; + +const {foo2} + // bar + : Foo = expr; + +=====================================output===================================== +const [] // bar +: Foo = expr; + +const {} // bar +: Foo = expr; + +const [foo1] // bar +: Foo = expr; + +const { + foo2, +} // bar +: Foo = expr; + ================================================================================ `;