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 #1353master
parent
689e520abe
commit
a7319dbbbb
|
@ -2401,7 +2401,6 @@ function printFunctionParams(path, print, options, expandArg) {
|
||||||
const flowTypeAnnotations = [
|
const flowTypeAnnotations = [
|
||||||
"AnyTypeAnnotation",
|
"AnyTypeAnnotation",
|
||||||
"NullLiteralTypeAnnotation",
|
"NullLiteralTypeAnnotation",
|
||||||
"NullableTypeAnnotation",
|
|
||||||
"GenericTypeAnnotation",
|
"GenericTypeAnnotation",
|
||||||
"ThisTypeAnnotation",
|
"ThisTypeAnnotation",
|
||||||
"NumberTypeAnnotation",
|
"NumberTypeAnnotation",
|
||||||
|
@ -2411,14 +2410,17 @@ function printFunctionParams(path, print, options, expandArg) {
|
||||||
"MixedTypeAnnotation",
|
"MixedTypeAnnotation",
|
||||||
"BooleanTypeAnnotation",
|
"BooleanTypeAnnotation",
|
||||||
"BooleanLiteralTypeAnnotation",
|
"BooleanLiteralTypeAnnotation",
|
||||||
"StringLiteralTypeAnnotation",
|
|
||||||
"StringTypeAnnotation"
|
"StringTypeAnnotation"
|
||||||
];
|
];
|
||||||
|
|
||||||
const isFlowShorthandWithOneArg =
|
const isFlowShorthandWithOneArg =
|
||||||
(isObjectTypePropertyAFunction(parent) ||
|
(isObjectTypePropertyAFunction(parent) ||
|
||||||
isTypeAnnotationAFunction(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].length === 1 &&
|
||||||
fun[paramsField][0].name === null &&
|
fun[paramsField][0].name === null &&
|
||||||
fun[paramsField][0].typeAnnotation &&
|
fun[paramsField][0].typeAnnotation &&
|
||||||
|
|
|
@ -75,7 +75,7 @@ type f = /* comment */ arg => void;
|
||||||
|
|
||||||
type f = arg /* comment */ => void;
|
type f = arg /* comment */ => void;
|
||||||
|
|
||||||
type f = ?arg => void;
|
type f = (?arg) => void;
|
||||||
|
|
||||||
class X {
|
class X {
|
||||||
constructor(
|
constructor(
|
||||||
|
|
|
@ -2,8 +2,48 @@
|
||||||
|
|
||||||
exports[`issue-1249.js 1`] = `
|
exports[`issue-1249.js 1`] = `
|
||||||
type Bar = ( number | string ) => number;
|
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 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 {}
|
function f(): string => string {}
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
const 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 {}
|
function f(): string => string {}
|
||||||
|
|
||||||
`;
|
`;
|
||||||
|
|
|
@ -1 +1,21 @@
|
||||||
type Bar = ( number | string ) => number;
|
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;
|
||||||
|
|
Loading…
Reference in New Issue