Consistently print function parameters the correct way

master
James Long 2017-01-04 16:52:49 -05:00
parent 588c8ce7cd
commit 898009ba66
16 changed files with 217 additions and 146 deletions

View File

@ -228,7 +228,7 @@ function print(w, doc) {
}
else {
// Expanded states are a rare case where a document
// can manually provide mutliple representations of
// can manually provide multiple representations of
// itself. It provides an array of documents
// going from the least expanded (most flattened)
// representation first to the most expanded. If a

View File

@ -358,17 +358,7 @@ function genericPrintNoParens(path, options, print) {
parts.push(
path.call(print, "typeParameters"),
concat(
[
"(",
indent(
options.tabWidth,
concat([ softline, printFunctionParams(path, print) ])
),
softline,
")"
]
),
printFunctionParams(path, print, options),
printReturnType(path, print),
" ",
path.call(print, "body")
@ -394,9 +384,7 @@ function genericPrintNoParens(path, options, print) {
parts.push(path.call(print, "params", 0));
} else {
parts.push(
"(",
printFunctionParams(path, print),
")",
printFunctionParams(path, print, options),
printReturnType(path, print)
);
}
@ -1408,7 +1396,7 @@ else
parts.push(path.call(print, "typeParameters"));
parts.push("(", printFunctionParams(path, print), ")");
parts.push(printFunctionParams(path, print, options));
// The returnType is not wrapped in a TypeAnnotation, so the colon
// needs to be added separately.
@ -1724,14 +1712,12 @@ function printMethod(path, options, print) {
parts.push(
key,
path.call(print, "value", "typeParameters"),
"(",
path.call(
function(valuePath) {
return printFunctionParams(valuePath, print);
return printFunctionParams(valuePath, print, options);
},
"value"
),
")",
path.call(p => printReturnType(p, print), "value"),
" ",
path.call(print, "value", "body")
@ -1771,7 +1757,7 @@ function printArgumentsList(path, options, print) {
concat([
"(",
join(concat([ ",", line ]), printed.slice(0, -1)),
", ",
printed.length > 1 ? ", " : "",
group(util.getLast(printed), { shouldBreak: true }),
")"
]),
@ -1807,7 +1793,7 @@ function printArgumentsList(path, options, print) {
);
}
function printFunctionParams(path, print) {
function printFunctionParams(path, print, options) {
var fun = path.getValue();
// namedTypes.Function.assert(fun);
var printed = path.map(print, "params");
@ -1830,7 +1816,15 @@ function printFunctionParams(path, print) {
printed.push(concat([ "...", path.call(print, "rest") ]));
}
return join(concat([ ",", line ]), printed);
return concat([
"(",
indent(
options.tabWidth,
concat([ softline, join(concat([ ",", line ]), printed) ])
),
softline,
")"
]);
}
function printObjectMethod(path, options, print) {
@ -1858,9 +1852,7 @@ function printObjectMethod(path, options, print) {
}
parts.push(
"(",
printFunctionParams(path, print),
")",
printReturnType(path, print),
" ",
path.call(print, "body")

View File

@ -1250,68 +1250,102 @@ export const builders: {
emptyStatement(): EmptyStatement;
blockStatement(body: Statement[]): BlockStatement;
expressionStatement(expression: Expression): ExpressionStatement;
ifStatement(test: Expression,
consequent: Statement,
alternate?: Statement): IfStatement;
ifStatement(
test: Expression,
consequent: Statement,
alternate?: Statement
): IfStatement;
breakStatement(label?: Identifier): BreakStatement;
continueStatement(label?: Identifier): ContinueStatement;
returnStatement(argument: ?Expression): ReturnStatement;
throwStatement(argument: ?Expression): ThrowStatement;
whileStatement(test: Expression, body: Statement): WhileStatement;
forStatement(init: ?(VariableDeclaration | Expression),
test: ?Expression,
update: ?Expression,
body: Statement): ForStatement;
forInStatement(left: VariableDeclaration | Expression,
right: Expression,
body: Statement): ForInStatement;
tryStatement(block: BlockStatement,
handler: ?CatchClause,
handlers: CatchClause[],
finalizer?: BlockStatement): TryStatement;
catchClause(param: Pattern,
guard: ?Expression,
body: BlockStatement): CatchClause;
forStatement(
init: ?(VariableDeclaration | Expression),
test: ?Expression,
update: ?Expression,
body: Statement
): ForStatement;
forInStatement(
left: VariableDeclaration | Expression,
right: Expression,
body: Statement
): ForInStatement;
tryStatement(
block: BlockStatement,
handler: ?CatchClause,
handlers: CatchClause[],
finalizer?: BlockStatement
): TryStatement;
catchClause(
param: Pattern,
guard: ?Expression,
body: BlockStatement
): CatchClause;
identifier(name: string): Identifier;
literal(value: ?(string | boolean | number | RegExp),
regex?: { pattern: string; flags: string }): Literal;
literal(
value: ?(string | boolean | number | RegExp),
regex?: { pattern: string; flags: string }
): Literal;
thisExpression(): ThisExpression;
arrayExpression(elements: Expression[]): ArrayExpression;
objectExpreession(properties: Property[]): ObjectExpreession;
property(kind: \"init\" | \"get\" | \"set\",
key: Literal | Identifier,
value: Expression): Property;
functionExpression(id: ?Identifier,
params: Pattern[],
body: BlockStatement): FunctionExpression;
binaryExpression(operator: BinaryOperator,
left: Expression,
right: Expression): BinaryExpression;
unaryExpression(operator: UnaryOperator,
argument: Expression,
prefix: boolean): UnaryExpression;
assignmentExpression(operator: AssignmentOperator,
left: Pattern,
right: Expression): AssignmentExpression;
updateExpression(operator: UpdateOperator,
argument: Expression,
prefix: boolean): UpdateExpression;
logicalExpression(operator: LogicalOperator,
left: Expression,
right: Expression): LogicalExpression;
conditionalExpression(test: Expression,
consequent: Expression,
alternate: Expression): ConditionalExpression;
property(
kind: \"init\" | \"get\" | \"set\",
key: Literal | Identifier,
value: Expression
): Property;
functionExpression(
id: ?Identifier,
params: Pattern[],
body: BlockStatement
): FunctionExpression;
binaryExpression(
operator: BinaryOperator,
left: Expression,
right: Expression
): BinaryExpression;
unaryExpression(
operator: UnaryOperator,
argument: Expression,
prefix: boolean
): UnaryExpression;
assignmentExpression(
operator: AssignmentOperator,
left: Pattern,
right: Expression
): AssignmentExpression;
updateExpression(
operator: UpdateOperator,
argument: Expression,
prefix: boolean
): UpdateExpression;
logicalExpression(
operator: LogicalOperator,
left: Expression,
right: Expression
): LogicalExpression;
conditionalExpression(
test: Expression,
consequent: Expression,
alternate: Expression
): ConditionalExpression;
newExpression(callee: Expression, arguments: Expression[]): NewExpression;
callExpression(callee: Expression, arguments: Expression[]): CallExpression;
memberExpression(object: Expression,
property: Identifier | Expression,
computed: boolean): MemberExpression;
variableDeclaration(kind: \"var\" | \"let\" | \"const\",
declarations: VariableDeclarator[]): VariableDeclaration;
functionDeclaration(id: Identifier,
body: BlockStatement,
params: Pattern[]): FunctionDeclaration;
memberExpression(
object: Expression,
property: Identifier | Expression,
computed: boolean
): MemberExpression;
variableDeclaration(
kind: \"var\" | \"let\" | \"const\",
declarations: VariableDeclarator[]
): VariableDeclaration;
functionDeclaration(
id: Identifier,
body: BlockStatement,
params: Pattern[]
): FunctionDeclaration;
variableDeclarator(id: Pattern, init?: Expression): VariableDeclarator
} = a;
"

View File

@ -469,20 +469,24 @@ let tests = [
createdCallback() {},
attachedCallback() {},
detachedCallback() {},
attributeChangedCallback(attributeLocalName,
oldAttributeValue,
newAttributeValue,
attributeNamespace) {}
attributeChangedCallback(
attributeLocalName,
oldAttributeValue,
newAttributeValue,
attributeNamespace
) {}
})
});
},
function() {
document.registerElement(\"custom-element\", {
prototype: {
attributeChangedCallback(localName: string,
oldVal: string,
newVal: string,
namespace: string) {}
attributeChangedCallback(
localName: string,
oldVal: string,
newVal: string,
namespace: string
) {}
}
});
}

View File

@ -319,7 +319,7 @@ const h: Response = new Response(\"responsebody\", {
status: 404,
headers: new Headers({ \"Content-Type\": \"image/jpeg\" })
});
const i: Response = new Response(, {
const i: Response = new Response({
status: 404,
headers: new Headers({ \"Content-Type\": \"image/jpeg\" })
});

View File

@ -103,18 +103,22 @@ buffer = buffer.fill(\"a\", 0, 0, \"utf8\");
buffer = buffer.fill(\"a\", \"utf8\");
maybeNum = buffer.find(, (element: number,
index: number,
array: Uint8Array) => false);
maybeNum = buffer.find((
element: number,
index: number,
array: Uint8Array
) => false);
maybeNum = buffer.find(
(element: number, index: number, array: Uint8Array) => false,
buffer
);
num = buffer.findIndex(, (element: number,
index: number,
array: Uint8Array) => false);
num = buffer.findIndex((
element: number,
index: number,
array: Uint8Array
) => false);
num = buffer.findIndex(
(element: number, index: number, array: Uint8Array) => false,

View File

@ -15,8 +15,10 @@ function is_string(x): %checks {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// @flow
// Filter the contents of an array
declare function my_filter<T, P: $Pred<1>>(v: Array<T>,
cb: P): Array<$Refine<T, P, 1>>;
declare function my_filter<T, P: $Pred<1>>(
v: Array<T>,
cb: P
): Array<$Refine<T, P, 1>>;
declare var arr: Array<mixed>;
const barr = my_filter(arr, is_string);
@ -51,8 +53,10 @@ declare var ab: Array<A|B|C>;
// Filter the contents of an array
// OK
// OK
declare function my_filter<T, P: $Pred<1>>(v: Array<T>,
cb: P): Array<$Refine<T, P, 1>>;
declare function my_filter<T, P: $Pred<1>>(
v: Array<T>,
cb: P
): Array<$Refine<T, P, 1>>;
type A = { kind: \"A\"; u: number };
type B = { kind: \"B\"; v: string };
type C = { kind: \"C\"; y: boolean };
@ -140,9 +144,11 @@ var b = refine(a, is_string);
(b: string);
declare function refine_fst<T, P: $Pred<2>>(v: T,
w: T,
cb: P): $Refine<T, P, 1>;
declare function refine_fst<T, P: $Pred<2>>(
v: T,
w: T,
cb: P
): $Refine<T, P, 1>;
declare var c: mixed;
declare var d: mixed;
var e = refine2(c, d, is_string_and_number);
@ -188,8 +194,10 @@ function is_string_regular(x): boolean {
// @flow
// Sanity check A: filtering the wrong type
// Sanity check B: Passing non-predicate function to filter
declare function my_filter<T, P: $Pred<1>>(v: Array<T>,
cb: P): Array<$Refine<T, P, 1>>;
declare function my_filter<T, P: $Pred<1>>(
v: Array<T>,
cb: P
): Array<$Refine<T, P, 1>>;
declare var a: Array<mixed>;
const b = my_filter(a, is_string);
@ -233,8 +241,10 @@ declare var ab: Array<A|B|C>;
// Filter the contents of an array
// ERROR
// ERROR
declare function my_filter<T, P: $Pred<1>>(v: Array<T>,
cb: P): Array<$Refine<T, P, 1>>;
declare function my_filter<T, P: $Pred<1>>(
v: Array<T>,
cb: P
): Array<$Refine<T, P, 1>>;
type A = { kind: \"A\"; u: number };
type B = { kind: \"B\"; v: string };
type C = { kind: \"C\"; y: boolean };
@ -312,10 +322,12 @@ var b = refine(a, is_string);
declare var c: mixed;
declare var d: mixed;
declare var e: mixed;
declare function refine3<T, P: $Pred<3>>(u: T,
v: T,
w: T,
cb: P): $Refine<T, P, 1>;
declare function refine3<T, P: $Pred<3>>(
u: T,
v: T,
w: T,
cb: P
): $Refine<T, P, 1>;
var e = refine3(c, d, e, is_string_and_number);
(e: string);

View File

@ -400,9 +400,10 @@ foo(3, 3);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// @flow
// Sanity check: make sure the parameters are checked as usual
declare function foo(input: mixed,
types: string | Array<string>): boolean %checks(typeof input === \"string\" ||
Array.isArray(input));
declare function foo(
input: mixed,
types: string | Array<string>
): boolean %checks(typeof input === \"string\" || Array.isArray(input));
foo(3, 3);
"

View File

@ -102,8 +102,9 @@ var a1 = (x: mixed): %checks => x !== null;
(x): %checks => x !== null;
const insert_a_really_big_predicated_arrow_function_name_here = (x): %checks => x !==
null;
const insert_a_really_big_predicated_arrow_function_name_here = (
x
): %checks => x !== null;
declare var x;
x;

View File

@ -129,7 +129,7 @@ var blah = <Foo {...props} />; // error bar, number given string expected
/* @flow */
// error bar, number given string expected
var React = require(\"react\");
var Foo = React.createClass(, {
var Foo = React.createClass({
propTypes: { bar: React.PropTypes.string.isRequired }
});
var props = { bar: 42 };
@ -155,7 +155,7 @@ var fail_mistyped_elems = <Example arr={[1, \"foo\"]} />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* @flow */
var React = require(\"react\");
var Example = React.createClass(, {
var Example = React.createClass({
propTypes: { arr: React.PropTypes.arrayOf(React.PropTypes.number).isRequired }
});
var ok_empty = <Example arr={[]}/>;
@ -183,7 +183,7 @@ var fail_mistyped = <Example func={2} />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* @flow */
var React = require(\"react\");
var Example = React.createClass(, {
var Example = React.createClass({
propTypes: { func: React.PropTypes.func.isRequired }
});
var ok_void = <Example func={() => {}}/>;
@ -275,7 +275,7 @@ var fail_mistyped = <Example object={2} />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* @flow */
var React = require(\"react\");
var Example = React.createClass(, {
var Example = React.createClass({
propTypes: { object: React.PropTypes.object.isRequired }
});
var ok_empty = <Example object={{}}/>;
@ -302,7 +302,7 @@ var fail_mistyped_props = <Example obj={{foo: \"foo\"}} />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* @flow */
var React = require(\"react\");
var Example = React.createClass(, {
var Example = React.createClass({
propTypes: {
obj: React.PropTypes.objectOf(React.PropTypes.number).isRequired
}
@ -329,7 +329,7 @@ var ex2 = <Example literal=\"bar\" />;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* @flow */
var React = require(\"react\");
var Example = React.createClass(, {
var Example = React.createClass({
propTypes: { literal: React.PropTypes.oneOf([ \"foo\" ]).isRequired }
});
var ex1 = <Example literal=\"foo\"/>;
@ -366,7 +366,7 @@ var fail_bool = <Example prop={true} />
var React = require(\"react\");
var Example = React.createClass({
propTypes: {
prop: React.PropTypes.oneOfType(, [
prop: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number
]).isRequired

View File

@ -61,7 +61,9 @@ function id(x: Task<any,any>): Task<any,any> { return x; }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* @flow */
type Task<error, value> = {
chain<tagged>(next: (input: value) => Task<error, tagged>): Task<error, tagged>
chain<tagged>(
next: (input: value) => Task<error, tagged>
): Task<error, tagged>
};
function id(x: Task<any, any>): Task<any, any> {

View File

@ -160,8 +160,10 @@ function test(
* result is a new object.
*/
// OK
declare function map<Tv, TNext>(obj: { [key: string]: Tv },
iterator: (obj: Tv) => TNext): Array<TNext>;
declare function map<Tv, TNext>(
obj: { [key: string]: Tv },
iterator: (obj: Tv) => TNext
): Array<TNext>;
function test(
x: { kind: ?string },

View File

@ -87,10 +87,10 @@ function f(b): React.Element<*> {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// @flow
var React = require(\"react\");
var A = React.createClass(, {
var A = React.createClass({
propTypes: { foo: React.PropTypes.string.isRequired }
});
var B = React.createClass(, {
var B = React.createClass({
propTypes: { bar: React.PropTypes.string.isRequired }
});

View File

@ -55,12 +55,20 @@ declare class Promise<R> {
// library files declared earlier, including default flow libs.
// Non-standard APIs
declare class Promise<R> {
constructor(callback: (resolve: (result?: Promise<R> | R) => void,
reject: (error?: any) => void) => void): void;
then<U>(onFulfill?: ?(value: R) => Promise<U> | ?U,
onReject?: ?(error: any) => Promise<U> | ?U): Promise<U>;
done<U>(onFulfill?: ?(value: R) => void,
onReject?: ?(error: any) => void): void;
constructor(
callback: (
resolve: (result?: Promise<R> | R) => void,
reject: (error?: any) => void
) => void
): void;
then<U>(
onFulfill?: ?(value: R) => Promise<U> | ?U,
onReject?: ?(error: any) => Promise<U> | ?U
): Promise<U>;
done<U>(
onFulfill?: ?(value: R) => void,
onReject?: ?(error: any) => void
): void;
catch<U>(onReject?: (error: any) => ?Promise<U> | U): Promise<U>;
static resolve<T>(object?: Promise<T> | T): Promise<T>;
static reject<T>(error?: any): Promise<T>;
@ -68,9 +76,9 @@ declare class Promise<R> {
static cast<T>(object?: T): Promise<T>;
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

@ -1626,10 +1626,12 @@ declare class C {
setMaxListeners(n: number): void
}
declare class D extends C {
listen(port: number,
hostname?: string,
backlog?: number,
callback?: Function): D;
listen(
port: number,
hostname?: string,
backlog?: number,
callback?: Function
): D;
listen(path: string, callback?: Function): D;
listen(handle: Object, callback?: Function): D;
close(callback?: Function): D;
@ -1812,10 +1814,14 @@ class C {
// with different instantiations.
type Row = { x: string };
declare class D<T> {
reduce(callbackfn: (previousValue: T, currentValue: T) => T,
initialValue: void): T;
reduce<U>(callbackfn: (previousValue: U, currentValue: T) => U,
initialValue: U): U
reduce(
callbackfn: (previousValue: T, currentValue: T) => T,
initialValue: void
): T;
reduce<U>(
callbackfn: (previousValue: U, currentValue: T) => U,
initialValue: U
): U
}
class C {
foo(rows: D<Row>, minWidth: number): number {

View File

@ -30,15 +30,20 @@ declare class Rows {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare class Set<T> { add(): Set<T> }
declare class Row {
reduce_row(callbackfn: (previousValue: number,
currentValue: number) => number,
initialValue: void): number;
reduce_row<U>(callbackfn: (previousValue: U, currentValue: number) => U,
initialValue: U): U
reduce_row(
callbackfn: (previousValue: number, currentValue: number) => number,
initialValue: void
): number;
reduce_row<U>(
callbackfn: (previousValue: U, currentValue: number) => U,
initialValue: U
): U
}
declare class Rows {
reduce_rows<X>(callbackfn: (previousValue: X, currentValue: Row) => X,
initialValue: X): X
reduce_rows<X>(
callbackfn: (previousValue: X, currentValue: Row) => X,
initialValue: X
): X
}
"
`;