Add support for leading pipes in Flow type alias RHS syntax
This commit is contained in:
parent
bcc32da0d9
commit
e6951e99f0
@ -2,3 +2,13 @@ type FBID = number;
|
|||||||
type Foo<T> = Bar<T>;
|
type Foo<T> = Bar<T>;
|
||||||
type Maybe<T> = _Maybe<T, *>;
|
type Maybe<T> = _Maybe<T, *>;
|
||||||
export type Foo = number;
|
export type Foo = number;
|
||||||
|
|
||||||
|
type union =
|
||||||
|
| {type: "A"}
|
||||||
|
| {type: "B"}
|
||||||
|
;
|
||||||
|
|
||||||
|
type overloads =
|
||||||
|
& ((x: string) => number)
|
||||||
|
& ((x: number) => string)
|
||||||
|
;
|
||||||
|
|||||||
@ -2,3 +2,7 @@ type FBID = number;
|
|||||||
type Foo<T> = Bar<T>;
|
type Foo<T> = Bar<T>;
|
||||||
type Maybe<T> = _Maybe<T, *>;
|
type Maybe<T> = _Maybe<T, *>;
|
||||||
export type Foo = number;
|
export type Foo = number;
|
||||||
|
|
||||||
|
type union = { type: "A" } | { type: "B" };
|
||||||
|
|
||||||
|
type overloads = (x: string) => number & (x: number) => string;
|
||||||
|
|||||||
@ -3,3 +3,13 @@ type A = number;
|
|||||||
type B = {
|
type B = {
|
||||||
name: string;
|
name: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type union =
|
||||||
|
| {type: "A"}
|
||||||
|
| {type: "B"}
|
||||||
|
;
|
||||||
|
|
||||||
|
type overloads =
|
||||||
|
& ((x: string) => number)
|
||||||
|
& ((x: number) => string)
|
||||||
|
;
|
||||||
|
|||||||
@ -3,3 +3,11 @@ function a() {}
|
|||||||
/*:: type B = {
|
/*:: type B = {
|
||||||
name: string;
|
name: string;
|
||||||
};*/
|
};*/
|
||||||
|
/*:: type union =
|
||||||
|
| {type: "A"}
|
||||||
|
| {type: "B"}
|
||||||
|
;*/
|
||||||
|
/*:: type overloads =
|
||||||
|
& ((x: string) => number)
|
||||||
|
& ((x: number) => string)
|
||||||
|
;*/
|
||||||
|
|||||||
@ -1,3 +1,13 @@
|
|||||||
type FBID = number;
|
type FBID = number;
|
||||||
type Foo<T> = Bar<T>
|
type Foo<T> = Bar<T>
|
||||||
export type Foo = number;
|
export type Foo = number;
|
||||||
|
|
||||||
|
type union =
|
||||||
|
| {type: "A"}
|
||||||
|
| {type: "B"}
|
||||||
|
;
|
||||||
|
|
||||||
|
type overloads =
|
||||||
|
& ((x: string) => number)
|
||||||
|
& ((x: number) => string)
|
||||||
|
;
|
||||||
|
|||||||
@ -3,10 +3,15 @@ import Parser from "../parser";
|
|||||||
|
|
||||||
let pp = Parser.prototype;
|
let pp = Parser.prototype;
|
||||||
|
|
||||||
pp.flowParseTypeInitialiser = function (tok) {
|
pp.flowParseTypeInitialiser = function (tok, allowLeadingPipeOrAnd) {
|
||||||
let oldInType = this.state.inType;
|
let oldInType = this.state.inType;
|
||||||
this.state.inType = true;
|
this.state.inType = true;
|
||||||
this.expect(tok || tt.colon);
|
this.expect(tok || tt.colon);
|
||||||
|
if (allowLeadingPipeOrAnd) {
|
||||||
|
if (this.match(tt.bitwiseAND) || this.match(tt.bitwiseOR)) {
|
||||||
|
this.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
let type = this.flowParseType();
|
let type = this.flowParseType();
|
||||||
this.state.inType = oldInType;
|
this.state.inType = oldInType;
|
||||||
return type;
|
return type;
|
||||||
@ -172,7 +177,10 @@ pp.flowParseTypeAlias = function (node) {
|
|||||||
node.typeParameters = null;
|
node.typeParameters = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
node.right = this.flowParseTypeInitialiser(tt.eq);
|
node.right = this.flowParseTypeInitialiser(
|
||||||
|
tt.eq,
|
||||||
|
/*allowLeadingPipeOrAnd*/ true
|
||||||
|
);
|
||||||
this.semicolon();
|
this.semicolon();
|
||||||
|
|
||||||
return this.finishNode(node, "TypeAlias");
|
return this.finishNode(node, "TypeAlias");
|
||||||
|
|||||||
9
packages/babylon/test/fixtures/flow/type-alias/4/actual.js
vendored
Normal file
9
packages/babylon/test/fixtures/flow/type-alias/4/actual.js
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
type union =
|
||||||
|
| {type: "A"}
|
||||||
|
| {type: "B"}
|
||||||
|
;
|
||||||
|
|
||||||
|
type overloads =
|
||||||
|
& ((x: string) => number)
|
||||||
|
& ((x: number) => string)
|
||||||
|
;
|
||||||
436
packages/babylon/test/fixtures/flow/type-alias/4/expected.json
vendored
Normal file
436
packages/babylon/test/fixtures/flow/type-alias/4/expected.json
vendored
Normal 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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user