From 58c6b42aa80ea8681c3b8aaaf8f891c9e4f15e51 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Sat, 9 Nov 2019 12:47:54 +0200 Subject: [PATCH] fix formatting of union type as arrow function return type (#6896) --- changelog_unreleased/typescript/pr-6896.md | 22 +++++++++++++ src/language-js/printer-estree.js | 3 ++ .../__snapshots__/jsfmt.spec.js.snap | 33 +++++++++++++++++++ tests/typescript_generic/arrow-return-type.ts | 12 +++++++ 4 files changed, 70 insertions(+) create mode 100644 changelog_unreleased/typescript/pr-6896.md create mode 100644 tests/typescript_generic/arrow-return-type.ts diff --git a/changelog_unreleased/typescript/pr-6896.md b/changelog_unreleased/typescript/pr-6896.md new file mode 100644 index 000000000000..e5cfea359fa5 --- /dev/null +++ b/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)) + + +```jsx +// Input +export const getVehicleDescriptor = async ( + vehicleId: string, +): Promise => {} + +// 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 +> => {}; +``` diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 82967e586c71..14a3fa9dacd1 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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"))); diff --git a/tests/typescript_generic/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_generic/__snapshots__/jsfmt.spec.js.snap index 3009a3eb7513..30c3a0efaba7 100644 --- a/tests/typescript_generic/__snapshots__/jsfmt.spec.js.snap +++ b/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 => {}; + +export const getVehicleDescriptor = async ( + vehicleId: string +): Promise< + Collections.Parts.PrintedCircuitBoardAssembly["attributes"] | undefined +> => {}; + +================================================================================ +`; + exports[`object-method.ts 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/typescript_generic/arrow-return-type.ts b/tests/typescript_generic/arrow-return-type.ts new file mode 100644 index 000000000000..0d05ca494782 --- /dev/null +++ b/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 +> => {}