Add ability for flow generics to break (#1041)

While looking at an instability on the React codebase ( ba1299acc2 (diff-2550aa3d377452ae29361f5e53c51c10) ), I realized that we don't let them break which makes the code look weird.

So I added the ability for them to break :)
master
Christopher Chedeau 2017-03-20 09:35:30 -07:00 committed by James Long
parent 82d6e0b4ab
commit a502bf9f45
6 changed files with 72 additions and 8 deletions

View File

@ -1574,7 +1574,7 @@ function genericPrintNoParens(path, options, print) {
parts.push(path.call(print, "typeParameters"));
parts.push(group(printFunctionParams(path, print, options)));
parts.push(printFunctionParams(path, print, options));
// The returnType is not wrapped in a TypeAnnotation, so the colon
// needs to be added separately.
@ -1587,7 +1587,7 @@ function genericPrintNoParens(path, options, print) {
);
}
return concat(parts);
return group(concat(parts));
case "FunctionTypeParam":
return concat([
path.call(print, "name"),
@ -1766,8 +1766,28 @@ function genericPrintNoParens(path, options, print) {
")"
]);
case "TypeParameterDeclaration":
case "TypeParameterInstantiation":
return concat(["<", join(", ", path.map(print, "params")), ">"]);
case "TypeParameterInstantiation": {
const shouldInline =
n.params.length === 1 &&
n.params[0].type === "ObjectTypeAnnotation";
if (shouldInline) {
return concat(["<", join(", ", path.map(print, "params")), ">"]);
}
return group(concat([
"<",
indent(
options.tabWidth,
concat([
softline,
join(concat([",", line]), path.map(print, "params")),
])
),
softline,
">"
]));
}
case "TypeParameter":
switch (n.variance) {
case "plus":

View File

@ -12,7 +12,12 @@ declare var $React: $Exports<\\"react\\">; // fake import
// Strawman: revised definition of $jsx (alternatively, React.Element).
// Using bounded poly to specify a constraint on a type parameter, and
// existentials to elide type arguments.
type _ReactElement<DefaultProps, Props, Config: $Diff<Props, DefaultProps>, C: $React.Component<DefaultProps, Props, any>> = $React.Element<Config>;
type _ReactElement<
DefaultProps,
Props,
Config: $Diff<Props, DefaultProps>,
C: $React.Component<DefaultProps, Props, any>
> = $React.Element<Config>;
type $jsx<C> = _ReactElement<*, *, *, C>;
"
`;

View File

@ -88,9 +88,9 @@ declare class Promise<R> {
static all<T>(promises: Array<?Promise<T> | T>): Promise<Array<T>>,
static race<T>(promises: Array<Promise<T>>): Promise<T>,
static allObject<T: Object>(promisesByKey: T): Promise<{
[key: $Keys<T>]: any
}>
static allObject<T: Object>(
promisesByKey: T
): Promise<{ [key: $Keys<T>]: any }>
}
"
`;

View File

@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`break.js 1`] = `
"var X = {
perform: function<
A, B, C, D, E, F, G,
T: (a: A, b: B, c: C, d: D, e: E, f: F) => G // eslint-disable-line space-before-function-paren
>(
method: T, scope: any,
a: A, b: B, c: C, d: D, e: E, f: F,
): G {
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var X = {
perform: function<
A,
B,
C,
D,
E,
F,
G,
T: (a: A, b: B, c: C, d: D, e: E, f: F) => G // eslint-disable-line space-before-function-paren
>(method: T, scope: any, a: A, b: B, c: C, d: D, e: E, f: F): G {}
};
"
`;

View File

@ -0,0 +1,10 @@
var X = {
perform: function<
A, B, C, D, E, F, G,
T: (a: A, b: B, c: C, d: D, e: E, f: F) => G // eslint-disable-line space-before-function-paren
>(
method: T, scope: any,
a: A, b: B, c: C, d: D, e: E, f: F,
): G {
}
}

View File

@ -0,0 +1 @@
run_spec(__dirname);