From 4169dbb871f29023fdb8ed4594c7bb19a758771e Mon Sep 17 00:00:00 2001 From: SUZUKI Sosuke Date: Sat, 16 Sep 2023 21:52:02 +0900 Subject: [PATCH] Fix printing comment for the last operand of union types (#15409) --- changelog_unreleased/typescript/15409.md | 36 +++++++++++++++++ src/language-js/comments/handle-comments.js | 39 +++++++++++++++++++ .../union/__snapshots__/jsfmt.spec.js.snap | 31 +++++++++++++++ tests/format/flow/union/comments.js | 9 +++++ .../union/__snapshots__/jsfmt.spec.js.snap | 31 +++++++++++++++ tests/format/typescript/union/comments.ts | 9 +++++ 6 files changed, 155 insertions(+) create mode 100644 changelog_unreleased/typescript/15409.md create mode 100644 tests/format/flow/union/comments.js create mode 100644 tests/format/typescript/union/comments.ts diff --git a/changelog_unreleased/typescript/15409.md b/changelog_unreleased/typescript/15409.md new file mode 100644 index 000000000..6834c9efb --- /dev/null +++ b/changelog_unreleased/typescript/15409.md @@ -0,0 +1,36 @@ +#### Fix printing comment for the last operand of union types (#15409 by @sosukesuzuki) + + +```tsx +// Input +type Foo1 = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +)[]; // Final comment1 +type Foo2 = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +) & Bar; // Final comment2 + +// Prettier stable +type Foo1 = ( + | "thing1" // Comment1 + | "thing2" +)[]; // Comment2 // Final comment1 +type Foo2 = ( + | "thing1" // Comment1 + | "thing2" +) & // Comment2 + Bar; // Final comment2 + +// Prettier main +type Foo1 = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +)[]; // Final comment1 +type Foo2 = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +) & + Bar; // Final comment2 +``` diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 193fa8fd3..417146c68 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -84,6 +84,7 @@ function handleEndOfLineComment(context) { handleVariableDeclaratorComments, handleBreakAndContinueStatementComments, handleSwitchDefaultCaseComments, + handleLastUnionElementInExpression, ].some((fn) => fn(context)); } @@ -887,6 +888,44 @@ function handleSwitchDefaultCaseComments({ return true; } +/** + * Handle `Comment2` and `Comment4`. + * + * type Foo = ( + * | "thing1" // Comment1 + * | "thing2" // Comment2 + * )[]; + * + * type Foo = ( + * | "thing1" // Comment3 + * | "thing2" // Comment4 + * ) & Bar; + * + * @param {CommentContext} context + * @returns {boolean} + */ +function handleLastUnionElementInExpression({ + comment, + precedingNode, + enclosingNode, + followingNode, +}) { + if ( + precedingNode && + (precedingNode.type === "TSUnionType" || + precedingNode.type === "UnionTypeAnnotation") && + (((enclosingNode.type === "TSArrayType" || + enclosingNode.type === "ArrayTypeAnnotation") && + followingNode === undefined) || + enclosingNode.type === "TSIntersectionType" || + enclosingNode.type === "IntersectionTypeAnnotation") + ) { + addTrailingComment(precedingNode.types.at(-1), comment); + return true; + } + return false; +} + /** * @param {Node} node * @returns {boolean} diff --git a/tests/format/flow/union/__snapshots__/jsfmt.spec.js.snap b/tests/format/flow/union/__snapshots__/jsfmt.spec.js.snap index 9aaaab64f..931c423fa 100644 --- a/tests/format/flow/union/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/flow/union/__snapshots__/jsfmt.spec.js.snap @@ -22,6 +22,37 @@ const myValue = (callcallcallcallcallcall( ================================================================================ `; +exports[`comments.js format 1`] = ` +====================================options===================================== +parsers: ["flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +)[]; // Final comment1 + +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +) & Bar; // Final comment2 + +=====================================output===================================== +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +)[]; // Final comment1 + +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +) & + Bar; // Final comment2 + +================================================================================ +`; + exports[`union.js format 1`] = ` ====================================options===================================== parsers: ["flow"] diff --git a/tests/format/flow/union/comments.js b/tests/format/flow/union/comments.js new file mode 100644 index 000000000..fac6e0706 --- /dev/null +++ b/tests/format/flow/union/comments.js @@ -0,0 +1,9 @@ +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +)[]; // Final comment1 + +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +) & Bar; // Final comment2 diff --git a/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap index 9eb784026..1328eb465 100644 --- a/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,36 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`comments.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +)[]; // Final comment1 + +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +) & Bar; // Final comment2 + +=====================================output===================================== +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +)[]; // Final comment1 + +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +) & + Bar; // Final comment2 + +================================================================================ +`; + exports[`inlining.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/union/comments.ts b/tests/format/typescript/union/comments.ts new file mode 100644 index 000000000..fac6e0706 --- /dev/null +++ b/tests/format/typescript/union/comments.ts @@ -0,0 +1,9 @@ +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +)[]; // Final comment1 + +type Foo = ( + | "thing1" // Comment1 + | "thing2" // Comment2 +) & Bar; // Final comment2