From 055dba4258cd4679bbbc95796dd3a54a0b3c4074 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 20 Aug 2026 20:14:15 +0800 Subject: [PATCH] Fix comment in destructuring patterns for Flow parser (#19902) --- changelog_unreleased/flow/19902.md | 31 ++++ src/language-js/comments/handle-comments.js | 3 +- .../__snapshots__/format.test.js.snap | 161 ------------------ .../{ => consistent-with-flow}/11639.ts | 0 .../{ => consistent-with-flow}/15707.ts | 12 +- .../__snapshots__/format.test.js.snap | 161 ++++++++++++++++++ 6 files changed, 200 insertions(+), 168 deletions(-) create mode 100644 changelog_unreleased/flow/19902.md rename tests/format/typescript/comments/{ => consistent-with-flow}/11639.ts (100%) rename tests/format/typescript/comments/{ => consistent-with-flow}/15707.ts (90%) diff --git a/changelog_unreleased/flow/19902.md b/changelog_unreleased/flow/19902.md new file mode 100644 index 000000000..c7070c02d --- /dev/null +++ b/changelog_unreleased/flow/19902.md @@ -0,0 +1,31 @@ +#### Fix comment in destructuring patterns (#19902 by @fisker) + + +```flow +// Input +const { + foo, + // bar + // baz +}: Foo = expr; + +// Prettier stable (First format) +const { + foo1, +} // bar +// baz +: Foo = expr; + +// Prettier stable (Second format) +const { + foo1, // bar +} // baz +: Foo = expr; + +// Prettier main +const { + foo1, + // bar + // baz +}: Foo = expr; +``` diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index a98029ac9..a1199cc65 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -989,7 +989,8 @@ function handleCommentsInDestructuringPattern({ if ( (enclosingNode?.type === "ObjectPattern" || enclosingNode?.type === "ArrayPattern") && - followingNode?.type === "TSTypeAnnotation" + (followingNode?.type === "TSTypeAnnotation" || + followingNode?.type === "TypeAnnotation") ) { if (precedingNode) { addTrailingComment(precedingNode, comment); diff --git a/tests/format/typescript/comments/__snapshots__/format.test.js.snap b/tests/format/typescript/comments/__snapshots__/format.test.js.snap index eabe65075..2edf5c895 100644 --- a/tests/format/typescript/comments/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/comments/__snapshots__/format.test.js.snap @@ -69,46 +69,6 @@ type D2 = ================================================================================ `; -exports[`11639.ts format 1`] = ` -====================================options===================================== -parsers: ["typescript"] - printWidth: 80 (default) | -=====================================input====================================== -const foo = forwardRef(({ - // foo - size, - // onClose, - // onOpen, - }: PopoverProps, -) => {}) - -export function Component({ - prop, - // comment -}: Type) { - return div; -} - -=====================================output===================================== -const foo = forwardRef( - ({ - // foo - size, - // onClose, - // onOpen, - }: PopoverProps) => {}, -); - -export function Component({ - prop, - // comment -}: Type) { - return div; -} - -================================================================================ -`; - exports[`11662.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -136,127 +96,6 @@ foo ================================================================================ `; -exports[`15707.ts format 1`] = ` -====================================options===================================== -parsers: ["typescript"] - printWidth: 80 (default) | -=====================================input====================================== -const { - foo, - // bar - // baz -}: Foo = expr; - -const { - foo1, - // bar - foo2, - // baz -}: Foo = expr; - -const [ - foo, - // bar - // baz -]: Foo = expr; - -const [ - foo1, - // bar - foo2, - // baz -]: Foo = expr; - -function method({ - foo, - // bar = "bar", - // bazz = "bazz", -}: Foo) {} - -function method({ - foo1, - // bar = "bar", - foo2 - // bazz = "bazz", -}: Foo) {} - -function method([ - foo, - // bar = "bar", - foo2 - // bazz = "bazz", -]: Foo) {} - -const { - // bar - // baz -}: Foo = expr; - -const [ - // bar - // baz -]: Foo = expr; - -=====================================output===================================== -const { - foo, - // bar - // baz -}: Foo = expr; - -const { - foo1, - // bar - foo2, - // baz -}: Foo = expr; - -const [ - foo, - // bar - // baz -]: Foo = expr; - -const [ - foo1, - // bar - foo2, - // baz -]: Foo = expr; - -function method({ - foo, - // bar = "bar", - // bazz = "bazz", -}: Foo) {} - -function method({ - foo1, - // bar = "bar", - foo2, - // bazz = "bazz", -}: Foo) {} - -function method([ - foo, - // bar = "bar", - foo2, - // bazz = "bazz", -]: Foo) {} - -const { - // bar - // baz -}: Foo = expr; - -const [ - // bar - // baz -]: Foo = expr; - -================================================================================ -`; - exports[`16065.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/comments/11639.ts b/tests/format/typescript/comments/consistent-with-flow/11639.ts similarity index 100% rename from tests/format/typescript/comments/11639.ts rename to tests/format/typescript/comments/consistent-with-flow/11639.ts diff --git a/tests/format/typescript/comments/15707.ts b/tests/format/typescript/comments/consistent-with-flow/15707.ts similarity index 90% rename from tests/format/typescript/comments/15707.ts rename to tests/format/typescript/comments/consistent-with-flow/15707.ts index 98f7001f1..976f03cdb 100644 --- a/tests/format/typescript/comments/15707.ts +++ b/tests/format/typescript/comments/consistent-with-flow/15707.ts @@ -1,26 +1,26 @@ const { - foo, + foo11, // bar // baz }: Foo = expr; const { - foo1, + foo21, // bar - foo2, + foo22, // baz }: Foo = expr; const [ - foo, + foo31, // bar // baz ]: Foo = expr; const [ - foo1, + foo41, // bar - foo2, + foo42, // baz ]: 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 10a0b6998..73776dc78 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 @@ -1,5 +1,166 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing +exports[`11639.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] + printWidth: 80 (default) | +=====================================input====================================== +const foo = forwardRef(({ + // foo + size, + // onClose, + // onOpen, + }: PopoverProps, +) => {}) + +export function Component({ + prop, + // comment +}: Type) { + return div; +} + +=====================================output===================================== +const foo = forwardRef( + ({ + // foo + size, + // onClose, + // onOpen, + }: PopoverProps) => {}, +); + +export function Component({ + prop, + // comment +}: Type) { + return div; +} + +================================================================================ +`; + +exports[`15707.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] + printWidth: 80 (default) | +=====================================input====================================== +const { + foo11, + // bar + // baz +}: Foo = expr; + +const { + foo21, + // bar + foo22, + // baz +}: Foo = expr; + +const [ + foo31, + // bar + // baz +]: Foo = expr; + +const [ + foo41, + // bar + foo42, + // baz +]: Foo = expr; + +function method({ + foo, + // bar = "bar", + // bazz = "bazz", +}: Foo) {} + +function method({ + foo1, + // bar = "bar", + foo2 + // bazz = "bazz", +}: Foo) {} + +function method([ + foo, + // bar = "bar", + foo2 + // bazz = "bazz", +]: Foo) {} + +const { + // bar + // baz +}: Foo = expr; + +const [ + // bar + // baz +]: Foo = expr; + +=====================================output===================================== +const { + foo11, + // bar + // baz +}: Foo = expr; + +const { + foo21, + // bar + foo22, + // baz +}: Foo = expr; + +const [ + foo31, + // bar + // baz +]: Foo = expr; + +const [ + foo41, + // bar + foo42, + // baz +]: Foo = expr; + +function method({ + foo, + // bar = "bar", + // bazz = "bazz", +}: Foo) {} + +function method({ + foo1, + // bar = "bar", + foo2, + // bazz = "bazz", +}: Foo) {} + +function method([ + foo, + // bar = "bar", + foo2, + // bazz = "bazz", +]: Foo) {} + +const { + // bar + // baz +}: Foo = expr; + +const [ + // bar + // baz +]: Foo = expr; + +================================================================================ +`; + exports[`after-jsx-generic.tsx format 1`] = ` ====================================options===================================== parsers: ["typescript", "flow"]