forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inferers.js
174 lines (141 loc) · 4.18 KB
/
inferers.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as t from "babel-types";
export { default as Identifier } from "./inferer-reference";
export function VariableDeclarator() {
let id = this.get("id");
if (id.isIdentifier()) {
return this.get("init").getTypeAnnotation();
} else {
return;
}
}
export function TypeCastExpression(node) {
return node.typeAnnotation;
}
TypeCastExpression.validParent = true;
export function NewExpression(node) {
if (this.get("callee").isIdentifier()) {
// only resolve identifier callee
return t.genericTypeAnnotation(node.callee);
}
}
export function TemplateLiteral() {
return t.stringTypeAnnotation();
}
export function UnaryExpression(node) {
let operator = node.operator;
if (operator === "void") {
return t.voidTypeAnnotation();
} else if (t.NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
return t.numberTypeAnnotation();
} else if (t.STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
return t.stringTypeAnnotation();
} else if (t.BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
return t.booleanTypeAnnotation();
}
}
export function BinaryExpression(node) {
let operator = node.operator;
if (t.NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
return t.numberTypeAnnotation();
} else if (t.BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
return t.booleanTypeAnnotation();
} else if (operator === "+") {
let right = this.get("right");
let left = this.get("left");
if (left.isBaseType("number") && right.isBaseType("number")) {
// both numbers so this will be a number
return t.numberTypeAnnotation();
} else if (left.isBaseType("string") || right.isBaseType("string")) {
// one is a string so the result will be a string
return t.stringTypeAnnotation();
}
// unsure if left and right are strings or numbers so stay on the safe side
return t.unionTypeAnnotation([
t.stringTypeAnnotation(),
t.numberTypeAnnotation()
]);
}
}
export function LogicalExpression() {
return t.createUnionTypeAnnotation([
this.get("left").getTypeAnnotation(),
this.get("right").getTypeAnnotation()
]);
}
export function ConditionalExpression() {
return t.createUnionTypeAnnotation([
this.get("consequent").getTypeAnnotation(),
this.get("alternate").getTypeAnnotation()
]);
}
export function SequenceExpression() {
return this.get("expressions").pop().getTypeAnnotation();
}
export function AssignmentExpression() {
return this.get("right").getTypeAnnotation();
}
export function UpdateExpression(node) {
let operator = node.operator;
if (operator === "++" || operator === "--") {
return t.numberTypeAnnotation();
}
}
export function StringLiteral() {
return t.stringTypeAnnotation();
}
export function NumericLiteral() {
return t.numberTypeAnnotation();
}
export function BooleanLiteral() {
return t.booleanTypeAnnotation();
}
export function NullLiteral() {
return t.nullLiteralTypeAnnotation();
}
export function RegExpLiteral() {
return t.genericTypeAnnotation(t.identifier("RegExp"));
}
export function ObjectExpression() {
return t.genericTypeAnnotation(t.identifier("Object"));
}
export function ArrayExpression() {
return t.genericTypeAnnotation(t.identifier("Array"));
}
export function RestElement() {
return ArrayExpression();
}
RestElement.validParent = true;
function Func() {
return t.genericTypeAnnotation(t.identifier("Function"));
}
export {
Func as FunctionExpression,
Func as ArrowFunctionExpression,
Func as FunctionDeclaration,
Func as ClassExpression,
Func as ClassDeclaration
};
export function CallExpression() {
return resolveCall(this.get("callee"));
}
export function TaggedTemplateExpression() {
return resolveCall(this.get("tag"));
}
function resolveCall(callee) {
callee = callee.resolve();
if (callee.isFunction()) {
if (callee.is("async")) {
if (callee.is("generator")) {
return t.genericTypeAnnotation(t.identifier("AsyncIterator"));
} else {
return t.genericTypeAnnotation(t.identifier("Promise"));
}
} else {
if (callee.node.returnType) {
return callee.node.returnType;
} else {
// todo: get union type of all return arguments
}
}
}
}