Fix optional flow parenthesis (#1357)

In #1251, we now have a proper whitelist of all the types that should have parenthesis. Turns out, it included NullableTypeAnnotation which is `?a`. For `?a => void` this wasn't needed but for `(?(a => b)) => c` it was! It's better to always put it anyway if it's not just a simple literal.

I've added tests for all the combinations I could think of, so we'll catch regressions if they happen.

Fixes #1353
master
Christopher Chedeau 2017-04-24 13:58:30 -07:00 committed by James Long
parent 689e520abe
commit a7319dbbbb
4 changed files with 68 additions and 6 deletions

View File

@ -2401,7 +2401,6 @@ function printFunctionParams(path, print, options, expandArg) {
const flowTypeAnnotations = [
"AnyTypeAnnotation",
"NullLiteralTypeAnnotation",
"NullableTypeAnnotation",
"GenericTypeAnnotation",
"ThisTypeAnnotation",
"NumberTypeAnnotation",
@ -2411,14 +2410,17 @@ function printFunctionParams(path, print, options, expandArg) {
"MixedTypeAnnotation",
"BooleanTypeAnnotation",
"BooleanLiteralTypeAnnotation",
"StringLiteralTypeAnnotation",
"StringTypeAnnotation"
];
const isFlowShorthandWithOneArg =
(isObjectTypePropertyAFunction(parent) ||
isTypeAnnotationAFunction(parent) ||
parent.type === "TypeAlias") &&
parent.type === "TypeAlias" ||
parent.type === "UnionTypeAnnotation" ||
parent.type === "IntersectionTypeAnnotation" ||
(parent.type === "FunctionTypeAnnotation" &&
parent.returnType === fun)) &&
fun[paramsField].length === 1 &&
fun[paramsField][0].name === null &&
fun[paramsField][0].typeAnnotation &&

View File

@ -75,7 +75,7 @@ type f = /* comment */ arg => void;
type f = arg /* comment */ => void;
type f = ?arg => void;
type f = (?arg) => void;
class X {
constructor(

View File

@ -2,8 +2,48 @@
exports[`issue-1249.js 1`] = `
type Bar = ( number | string ) => number;
type X = (?(number, number) => number) => void;
type X = ?((number, number) => number) => void;
type X = ?(number, number) => number => void;
type X = 1234 => void;
type X = 'abc' => void;
type X = true => void;
type X = false => void;
type X = boolean => void;
type X = number => void;
type X = void => void;
type X = null => void;
type X = any => void;
type X = empty => void;
type X = mixed => void;
type X = string => void;
type X = abc => void;
type X = a | b => void;
type X = (a | b) => void;
type X = a & b => void;
type X = (a & b) => void;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type Bar = (number | string) => number;
type X = (?(number, number) => number) => void;
type X = ?((number, number) => number) => void;
type X = ?(number, number) => number => void;
type X = (1234) => void;
type X = ("abc") => void;
type X = true => void;
type X = false => void;
type X = boolean => void;
type X = number => void;
type X = void => void;
type X = null => void;
type X = any => void;
type X = empty => void;
type X = mixed => void;
type X = string => void;
type X = abc => void;
type X = a | (b => void);
type X = (a | b) => void;
type X = a & (b => void);
type X = (a & b) => void;
`;
@ -14,8 +54,8 @@ const f = (): (a & string => string) => {};
function f(): string => string {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const f = (): (string => string) => {};
const f = (): a | ((string) => string) => {};
const f = (): a & ((string) => string) => {};
const f = (): a | (string => string) => {};
const f = (): a & (string => string) => {};
function f(): string => string {}
`;

View File

@ -1 +1,21 @@
type Bar = ( number | string ) => number;
type X = (?(number, number) => number) => void;
type X = ?((number, number) => number) => void;
type X = ?(number, number) => number => void;
type X = 1234 => void;
type X = 'abc' => void;
type X = true => void;
type X = false => void;
type X = boolean => void;
type X = number => void;
type X = void => void;
type X = null => void;
type X = any => void;
type X = empty => void;
type X = mixed => void;
type X = string => void;
type X = abc => void;
type X = a | b => void;
type X = (a | b) => void;
type X = a & b => void;
type X = (a & b) => void;