Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix formatting of union type as arrow function return type #6896

Merged
merged 2 commits into from Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
> => {}