diff --git a/src/language-js/print/union-type.js b/src/language-js/print/union-type.js index 0b1da7ab9..34e26a49e 100644 --- a/src/language-js/print/union-type.js +++ b/src/language-js/print/union-type.js @@ -97,7 +97,11 @@ function shouldIndentUnionType(path) { (key === "elementTypes" && isTupleType(parent)) || ((key === "trueType" || key === "falseType") && isConditionalType(parent)) || - (key === "params" && isTypeParameterInstantiation(parent)) + (key === "params" && isTypeParameterInstantiation(parent)) || + (key === "typeAnnotation" && + parent.type === "FunctionTypeParam" && + !parent.name && + path.grandparent.this !== parent) ) { return false; } diff --git a/tests/format/flow/this-annotation/__snapshots__/format.test.js.snap b/tests/format/flow/this-annotation/__snapshots__/format.test.js.snap index 1d7877785..264541e52 100644 --- a/tests/format/flow/this-annotation/__snapshots__/format.test.js.snap +++ b/tests/format/flow/this-annotation/__snapshots__/format.test.js.snap @@ -176,32 +176,54 @@ trailingComma: "all" =====================================input====================================== // @flow -type A = ( - this: | SupperLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, +type A1 = ( + this: | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, b: number, ) => boolean; -type B = ( - _this: | SupperLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, +type A2 = ( + _this: | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, + b: number, +) => boolean + +type A3 = ( + | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, + b: number, +) => boolean + +type A4 = ( + /* this: */ | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, b: number, ) => boolean =====================================output===================================== // @flow -type A = ( +type A1 = ( this: - | SupperLongLongLongLongLongLongLongLongLongLongLongType + | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, b: number, ) => boolean; -type B = ( +type A2 = ( _this: - | SupperLongLongLongLongLongLongLongLongLongLongLongType + | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, b: number, ) => boolean; +type A3 = ( + | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType + | FooBarBazLorem12345, + b: number, +) => boolean; + +type A4 = ( + /* this: */ | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType + | FooBarBazLorem12345, + b: number, +) => boolean; + ================================================================================ `; diff --git a/tests/format/flow/this-annotation/union_type.js b/tests/format/flow/this-annotation/union_type.js index cf502b970..3260ea5df 100644 --- a/tests/format/flow/this-annotation/union_type.js +++ b/tests/format/flow/this-annotation/union_type.js @@ -1,11 +1,21 @@ // @flow -type A = ( - this: | SupperLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, +type A1 = ( + this: | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, b: number, ) => boolean; -type B = ( - _this: | SupperLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, +type A2 = ( + _this: | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, + b: number, +) => boolean + +type A3 = ( + | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, + b: number, +) => boolean + +type A4 = ( + /* this: */ | SupperLongLongLongLongLongLongLongLongLongLongLongLongLongType | FooBarBazLorem12345, b: number, ) => boolean diff --git a/tests/format/flow/union/__snapshots__/format.test.js.snap b/tests/format/flow/union/__snapshots__/format.test.js.snap index 427fc17a9..4f9575673 100644 --- a/tests/format/flow/union/__snapshots__/format.test.js.snap +++ b/tests/format/flow/union/__snapshots__/format.test.js.snap @@ -22,6 +22,85 @@ const myValue = (callcallcallcallcallcall( ================================================================================ `; +exports[`function-type-param.js format 1`] = ` +====================================options===================================== +parsers: ["flow"] + printWidth: 80 (default) | +=====================================input====================================== +type a1 = ( + | {// + } + | {// + }, + ) => void + +type a2 = ( +// Comment + | {// + } + | {// + }, + ) => void + +hook useRefetchFunction( + dispatch: ( + | { + environment: IEnvironment, + fragmentIdentifier: string, + type: 'reset', + } + | { + fetchPolicy?: FetchPolicy, + onComplete?: (Error | null) => void, + refetchEnvironment: ?IEnvironment, + refetchQuery: OperationDescriptor, + renderPolicy?: RenderPolicy, + type: 'refetch', + }, + ) => void, +) {} + +=====================================output===================================== +type a1 = ( + | { + // + } + | { + // + }, +) => void; + +type a2 = ( + // Comment + | { + // + } + | { + // + }, +) => void; + +hook useRefetchFunction( + dispatch: ( + | { + environment: IEnvironment, + fragmentIdentifier: string, + type: "reset", + } + | { + fetchPolicy?: FetchPolicy, + onComplete?: (Error | null) => void, + refetchEnvironment: ?IEnvironment, + refetchQuery: OperationDescriptor, + renderPolicy?: RenderPolicy, + type: "refetch", + }, + ) => void, +) {} + +================================================================================ +`; + exports[`issue-1197-2.js format 1`] = ` ====================================options===================================== parsers: ["flow"] diff --git a/tests/format/flow/union/function-type-param.js b/tests/format/flow/union/function-type-param.js new file mode 100644 index 000000000..d0b50e27e --- /dev/null +++ b/tests/format/flow/union/function-type-param.js @@ -0,0 +1,32 @@ +type a1 = ( + | {// + } + | {// + }, + ) => void + +type a2 = ( +// Comment + | {// + } + | {// + }, + ) => void + +hook useRefetchFunction( + dispatch: ( + | { + environment: IEnvironment, + fragmentIdentifier: string, + type: 'reset', + } + | { + fetchPolicy?: FetchPolicy, + onComplete?: (Error | null) => void, + refetchEnvironment: ?IEnvironment, + refetchQuery: OperationDescriptor, + renderPolicy?: RenderPolicy, + type: 'refetch', + }, + ) => void, +) {} diff --git a/tests/format/typescript/union/consistent-with-flow/__snapshots__/format.test.js.snap b/tests/format/typescript/union/consistent-with-flow/__snapshots__/format.test.js.snap index fc3350529..21eff6eb5 100644 --- a/tests/format/typescript/union/consistent-with-flow/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/union/consistent-with-flow/__snapshots__/format.test.js.snap @@ -257,6 +257,52 @@ type A = T extends "arrow" ================================================================================ `; +exports[`function-type-param.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] + printWidth: 80 (default) | +=====================================input====================================== +type a1 = ( + a: | {// + } + | {// + }, + ) => void + +type a2 = ( +a: +// Comment + | {// + } + | {// + }, + ) => void + +=====================================output===================================== +type a1 = ( + a: + | { + // + } + | { + // + }, +) => void; + +type a2 = ( + a: + // Comment + | { + // + } + | { + // + }, +) => void; + +================================================================================ +`; + exports[`in-tuple.ts format 1`] = ` ====================================options===================================== parsers: ["typescript", "flow"] diff --git a/tests/format/typescript/union/consistent-with-flow/function-type-param.ts b/tests/format/typescript/union/consistent-with-flow/function-type-param.ts new file mode 100644 index 000000000..ad7f70042 --- /dev/null +++ b/tests/format/typescript/union/consistent-with-flow/function-type-param.ts @@ -0,0 +1,15 @@ +type a1 = ( + a: | {// + } + | {// + }, + ) => void + +type a2 = ( +a: +// Comment + | {// + } + | {// + }, + ) => void