Skip to content

Commit

Permalink
fix formatting of union type as arrow function return type (#6896)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorn0 authored and lydell committed Nov 9, 2019
1 parent 8c3efeb commit 58c6b42
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
22 changes: 22 additions & 0 deletions changelog_unreleased/typescript/pr-6896.md
@@ -0,0 +1,22 @@
#### Fix formatting of union type as arrow function return type ([#6896](https://github.com/prettier/prettier/pull/6896) by [@thorn0](https://github.com/thorn0))

<!-- prettier-ignore -->
```jsx
// Input
export const getVehicleDescriptor = async (
vehicleId: string,
): Promise<Collections.Parts.PrintedCircuitBoardAssembly['attributes'] | undefined> => {}

// Prettier stable
export const getVehicleDescriptor = async (
vehicleId: string
): Promise<| Collections.Parts.PrintedCircuitBoardAssembly["attributes"]
| undefined> => {};

// Prettier master
export const getVehicleDescriptor = async (
vehicleId: string
): Promise<
Collections.Parts.PrintedCircuitBoardAssembly["attributes"] | undefined
> => {};
```
3 changes: 3 additions & 0 deletions src/language-js/printer-estree.js
Expand Up @@ -4587,10 +4587,13 @@ function printTypeParameters(path, options, print, paramsKey) {
(n[paramsKey][0].type === "TSTypeReference" &&
shouldHugType(n[paramsKey][0].typeName)) ||
n[paramsKey][0].type === "NullableTypeAnnotation" ||
// See https://github.com/prettier/prettier/pull/6467 for the context.
(greatGreatGrandParent &&
greatGreatGrandParent.type === "VariableDeclarator" &&
grandparent &&
grandparent.type === "TSTypeAnnotation" &&
n[paramsKey][0].type !== "TSUnionType" &&
n[paramsKey][0].type !== "UnionTypeAnnotation" &&
n[paramsKey][0].type !== "TSConditionalType" &&
n[paramsKey][0].type !== "TSMappedType")));

Expand Down
33 changes: 33 additions & 0 deletions tests/typescript_generic/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`arrow-return-type.ts 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
export const getVehicleDescriptor = async (
vehicleId: string,
): Promise<
Descriptor | undefined
> => {}
export const getVehicleDescriptor = async (
vehicleId: string,
): Promise<
Collections.Parts.PrintedCircuitBoardAssembly['attributes'] | undefined
> => {}
=====================================output=====================================
export const getVehicleDescriptor = async (
vehicleId: string
): Promise<Descriptor | undefined> => {};
export const getVehicleDescriptor = async (
vehicleId: string
): Promise<
Collections.Parts.PrintedCircuitBoardAssembly["attributes"] | undefined
> => {};
================================================================================
`;
exports[`object-method.ts 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down
12 changes: 12 additions & 0 deletions tests/typescript_generic/arrow-return-type.ts
@@ -0,0 +1,12 @@
export const getVehicleDescriptor = async (
vehicleId: string,
): Promise<
Descriptor | undefined
> => {}


export const getVehicleDescriptor = async (
vehicleId: string,
): Promise<
Collections.Parts.PrintedCircuitBoardAssembly['attributes'] | undefined
> => {}

0 comments on commit 58c6b42

Please sign in to comment.