Add support for "declare interface" Flow syntax

This has been a feature in Flow for a long time (couldn't easily find a
specific commit adding this). Interfaces are basically undocumented, though, so
it's easy to see how this was missed.
This commit is contained in:
Sam Goldman
2016-01-04 11:50:12 -08:00
parent e764346d5f
commit 26c97c4069
10 changed files with 316 additions and 0 deletions

View File

@@ -36,6 +36,11 @@ export function DeclareFunction(node: Object) {
this.semicolon();
}
export function DeclareInterface(node: Object) {
this.push("declare ");
this.InterfaceDeclaration(node);
}
export function DeclareModule(node: Object) {
this.push("declare module ");
this.print(node.id, node);

View File

@@ -12,3 +12,5 @@ declare class A { static () : number }
declare class A mixins B<T>, C {}
declare type A = string
declare type T<U> = { [k:string]: U }
declare interface I { foo: string }
declare interface I<T> { foo: T }

View File

@@ -12,3 +12,5 @@ declare class A { static (): number }
declare class A mixins B<T>, C {}
declare type A = string;
declare type T<U> = { [k: string]: U };
declare interface I { foo: string }
declare interface I<T> { foo: T }

View File

@@ -12,3 +12,5 @@ declare class A { static () : number }
declare class A mixins B<T>, C {}
declare type A = string
declare type T<U> = { [k:string]: U }
declare interface I { foo: string }
declare interface I<T> { foo: T }

View File

@@ -12,3 +12,5 @@
/*:: declare class A mixins B<T>, C {}*/
/*:: declare type A = string*/
/*:: declare type T<U> = { [k:string]: U }*/
/*:: declare interface I { foo: string }*/
/*:: declare interface I<T> { foo: T }*/

View File

@@ -12,3 +12,5 @@ declare class A { static () : number }
declare class A mixins B<T>, C {}
declare type A = string
declare type T<U> = { [k:string]: U }
declare interface I { foo: string }
declare interface I<T> { foo: T }

View File

@@ -66,6 +66,14 @@ defineType("DeclareFunction", {
}
});
defineType("DeclareInterface", {
visitor: ["id", "typeParameters", "extends", "body"],
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"],
fields: {
// todo
}
});
defineType("DeclareModule", {
visitor: ["id", "body"],
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"],

View File

@@ -60,6 +60,8 @@ pp.flowParseDeclare = function (node) {
return this.flowParseDeclareModule(node);
} else if (this.isContextual("type")) {
return this.flowParseDeclareTypeAlias(node);
} else if (this.isContextual("interface")) {
return this.flowParseDeclareInterface(node);
} else {
this.unexpected();
}
@@ -104,6 +106,12 @@ pp.flowParseDeclareTypeAlias = function (node) {
return this.finishNode(node, "DeclareTypeAlias");
};
pp.flowParseDeclareInterface = function (node) {
this.next();
this.flowParseInterfaceish(node);
return this.finishNode(node, "DeclareInterface");
}
// Interfaces

View File

@@ -0,0 +1,2 @@
declare interface I { foo: string }
declare interface I<T> { foo: T }

View File

@@ -0,0 +1,283 @@
{
"type": "File",
"start": 0,
"end": 69,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 33
}
},
"program": {
"type": "Program",
"start": 0,
"end": 69,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 33
}
},
"sourceType": "module",
"body": [
{
"type": "DeclareInterface",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 35
}
},
"id": {
"type": "Identifier",
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 18
},
"end": {
"line": 1,
"column": 19
}
},
"name": "I"
},
"typeParameters": null,
"extends": [],
"mixins": [],
"body": {
"type": "ObjectTypeAnnotation",
"start": 20,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 35
}
},
"callProperties": [],
"properties": [
{
"type": "ObjectTypeProperty",
"start": 22,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 33
}
},
"key": {
"type": "Identifier",
"start": 22,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 25
}
},
"name": "foo"
},
"value": {
"type": "StringTypeAnnotation",
"start": 27,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 27
},
"end": {
"line": 1,
"column": 33
}
}
},
"optional": false
}
],
"indexers": []
}
},
{
"type": "DeclareInterface",
"start": 36,
"end": 69,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 33
}
},
"id": {
"type": "Identifier",
"start": 54,
"end": 55,
"loc": {
"start": {
"line": 2,
"column": 18
},
"end": {
"line": 2,
"column": 19
}
},
"name": "I"
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 55,
"end": 58,
"loc": {
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 22
}
},
"params": [
{
"type": "Identifier",
"start": 56,
"end": 57,
"loc": {
"start": {
"line": 2,
"column": 20
},
"end": {
"line": 2,
"column": 21
}
},
"name": "T"
}
]
},
"extends": [],
"mixins": [],
"body": {
"type": "ObjectTypeAnnotation",
"start": 59,
"end": 69,
"loc": {
"start": {
"line": 2,
"column": 23
},
"end": {
"line": 2,
"column": 33
}
},
"callProperties": [],
"properties": [
{
"type": "ObjectTypeProperty",
"start": 61,
"end": 67,
"loc": {
"start": {
"line": 2,
"column": 25
},
"end": {
"line": 2,
"column": 31
}
},
"key": {
"type": "Identifier",
"start": 61,
"end": 64,
"loc": {
"start": {
"line": 2,
"column": 25
},
"end": {
"line": 2,
"column": 28
}
},
"name": "foo"
},
"value": {
"type": "GenericTypeAnnotation",
"start": 66,
"end": 67,
"loc": {
"start": {
"line": 2,
"column": 30
},
"end": {
"line": 2,
"column": 31
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 66,
"end": 67,
"loc": {
"start": {
"line": 2,
"column": 30
},
"end": {
"line": 2,
"column": 31
}
},
"name": "T"
}
},
"optional": false
}
],
"indexers": []
}
}
],
"directives": []
}
}