From bafd72418e27bcbf427bd64ad172f3c23fcffd7f Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 1 May 2017 14:39:46 -0700 Subject: [PATCH] Don't break for unparenthesised single argument flow function (#1452) This has been reported internally. It looks --very-- weird to have it indented the way it was. --- src/printer.js | 9 ++-- .../__snapshots__/jsfmt.spec.js.snap | 50 ++++++++++++++----- tests/flow_function_parentheses/single.js | 7 +++ 3 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 tests/flow_function_parentheses/single.js diff --git a/src/printer.js b/src/printer.js index db28253d..e38f2531 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2651,19 +2651,22 @@ function printFunctionParams(path, print, options, expandArg) { flowTypeAnnotations.indexOf(fun[paramsField][0].typeAnnotation.type) !== -1 && !fun.rest; + if (isFlowShorthandWithOneArg) { + return concat(printed); + } + const canHaveTrailingComma = - !isFlowShorthandWithOneArg && !(lastParam && lastParam.type === "RestElement") && !fun.rest; return concat([ - isFlowShorthandWithOneArg ? "" : "(", + "(", indent(concat([softline, join(concat([",", line]), printed)])), ifBreak( canHaveTrailingComma && shouldPrintComma(options, "all") ? "," : "" ), softline, - isFlowShorthandWithOneArg ? "" : ")" + ")" ]); } diff --git a/tests/flow_function_parentheses/__snapshots__/jsfmt.spec.js.snap b/tests/flow_function_parentheses/__snapshots__/jsfmt.spec.js.snap index ced64952..bd82002a 100644 --- a/tests/flow_function_parentheses/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow_function_parentheses/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,39 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`single.js 1`] = ` +const selectorByPath: + Path + => SomethingSelector< + SomethingUEditorContextType, + SomethingUEditorContextType, + SomethingBulkValue +> = memoizeWithArgs(/* ... */) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +const selectorByPath: Path => SomethingSelector< + SomethingUEditorContextType, + SomethingUEditorContextType, + SomethingBulkValue +> = memoizeWithArgs(/* ... */); + +`; + +exports[`single.js 2`] = ` +const selectorByPath: + Path + => SomethingSelector< + SomethingUEditorContextType, + SomethingUEditorContextType, + SomethingBulkValue +> = memoizeWithArgs(/* ... */) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +const selectorByPath: Path => SomethingSelector< + SomethingUEditorContextType, + SomethingUEditorContextType, + SomethingBulkValue, +> = memoizeWithArgs(/* ... */); + +`; + exports[`test.js 1`] = ` type Banana = { eat: string => boolean, @@ -79,16 +113,12 @@ type f = (?arg) => void; class X { constructor( - ideConnectionFactory: - child_process$ChildProcess - => FlowIDEConnection = defaultIDEConnectionFactory + ideConnectionFactory: child_process$ChildProcess => FlowIDEConnection = defaultIDEConnectionFactory ) {} } interface F { - ideConnectionFactoryLongLongLong: - child_process$ChildProcess - => FlowIDEConnection + ideConnectionFactoryLongLongLong: child_process$ChildProcess => FlowIDEConnection } `; @@ -172,16 +202,12 @@ type f = (?arg) => void; class X { constructor( - ideConnectionFactory: - child_process$ChildProcess - => FlowIDEConnection = defaultIDEConnectionFactory, + ideConnectionFactory: child_process$ChildProcess => FlowIDEConnection = defaultIDEConnectionFactory, ) {} } interface F { - ideConnectionFactoryLongLongLong: - child_process$ChildProcess - => FlowIDEConnection, + ideConnectionFactoryLongLongLong: child_process$ChildProcess => FlowIDEConnection, } `; diff --git a/tests/flow_function_parentheses/single.js b/tests/flow_function_parentheses/single.js new file mode 100644 index 00000000..162ae0a1 --- /dev/null +++ b/tests/flow_function_parentheses/single.js @@ -0,0 +1,7 @@ +const selectorByPath: + Path + => SomethingSelector< + SomethingUEditorContextType, + SomethingUEditorContextType, + SomethingBulkValue +> = memoizeWithArgs(/* ... */)