From 188c466cd5190c282a0b006a103711bf850eaaf8 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sun, 16 Jul 2017 19:53:01 +1000 Subject: [PATCH] Don't call shouldHugType for function arguments, fixes #2458 (#2496) --- src/printer.js | 7 ++-- .../__snapshots__/jsfmt.spec.js.snap | 36 +++++++++++++++++++ tests/flow_union/union.js | 13 +++++++ .../__snapshots__/jsfmt.spec.js.snap | 26 ++++++++++++++ tests/function_single_destructuring/test.js | 13 +++++++ 5 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/printer.js b/src/printer.js index 11d28468..1caba9d0 100644 --- a/src/printer.js +++ b/src/printer.js @@ -4779,7 +4779,7 @@ function isNodeStartingWithDeclare(node, options) { } function shouldHugType(node) { - if (node.type === "ObjectTypeAnnotation" || node.type === "TSTypeLiteral") { + if (isObjectType(node)) { return true; } @@ -4805,6 +4805,7 @@ function shouldHugType(node) { return true; } } + return false; } @@ -4818,9 +4819,9 @@ function shouldHugArguments(fun) { (fun.params[0].type === "Identifier" && fun.params[0].typeAnnotation && fun.params[0].typeAnnotation.type === "TypeAnnotation" && - shouldHugType(fun.params[0].typeAnnotation.typeAnnotation)) || + isObjectType(fun.params[0].typeAnnotation.typeAnnotation)) || (fun.params[0].type === "FunctionTypeParam" && - shouldHugType(fun.params[0].typeAnnotation))) && + isObjectType(fun.params[0].typeAnnotation))) && !fun.rest ); } diff --git a/tests/flow_union/__snapshots__/jsfmt.spec.js.snap b/tests/flow_union/__snapshots__/jsfmt.spec.js.snap index a1cde89e..8fbf7eff 100644 --- a/tests/flow_union/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow_union/__snapshots__/jsfmt.spec.js.snap @@ -14,6 +14,19 @@ interface RelayProps { } export function aPrettyLongFunction(aRatherLongParamName: string | null): string {} + +export function aPrettyLongFunctionA(aRatherLongParameterName: {} | null): string[] {} +export function aPrettyLongFunctionB(aRatherLongParameterName: Function | null): string[] {} +export interface MyInterface {} +export function aPrettyLongFunctionC(aRatherLongParameterName: MyInterface | null): string[] {} +export type MyType = MyInterface +export function aPrettyLongFunctionD(aRatherLongParameterName: MyType | null): string[] {} + +export function aShortFn(aShortParmName: MyType | null): string[] {} + +export function aPrettyLongFunctionE(aRatherLongParameterName: Array<{ + __id: string, +} | null> | null | void): string[] {} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ interface RelayProps { articles: @@ -35,4 +48,27 @@ export function aPrettyLongFunction( aRatherLongParamName: string | null ): string {} +export function aPrettyLongFunctionA( + aRatherLongParameterName: {} | null +): string[] {} +export function aPrettyLongFunctionB( + aRatherLongParameterName: Function | null +): string[] {} +export interface MyInterface {} +export function aPrettyLongFunctionC( + aRatherLongParameterName: MyInterface | null +): string[] {} +export type MyType = MyInterface; +export function aPrettyLongFunctionD( + aRatherLongParameterName: MyType | null +): string[] {} + +export function aShortFn(aShortParmName: MyType | null): string[] {} + +export function aPrettyLongFunctionE( + aRatherLongParameterName: Array<{ + __id: string + } | null> | null | void +): string[] {} + `; diff --git a/tests/flow_union/union.js b/tests/flow_union/union.js index deba0b01..8333e78b 100644 --- a/tests/flow_union/union.js +++ b/tests/flow_union/union.js @@ -11,3 +11,16 @@ interface RelayProps { } export function aPrettyLongFunction(aRatherLongParamName: string | null): string {} + +export function aPrettyLongFunctionA(aRatherLongParameterName: {} | null): string[] {} +export function aPrettyLongFunctionB(aRatherLongParameterName: Function | null): string[] {} +export interface MyInterface {} +export function aPrettyLongFunctionC(aRatherLongParameterName: MyInterface | null): string[] {} +export type MyType = MyInterface +export function aPrettyLongFunctionD(aRatherLongParameterName: MyType | null): string[] {} + +export function aShortFn(aShortParmName: MyType | null): string[] {} + +export function aPrettyLongFunctionE(aRatherLongParameterName: Array<{ + __id: string, +} | null> | null | void): string[] {} diff --git a/tests/function_single_destructuring/__snapshots__/jsfmt.spec.js.snap b/tests/function_single_destructuring/__snapshots__/jsfmt.spec.js.snap index fb8784d4..903173b3 100644 --- a/tests/function_single_destructuring/__snapshots__/jsfmt.spec.js.snap +++ b/tests/function_single_destructuring/__snapshots__/jsfmt.spec.js.snap @@ -14,6 +14,19 @@ function StatelessFunctionalComponent({ return
} +function StatelessFunctionalComponent({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items, +}: MyType | null | void) { + return
+} + const StatelessFunctionalComponent = ({ isActive, onFiltersUpdated, @@ -80,6 +93,19 @@ function StatelessFunctionalComponent({ return
; } +function StatelessFunctionalComponent({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items +}: MyType | null | void) { + return
; +} + const StatelessFunctionalComponent = ({ isActive, onFiltersUpdated, diff --git a/tests/function_single_destructuring/test.js b/tests/function_single_destructuring/test.js index 10137b48..96f53d7e 100644 --- a/tests/function_single_destructuring/test.js +++ b/tests/function_single_destructuring/test.js @@ -11,6 +11,19 @@ function StatelessFunctionalComponent({ return
} +function StatelessFunctionalComponent({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items, +}: MyType | null | void) { + return
+} + const StatelessFunctionalComponent = ({ isActive, onFiltersUpdated,