Fix printing comment for the last operand of union types (#18798)
This commit is contained in:
1 parent
52b9fd6331
commit
96546f353f
9 files changed
+107
-77
No files matched your search
@@ -0,0 +1,40 @@
|
||||
#### Fix printing comment for the last operand of union types (#18798 by @fisker)
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```tsx
|
||||
// Input
|
||||
type Foo2 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) & Bar; // Final comment2
|
||||
type Foo2 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) | Bar; // Final comment2
|
||||
|
||||
// Prettier stable
|
||||
type Foo2 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) &
|
||||
Bar; // Final comment2
|
||||
type Foo2 =
|
||||
| (
|
||||
| "thing1" // Comment1
|
||||
| "thing2"
|
||||
) // Comment2
|
||||
| Bar; // Final comment2
|
||||
|
||||
// Prettier main
|
||||
type Foo2 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) &
|
||||
Bar; // Final comment2
|
||||
type Foo2 =
|
||||
| (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)
|
||||
| Bar; // Final comment2
|
||||
```
|
||||
@@ -20,6 +20,7 @@ import { isObjectProperty } from "../utilities/is-object-property.js";
|
||||
import { isPrettierIgnoreComment } from "../utilities/is-prettier-ignore-comment.js";
|
||||
import { isTypeCastComment } from "../utilities/is-type-cast-comment.js";
|
||||
import {
|
||||
isArrayType,
|
||||
isBinaryCastExpression,
|
||||
isCallLikeExpression,
|
||||
isCallOrNewExpression,
|
||||
@@ -994,7 +995,7 @@ function handleSwitchDefaultCaseComments({
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle `Comment2` and `Comment4`.
|
||||
* Handle `Comment2`, `Comment4`, `Comment6`.
|
||||
*
|
||||
* type Foo = (
|
||||
* | "thing1" // Comment1
|
||||
@@ -1006,6 +1007,11 @@ function handleSwitchDefaultCaseComments({
|
||||
* | "thing2" // Comment4
|
||||
* ) & Bar;
|
||||
*
|
||||
* type Foo = (
|
||||
* | "thing1" // Comment5
|
||||
* | "thing2" // Comment6
|
||||
* ) | Bar;
|
||||
*
|
||||
* @param {CommentContext} context
|
||||
* @returns {boolean}
|
||||
*/
|
||||
@@ -1017,10 +1023,9 @@ function handleLastUnionElementInExpression({
|
||||
}) {
|
||||
if (
|
||||
isUnionType(precedingNode) &&
|
||||
(((enclosingNode.type === "TSArrayType" ||
|
||||
enclosingNode.type === "ArrayTypeAnnotation") &&
|
||||
!followingNode) ||
|
||||
isIntersectionType(enclosingNode))
|
||||
((isArrayType(enclosingNode) && !followingNode) ||
|
||||
isIntersectionType(enclosingNode) ||
|
||||
isUnionType(enclosingNode))
|
||||
) {
|
||||
addTrailingComment(precedingNode.types.at(-1), comment);
|
||||
return true;
|
||||
|
||||
@@ -132,4 +132,9 @@ export const isChainElementWrapper = createTypeCheckFunction([
|
||||
"TSNonNullExpression",
|
||||
]);
|
||||
|
||||
export const isArrayType = createTypeCheckFunction([
|
||||
"TSArrayType",
|
||||
"ArrayTypeAnnotation",
|
||||
]);
|
||||
|
||||
export * from "./literal.js";
|
||||
@@ -22,37 +22,6 @@ 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"]
|
||||
|
||||
@@ -1,36 +1,5 @@
|
||||
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
|
||||
|
||||
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"]
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) & Bar; // Final comment2
|
||||
@@ -208,7 +208,10 @@ type A2 =
|
||||
| c;
|
||||
|
||||
type A4 =
|
||||
| (A | B) // comment 1
|
||||
| (
|
||||
| A
|
||||
| B // comment 1
|
||||
)
|
||||
// comment2
|
||||
| (A | B);
|
||||
|
||||
|
||||
@@ -380,6 +380,49 @@ type B2 =
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`union-last-comment.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript", "flow"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) & Bar; // Final comment2
|
||||
|
||||
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
|
||||
|
||||
type Foo =
|
||||
| (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)
|
||||
| Bar; // Final comment2
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`within-tuple.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript", "flow"]
|
||||
|
||||
+5
@@ -7,3 +7,8 @@ type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) & Bar; // Final comment2
|
||||
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) | Bar; // Final comment2
|
||||
Reference in new issue
Block a user