Skip to content

Commit

Permalink
Fix flow union params (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonLaster authored and vjeux committed Apr 14, 2017
1 parent 97c662b commit 2078225
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/printer.js
Expand Up @@ -2302,12 +2302,32 @@ function printFunctionParams(path, print, options, expandArg) {
}

const parent = path.getParentNode();

const flowTypeAnnotations = [
"AnyTypeAnnotation",
"NullLiteralTypeAnnotation",
"NullableTypeAnnotation",
"GenericTypeAnnotation",
"ThisTypeAnnotation",
"NumberTypeAnnotation",
"VoidTypeAnnotation",
"NullTypeAnnotation",
"EmptyTypeAnnotation",
"MixedTypeAnnotation",
"BooleanTypeAnnotation",
"BooleanLiteralTypeAnnotation",
"StringLiteralTypeAnnotation",
"StringTypeAnnotation"
];

const isFlowShorthandWithOneArg =
(isObjectTypePropertyAFunction(parent) ||
isTypeAnnotationAFunction(parent) ||
parent.type === "TypeAlias") &&
fun[paramsField].length === 1 &&
fun[paramsField][0].name === null &&
fun[paramsField][0].typeAnnotation &&
flowTypeAnnotations.indexOf(fun[paramsField][0].typeAnnotation.type) !== -1 &&
!fun.rest;

return concat([
Expand Down
7 changes: 7 additions & 0 deletions tests/flow_return_arrow/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`issue-1249.js 1`] = `
type Bar = ( number | string ) => number;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type Bar = (number | string) => number;
`;

exports[`parens.js 1`] = `
const f = (): (string => string) => {};
const f = (): (a | string => string) => {};
Expand Down
1 change: 1 addition & 0 deletions tests/flow_return_arrow/issue-1249.js
@@ -0,0 +1 @@
type Bar = ( number | string ) => number;

0 comments on commit 2078225

Please sign in to comment.