First attempt at making union types prettier

This commit is contained in:
Jeremy Morrell committed 2017-01-10 18:31:29 -03:00
1 parent 0ba4d71c5c
commit 9213d5ad7f
11 files changed
+7253 -7082

No files matched your search

+28 -8
View File
@@ -1364,14 +1364,34 @@ function genericPrintNoParens(path, options, print) {
case "UnionTypeAnnotation": {
const types = path.map(print, "types");
const op = n.type === "IntersectionTypeAnnotation" ? "&" : "|";
return group(
concat([
types[0],
indent(
options.tabWidth,
concat(types.slice(1).map(t => concat([ " ", op, line, t ])))
)
])
return conditionalGroup(
[
// single-line variation
// A | B | C
concat([
indent(
options.tabWidth,
concat([
types[0],
indent(
options.tabWidth,
concat(types.slice(1).map(t => concat([ " ", op, line, t ])))
)
])
)
]),
// multi-line variation
// | A
// | B
// | C
concat([
indent(
options.tabWidth,
concat(types.map(t => concat([ line, op, " ", t ])))
)
])
]
);
}
case "NullableTypeAnnotation":
@@ -84,13 +84,14 @@ export type TypedNode =
* @flow
*/
export type InferredType = \"unknown\" |
\"gender\" |
\"enum\" |
\"number-or-string\" |
\"number\" |
\"string\" |
\"error\";
export type InferredType =
| \"unknown\"
| \"gender\"
| \"enum\"
| \"number-or-string\"
| \"number\"
| \"string\"
| \"error\";
export type Pos = {
firstLine: number,
@@ -150,12 +151,13 @@ export type TypedFunctionInvocationNode = {
typed: true
};
export type TypedNode = TypedBinaryOpNode |
TypedUnaryMinusNode |
TypedNumberNode |
TypedStringLiteralNode |
TypedVariableNode |
TypedFunctionInvocationNode;"
export type TypedNode =
| TypedBinaryOpNode
| TypedUnaryMinusNode
| TypedNumberNode
| TypedStringLiteralNode
| TypedVariableNode
| TypedFunctionInvocationNode;"
`;
exports[`test emit.js 1`] = `
@@ -965,100 +967,105 @@ export type Program = {
body: Statement[]
};
export type BinaryOperator = \"==\" |
\"!=\" |
\"===\" |
\"!==\" |
\"<\" |
\"<=\" |
\">\" |
\">=\" |
\"<<\" |
\">>\" |
\">>>\" |
\"+\" |
\"-\" |
\"*\" |
\"/\" |
\"%\" |
\"&\" /* TODO Missing from the Parser API.*/ |
\"|\" |
\"^\" |
\"in\" |
\"instanceof\" |
\"..\";
export type BinaryOperator =
| \"==\"
| \"!=\"
| \"===\"
| \"!==\"
| \"<\"
| \"<=\"
| \">\"
| \">=\"
| \"<<\"
| \">>\"
| \">>>\"
| \"+\"
| \"-\"
| \"*\"
| \"/\"
| \"%\"
| \"&\" /* TODO Missing from the Parser API.*/
| \"|\"
| \"^\"
| \"in\"
| \"instanceof\"
| \"..\";
export type UnaryOperator = \"-\" |
\"+\" |
\"!\" |
\"~\" |
\"typeof\" |
\"void\" |
\"delete\";
export type UnaryOperator =
| \"-\"
| \"+\"
| \"!\"
| \"~\"
| \"typeof\"
| \"void\"
| \"delete\";
export type AssignmentOperator = \"=\" |
\"+=\" |
\"-=\" |
\"*=\" |
\"/=\" |
\"%=\" |
\"<<=\" |
\">>=\" |
\">>>=\" |
\"|=\" |
\"^=\" |
\"&=\";
export type AssignmentOperator =
| \"=\"
| \"+=\"
| \"-=\"
| \"*=\"
| \"/=\"
| \"%=\"
| \"<<=\"
| \">>=\"
| \">>>=\"
| \"|=\"
| \"^=\"
| \"&=\";
export type UpdateOperator = \"++\" | \"--\";
export type LogicalOperator = \"&&\" | \"||\";
export type Node = EmptyStatement |
BlockStatement |
ExpressionStatement |
IfStatement |
BreakStatement |
ContinueStatement |
ReturnStatement |
ThrowStatement |
WhileStatement |
ForStatement |
ForInStatement |
TryStatement |
CatchClause |
Identifier |
Literal |
ThisExpression |
ArrayExpression |
ObjectExpreession |
Property |
FunctionExpression |
BinaryExpression |
UnaryExpression |
AssignmentExpression |
UpdateExpression |
LogicalExpression |
ConditionalExpression |
NewExpression |
CallExpression |
MemberExpression |
VariableDeclaration |
FunctionDeclaration |
VariableDeclarator;
export type Node =
| EmptyStatement
| BlockStatement
| ExpressionStatement
| IfStatement
| BreakStatement
| ContinueStatement
| ReturnStatement
| ThrowStatement
| WhileStatement
| ForStatement
| ForInStatement
| TryStatement
| CatchClause
| Identifier
| Literal
| ThisExpression
| ArrayExpression
| ObjectExpreession
| Property
| FunctionExpression
| BinaryExpression
| UnaryExpression
| AssignmentExpression
| UpdateExpression
| LogicalExpression
| ConditionalExpression
| NewExpression
| CallExpression
| MemberExpression
| VariableDeclaration
| FunctionDeclaration
| VariableDeclarator;
export type Statement = BlockStatement |
EmptyStatement |
ExpressionStatement |
IfStatement |
BreakStatement |
ContinueStatement |
ReturnStatement |
ThrowStatement |
WhileStatement |
ForStatement |
ForInStatement |
TryStatement |
Declaration;
export type Statement =
| BlockStatement
| EmptyStatement
| ExpressionStatement
| IfStatement
| BreakStatement
| ContinueStatement
| ReturnStatement
| ThrowStatement
| WhileStatement
| ForStatement
| ForInStatement
| TryStatement
| Declaration;
export type EmptyStatement = {
source: ?string,
@@ -1189,21 +1196,22 @@ export type CatchClause = {
body: BlockStatement
};
export type Expression = Identifier |
ThisExpression |
Literal |
FunctionExpression |
BinaryExpression |
UnaryExpression |
AssignmentExpression |
UpdateExpression |
LogicalExpression |
ConditionalExpression |
NewExpression |
CallExpression |
MemberExpression |
ArrayExpression |
ObjectExpreession;
export type Expression =
| Identifier
| ThisExpression
| Literal
| FunctionExpression
| BinaryExpression
| UnaryExpression
| AssignmentExpression
| UpdateExpression
| LogicalExpression
| ConditionalExpression
| NewExpression
| CallExpression
| MemberExpression
| ArrayExpression
| ObjectExpreession;
export type Identifier = {
source: ?string,
+8 -6
View File
@@ -664,16 +664,18 @@ let tests = [
function() {
const i: NodeIterator<*, *> = document.createNodeIterator(document.body);
const filter: NodeFilter = i.filter;
const response: typeof NodeFilter.FILTER_ACCEPT |
typeof NodeFilter.FILTER_REJECT |
typeof NodeFilter.FILTER_SKIP = filter.acceptNode(document.body);
const response:
| typeof NodeFilter.FILTER_ACCEPT
| typeof NodeFilter.FILTER_REJECT
| typeof NodeFilter.FILTER_SKIP = filter.acceptNode(document.body);
},
function() {
const w: TreeWalker<*, *> = document.createTreeWalker(document.body);
const filter: NodeFilter = w.filter;
const response: typeof NodeFilter.FILTER_ACCEPT |
typeof NodeFilter.FILTER_REJECT |
typeof NodeFilter.FILTER_SKIP = filter.acceptNode(document.body);
const response:
| typeof NodeFilter.FILTER_ACCEPT
| typeof NodeFilter.FILTER_REJECT
| typeof NodeFilter.FILTER_SKIP = filter.acceptNode(document.body);
},
// rootNode must be a Node
function() {
@@ -75,9 +75,10 @@ function hasObjectMode_ok(options: DuplexStreamOptions): boolean {
* @flow
*/
type DuplexStreamOptions = ReadableStreamOptions &
WritableStreamOptions &
{
type DuplexStreamOptions =
& ReadableStreamOptions
& WritableStreamOptions
& {
allowHalfOpen?: boolean,
readableObjectMode?: boolean,
writableObjectMode?: boolean
@@ -69,6 +69,7 @@ var x1: $NonMaybeType<number | string> = true;
// err, boolean ~> number|string
var x2: $PropertyType<{ p: number } | { p: string }, \"p\"> = 0;
// ok, number ~> number|string
var x3: $PropertyType<{ p: number } |
{ p: string }, \"p\"> = true; // err, boolean ~> number|string"
var x3: $PropertyType<
| { p: number }
| { p: string }, \"p\"> = true; // err, boolean ~> number|string"
`;
File diff suppressed because it is too large. Load diff
@@ -25,8 +25,9 @@ declare var b: B<any>;
class A {}
type B<T> = A &
{
type B<T> =
& A
& {
+a: () => B<T>,
+b: () => B<T>,
+c: () => B<T>,
@@ -70,8 +71,9 @@ declare var b: B<any>;
class A {}
type B<T> = A &
{
type B<T> =
& A
& {
+a: (x: B<T>) => void,
+b: (x: B<T>) => void,
+c: (x: B<T>) => void,
File diff suppressed because it is too large. Load diff
+35
View File
@@ -13,3 +13,38 @@ function f3(x: T3): T3 { return x; }
type T4 = (A | C) & (B | C);
function f4(x: T4): T4 { return x; }
type T5 = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "10" | "11" | "12" | "13";
type T6 = "a-long-string" | "another-long-string" | "yet-another-long-string" | "one-more-for-good-measure";
type T7 =
{ eventName: "these", a: number } |
{ eventName: "will", b: number } |
{ eventName: "not", c: number } |
{ eventName: "fit", d: number } |
{ eventName: "on", e: number } |
{ eventName: "one", f: number } |
{ eventName: "line", g: number };
type Comment = {
type: 'CommentLine';
_CommentLine: void;
value: string;
end: number;
loc: {
end: {column: number, line: number},
start: {column: number, line: number},
};
start: number;
} | {
type: 'CommentBlock';
_CommentBlock: void;
value: string;
end: number;
loc: {
end: {column: number, line: number},
start: {column: number, line: number},
};
start: number;
};
File diff suppressed because it is too large. Load diff
@@ -1278,8 +1278,9 @@ exports[`test test13.js 1`] = `
/* ensure there are no unintended side effects when trying branches */
({ type: \"B\", id: \"hi\" }: { type: \"A\", id: ?string } |
{ type: \"B\", id: string });"
({ type: \"B\", id: \"hi\" }:
| { type: \"A\", id: ?string }
| { type: \"B\", id: string });"
`;
exports[`test test14.js 1`] = `