add ExistentialTypeParam - fixes #2587
This commit is contained in:
parent
49cbd27441
commit
7dc1b4e7e9
@ -46,6 +46,10 @@ export function DeclareVariable(node: Object) {
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
export function ExistentialTypeParam() {
|
||||
this.push("*");
|
||||
}
|
||||
|
||||
export function FunctionTypeAnnotation(node: Object, parent: Object) {
|
||||
this.print(node.typeParameters, node);
|
||||
this.push("(");
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
type FBID = number;
|
||||
type Foo<T> = Bar<T>
|
||||
type Foo<T> = Bar<T>;
|
||||
type Maybe<T> = _Maybe<T, *>;
|
||||
export type Foo = number;
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
type FBID = number;
|
||||
type Foo<T> = Bar<T>;
|
||||
type Maybe<T> = _Maybe<T, *>;
|
||||
export type Foo = number;
|
||||
|
||||
@ -79,6 +79,10 @@ defineType("DeclareVariable", {
|
||||
}
|
||||
});
|
||||
|
||||
defineType("ExistentialTypeParam", {
|
||||
aliases: ["Flow"]
|
||||
});
|
||||
|
||||
defineType("FunctionTypeAnnotation", {
|
||||
visitor: ["typeParameters", "params", "rest", "returnType"],
|
||||
aliases: ["Flow"],
|
||||
|
||||
@ -164,7 +164,7 @@ pp.flowParseTypeParameterDeclaration = function () {
|
||||
|
||||
this.expectRelational("<");
|
||||
while (!this.isRelational(">")) {
|
||||
node.params.push(this.flowParseTypeAnnotatableIdentifier());
|
||||
node.params.push(this.parseFlowTypeParam());
|
||||
if (!this.isRelational(">")) {
|
||||
this.expect(tt.comma);
|
||||
}
|
||||
@ -174,6 +174,16 @@ pp.flowParseTypeParameterDeclaration = function () {
|
||||
return this.finishNode(node, "TypeParameterDeclaration");
|
||||
};
|
||||
|
||||
pp.parseFlowTypeParam = function () {
|
||||
if (this.match(tt.star)) {
|
||||
let node = this.startNode();
|
||||
this.next();
|
||||
return this.finishNode(node, "ExistentialTypeParam");
|
||||
} else {
|
||||
return this.flowParseTypeAnnotatableIdentifier();
|
||||
}
|
||||
};
|
||||
|
||||
pp.flowParseTypeParameterInstantiation = function () {
|
||||
let node = this.startNode(), oldInType = this.state.inType;
|
||||
node.params = [];
|
||||
@ -182,7 +192,7 @@ pp.flowParseTypeParameterInstantiation = function () {
|
||||
|
||||
this.expectRelational("<");
|
||||
while (!this.isRelational(">")) {
|
||||
node.params.push(this.flowParseType());
|
||||
node.params.push(this.parseFlowTypeParam());
|
||||
if (!this.isRelational(">")) {
|
||||
this.expect(tt.comma);
|
||||
}
|
||||
|
||||
@ -56,6 +56,8 @@ tt.parenR.updateContext = tt.braceR.updateContext = function () {
|
||||
};
|
||||
|
||||
tt.name.updateContext = function (prevType) {
|
||||
this.state.exprAllowed = false;
|
||||
|
||||
if (prevType === tt._let || prevType === tt._const || prevType === tt._var) {
|
||||
if (lineBreak.test(this.input.slice(this.state.end))) {
|
||||
this.state.exprAllowed = true;
|
||||
|
||||
1
packages/babylon/test/fixtures/flow/type-annotations/existential-type-param/actual.js
vendored
Normal file
1
packages/babylon/test/fixtures/flow/type-annotations/existential-type-param/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
type Maybe<T> = _Maybe<T, *>;
|
||||
177
packages/babylon/test/fixtures/flow/type-annotations/existential-type-param/expected.json
vendored
Normal file
177
packages/babylon/test/fixtures/flow/type-annotations/existential-type-param/expected.json
vendored
Normal file
@ -0,0 +1,177 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 29,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 29
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 29,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 29
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"body": [
|
||||
{
|
||||
"type": "TypeAlias",
|
||||
"start": 0,
|
||||
"end": 29,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 29
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 5,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"name": "Maybe"
|
||||
},
|
||||
"typeParameters": {
|
||||
"type": "TypeParameterDeclaration",
|
||||
"start": 10,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start": 11,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
}
|
||||
},
|
||||
"name": "T"
|
||||
}
|
||||
]
|
||||
},
|
||||
"right": {
|
||||
"type": "GenericTypeAnnotation",
|
||||
"start": 16,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 16
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 28
|
||||
}
|
||||
},
|
||||
"typeParameters": {
|
||||
"type": "TypeParameterInstantiation",
|
||||
"start": 22,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 22
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 28
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start": 23,
|
||||
"end": 24,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 23
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 24
|
||||
}
|
||||
},
|
||||
"name": "T"
|
||||
},
|
||||
{
|
||||
"type": "ExistentialTypeParam",
|
||||
"start": 26,
|
||||
"end": 27,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 26
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 27
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 16,
|
||||
"end": 22,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 16
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 22
|
||||
}
|
||||
},
|
||||
"name": "_Maybe"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user