Skip to content

Commit 58c6b42

Browse files
thorn0lydell
authored andcommitted
fix formatting of union type as arrow function return type (prettier#6896)
1 parent 8c3efeb commit 58c6b42

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#### Fix formatting of union type as arrow function return type ([#6896](https://github.com/prettier/prettier/pull/6896) by [@thorn0](https://github.com/thorn0))
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
export const getVehicleDescriptor = async (
7+
vehicleId: string,
8+
): Promise<Collections.Parts.PrintedCircuitBoardAssembly['attributes'] | undefined> => {}
9+
10+
// Prettier stable
11+
export const getVehicleDescriptor = async (
12+
vehicleId: string
13+
): Promise<| Collections.Parts.PrintedCircuitBoardAssembly["attributes"]
14+
| undefined> => {};
15+
16+
// Prettier master
17+
export const getVehicleDescriptor = async (
18+
vehicleId: string
19+
): Promise<
20+
Collections.Parts.PrintedCircuitBoardAssembly["attributes"] | undefined
21+
> => {};
22+
```

src/language-js/printer-estree.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4587,10 +4587,13 @@ function printTypeParameters(path, options, print, paramsKey) {
45874587
(n[paramsKey][0].type === "TSTypeReference" &&
45884588
shouldHugType(n[paramsKey][0].typeName)) ||
45894589
n[paramsKey][0].type === "NullableTypeAnnotation" ||
4590+
// See https://github.com/prettier/prettier/pull/6467 for the context.
45904591
(greatGreatGrandParent &&
45914592
greatGreatGrandParent.type === "VariableDeclarator" &&
45924593
grandparent &&
45934594
grandparent.type === "TSTypeAnnotation" &&
4595+
n[paramsKey][0].type !== "TSUnionType" &&
4596+
n[paramsKey][0].type !== "UnionTypeAnnotation" &&
45944597
n[paramsKey][0].type !== "TSConditionalType" &&
45954598
n[paramsKey][0].type !== "TSMappedType")));
45964599

tests/typescript_generic/__snapshots__/jsfmt.spec.js.snap

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`arrow-return-type.ts 1`] = `
4+
====================================options=====================================
5+
parsers: ["typescript"]
6+
printWidth: 80
7+
| printWidth
8+
=====================================input======================================
9+
export const getVehicleDescriptor = async (
10+
vehicleId: string,
11+
): Promise<
12+
Descriptor | undefined
13+
> => {}
14+
15+
16+
export const getVehicleDescriptor = async (
17+
vehicleId: string,
18+
): Promise<
19+
Collections.Parts.PrintedCircuitBoardAssembly['attributes'] | undefined
20+
> => {}
21+
22+
=====================================output=====================================
23+
export const getVehicleDescriptor = async (
24+
vehicleId: string
25+
): Promise<Descriptor | undefined> => {};
26+
27+
export const getVehicleDescriptor = async (
28+
vehicleId: string
29+
): Promise<
30+
Collections.Parts.PrintedCircuitBoardAssembly["attributes"] | undefined
31+
> => {};
32+
33+
================================================================================
34+
`;
35+
336
exports[`object-method.ts 1`] = `
437
====================================options=====================================
538
parsers: ["typescript"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const getVehicleDescriptor = async (
2+
vehicleId: string,
3+
): Promise<
4+
Descriptor | undefined
5+
> => {}
6+
7+
8+
export const getVehicleDescriptor = async (
9+
vehicleId: string,
10+
): Promise<
11+
Collections.Parts.PrintedCircuitBoardAssembly['attributes'] | undefined
12+
> => {}

0 commit comments

Comments
 (0)