forked from benjamn/ast-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-annotations.ts
60 lines (51 loc) · 1.72 KB
/
type-annotations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Type annotation defs shared between Flow and TypeScript.
* These defs could not be defined in ./flow.ts or ./typescript.ts directly
* because they use the same name.
*/
import { Fork } from "../types";
import typesPlugin from "../lib/types";
import sharedPlugin from "../lib/shared";
export default function (fork: Fork) {
var types = fork.use(typesPlugin);
var def = types.Type.def;
var or = types.Type.or;
var defaults = fork.use(sharedPlugin).defaults;
var TypeAnnotation = or(
def("TypeAnnotation"),
def("TSTypeAnnotation"),
null
);
var TypeParamDecl = or(
def("TypeParameterDeclaration"),
def("TSTypeParameterDeclaration"),
null
);
def("Identifier")
.field("typeAnnotation", TypeAnnotation, defaults["null"]);
def("ObjectPattern")
.field("typeAnnotation", TypeAnnotation, defaults["null"]);
def("Function")
.field("returnType", TypeAnnotation, defaults["null"])
.field("typeParameters", TypeParamDecl, defaults["null"]);
def("ClassProperty")
.build("key", "value", "typeAnnotation", "static")
.field("value", or(def("Expression"), null))
.field("static", Boolean, defaults["false"])
.field("typeAnnotation", TypeAnnotation, defaults["null"]);
["ClassDeclaration",
"ClassExpression",
].forEach(typeName => {
def(typeName)
.field("typeParameters", TypeParamDecl, defaults["null"])
.field("superTypeParameters",
or(def("TypeParameterInstantiation"),
def("TSTypeParameterInstantiation"),
null),
defaults["null"])
.field("implements",
or([def("ClassImplements")],
[def("TSExpressionWithTypeArguments")]),
defaults.emptyArray);
});
};