Fix union type should be printed in the multi-line variant when there are comments (#13860)

Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
Yiming Panandfisker Cheung authored and GitHub committed 2022-11-23 11:33:45 +08:00
1 parent 55fa0e9e0f
commit bcbc6daeec
4 files changed
+106 -1

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
#### Fix union type should be printed in the multi-line variant when there are comments (#13860 by @PerfectPan)
<!-- prettier-ignore -->
```tsx
// Input
type FooBar =
| Number // this documents the first option
| void // this documents the second option
;
// Prettier stable
type FooBar = Number | void; // this documents the first option // this documents the second option
// Prettier main
type FooBar =
| Number // this documents the first option
| void; // this documents the second option
```
+4 -1
View File
@@ -16,6 +16,7 @@ import {
hasLeadingOwnLineComment,
isObjectTypePropertyAFunction,
shouldPrintComma,
hasComment,
} from "../utils/index.js";
import { printAssignment } from "./assignment.js";
import {
@@ -47,7 +48,9 @@ function shouldHugType(node) {
node.type === "TSTypeReference"
);
if (node.types.length - 1 === voidCount && hasObject) {
const hasComments = node.types.some((node) => hasComment(node));
if (node.types.length - 1 === voidCount && hasObject && !hasComments) {
return true;
}
}
@@ -47,6 +47,35 @@ interface RelayProps {
} | null> | null | void,
}
interface RelayProps {
articles: Array<{
__id: string,
} | null>
| null // articles type may be null
| void, // articles type may be void
}
type FooBar = null // null
| { /** x **/
y: number;
z: string;
} // this documents the first option
| void // this documents the second option
;
type FooBarWithoutComment = null
| {
y: number;
z: string;
}
| void
;
type FooBar2 =
| Number // this documents the first option
| void // this documents the second option
;
type UploadState<E, EM, D>
// The upload hasnt begun yet
= {type: "Not_begun"}
@@ -90,6 +119,32 @@ interface RelayProps {
} | null> | null | void;
}
interface RelayProps {
articles:
| Array<{
__id: string;
} | null>
| null // articles type may be null
| void; // articles type may be void
}
type FooBar =
| null // null
| {
/** x **/ y: number;
z: string;
} // this documents the first option
| void; // this documents the second option
type FooBarWithoutComment = null | {
y: number;
z: string;
} | void;
type FooBar2 =
| Number // this documents the first option
| void; // this documents the second option
type UploadState<E, EM, D> =
// The upload hasnt begun yet
| { type: "Not_begun" }
+29
View File
@@ -7,6 +7,35 @@ interface RelayProps {
} | null> | null | void,
}
interface RelayProps {
articles: Array<{
__id: string,
} | null>
| null // articles type may be null
| void, // articles type may be void
}
type FooBar = null // null
| { /** x **/
y: number;
z: string;
} // this documents the first option
| void // this documents the second option
;
type FooBarWithoutComment = null
| {
y: number;
z: string;
}
| void
;
type FooBar2 =
| Number // this documents the first option
| void // this documents the second option
;
type UploadState<E, EM, D>
// The upload hasnt begun yet
= {type: "Not_begun"}