Add support for leading pipes in Flow type alias RHS syntax

This commit is contained in:
Jeff Morrison 2016-02-04 15:50:19 -05:00
parent bcc32da0d9
commit e6951e99f0
8 changed files with 497 additions and 2 deletions

View File

@ -2,3 +2,13 @@ type FBID = number;
type Foo<T> = Bar<T>;
type Maybe<T> = _Maybe<T, *>;
export type Foo = number;
type union =
| {type: "A"}
| {type: "B"}
;
type overloads =
& ((x: string) => number)
& ((x: number) => string)
;

View File

@ -2,3 +2,7 @@ type FBID = number;
type Foo<T> = Bar<T>;
type Maybe<T> = _Maybe<T, *>;
export type Foo = number;
type union = { type: "A" } | { type: "B" };
type overloads = (x: string) => number & (x: number) => string;

View File

@ -3,3 +3,13 @@ type A = number;
type B = {
name: string;
};
type union =
| {type: "A"}
| {type: "B"}
;
type overloads =
& ((x: string) => number)
& ((x: number) => string)
;

View File

@ -3,3 +3,11 @@ function a() {}
/*:: type B = {
name: string;
};*/
/*:: type union =
| {type: "A"}
| {type: "B"}
;*/
/*:: type overloads =
& ((x: string) => number)
& ((x: number) => string)
;*/

View File

@ -1,3 +1,13 @@
type FBID = number;
type Foo<T> = Bar<T>
export type Foo = number;
type union =
| {type: "A"}
| {type: "B"}
;
type overloads =
& ((x: string) => number)
& ((x: number) => string)
;

View File

@ -3,10 +3,15 @@ import Parser from "../parser";
let pp = Parser.prototype;
pp.flowParseTypeInitialiser = function (tok) {
pp.flowParseTypeInitialiser = function (tok, allowLeadingPipeOrAnd) {
let oldInType = this.state.inType;
this.state.inType = true;
this.expect(tok || tt.colon);
if (allowLeadingPipeOrAnd) {
if (this.match(tt.bitwiseAND) || this.match(tt.bitwiseOR)) {
this.next();
}
}
let type = this.flowParseType();
this.state.inType = oldInType;
return type;
@ -172,7 +177,10 @@ pp.flowParseTypeAlias = function (node) {
node.typeParameters = null;
}
node.right = this.flowParseTypeInitialiser(tt.eq);
node.right = this.flowParseTypeInitialiser(
tt.eq,
/*allowLeadingPipeOrAnd*/ true
);
this.semicolon();
return this.finishNode(node, "TypeAlias");

View File

@ -0,0 +1,9 @@
type union =
| {type: "A"}
| {type: "B"}
;
type overloads =
& ((x: string) => number)
& ((x: number) => string)
;

View File

@ -0,0 +1,436 @@
{
"type": "File",
"start": 0,
"end": 120,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 9,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 120,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 9,
"column": 1
}
},
"sourceType": "module",
"body": [
{
"type": "TypeAlias",
"start": 0,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 5,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 10
}
},
"name": "union"
},
"typeParameters": null,
"right": {
"type": "UnionTypeAnnotation",
"start": 16,
"end": 42,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 3,
"column": 14
}
},
"types": [
{
"type": "ObjectTypeAnnotation",
"start": 16,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 14
}
},
"callProperties": [],
"properties": [
{
"type": "ObjectTypeProperty",
"start": 17,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 13
}
},
"key": {
"type": "Identifier",
"start": 17,
"end": 21,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 8
}
},
"name": "type"
},
"value": {
"type": "StringLiteralTypeAnnotation",
"start": 23,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 13
}
},
"value": "A",
"extra": {
"rawValue": "A",
"raw": "\"A\""
}
},
"optional": false
}
],
"indexers": []
},
{
"type": "ObjectTypeAnnotation",
"start": 31,
"end": 42,
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 14
}
},
"callProperties": [],
"properties": [
{
"type": "ObjectTypeProperty",
"start": 32,
"end": 41,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 13
}
},
"key": {
"type": "Identifier",
"start": 32,
"end": 36,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 8
}
},
"name": "type"
},
"value": {
"type": "StringLiteralTypeAnnotation",
"start": 38,
"end": 41,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 13
}
},
"value": "B",
"extra": {
"rawValue": "B",
"raw": "\"B\""
}
},
"optional": false
}
],
"indexers": []
}
]
}
},
{
"type": "TypeAlias",
"start": 46,
"end": 120,
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 9,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 51,
"end": 60,
"loc": {
"start": {
"line": 6,
"column": 5
},
"end": {
"line": 6,
"column": 14
}
},
"name": "overloads"
},
"typeParameters": null,
"right": {
"type": "IntersectionTypeAnnotation",
"start": 67,
"end": 118,
"loc": {
"start": {
"line": 7,
"column": 4
},
"end": {
"line": 8,
"column": 27
}
},
"types": [
{
"type": "FunctionTypeAnnotation",
"start": 68,
"end": 89,
"loc": {
"start": {
"line": 7,
"column": 5
},
"end": {
"line": 7,
"column": 26
}
},
"params": [
{
"type": "FunctionTypeParam",
"start": 69,
"end": 78,
"loc": {
"start": {
"line": 7,
"column": 6
},
"end": {
"line": 7,
"column": 15
}
},
"name": {
"type": "Identifier",
"start": 69,
"end": 70,
"loc": {
"start": {
"line": 7,
"column": 6
},
"end": {
"line": 7,
"column": 7
}
},
"name": "x"
},
"optional": false,
"typeAnnotation": {
"type": "StringTypeAnnotation",
"start": 72,
"end": 78,
"loc": {
"start": {
"line": 7,
"column": 9
},
"end": {
"line": 7,
"column": 15
}
}
}
}
],
"rest": null,
"returnType": {
"type": "NumberTypeAnnotation",
"start": 83,
"end": 89,
"loc": {
"start": {
"line": 7,
"column": 20
},
"end": {
"line": 7,
"column": 26
}
}
},
"typeParameters": null
},
{
"type": "FunctionTypeAnnotation",
"start": 96,
"end": 117,
"loc": {
"start": {
"line": 8,
"column": 5
},
"end": {
"line": 8,
"column": 26
}
},
"params": [
{
"type": "FunctionTypeParam",
"start": 97,
"end": 106,
"loc": {
"start": {
"line": 8,
"column": 6
},
"end": {
"line": 8,
"column": 15
}
},
"name": {
"type": "Identifier",
"start": 97,
"end": 98,
"loc": {
"start": {
"line": 8,
"column": 6
},
"end": {
"line": 8,
"column": 7
}
},
"name": "x"
},
"optional": false,
"typeAnnotation": {
"type": "NumberTypeAnnotation",
"start": 100,
"end": 106,
"loc": {
"start": {
"line": 8,
"column": 9
},
"end": {
"line": 8,
"column": 15
}
}
}
}
],
"rest": null,
"returnType": {
"type": "StringTypeAnnotation",
"start": 111,
"end": 117,
"loc": {
"start": {
"line": 8,
"column": 20
},
"end": {
"line": 8,
"column": 26
}
}
},
"typeParameters": null
}
]
}
}
],
"directives": []
}
}