-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
inferers.ts
237 lines (197 loc) 路 6.3 KB
/
inferers.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import {
BOOLEAN_BINARY_OPERATORS,
BOOLEAN_UNARY_OPERATORS,
NUMBER_BINARY_OPERATORS,
NUMBER_UNARY_OPERATORS,
STRING_UNARY_OPERATORS,
anyTypeAnnotation,
arrayTypeAnnotation,
booleanTypeAnnotation,
buildMatchMemberExpression,
genericTypeAnnotation,
identifier,
nullLiteralTypeAnnotation,
numberTypeAnnotation,
stringTypeAnnotation,
tupleTypeAnnotation,
unionTypeAnnotation,
voidTypeAnnotation,
isIdentifier,
} from "@babel/types";
import type * as t from "@babel/types";
export { default as Identifier } from "./inferer-reference";
import { createUnionType } from "./util";
import type NodePath from "..";
export function VariableDeclarator(this: NodePath<t.VariableDeclarator>) {
if (!this.get("id").isIdentifier()) return;
return this.get("init").getTypeAnnotation();
}
export function TypeCastExpression(node) {
return node.typeAnnotation;
}
TypeCastExpression.validParent = true;
export function TSAsExpression(node) {
return node.typeAnnotation;
}
TSAsExpression.validParent = true;
export function TSNonNullExpression(this: NodePath<t.TSNonNullExpression>) {
return this.get("expression").getTypeAnnotation();
}
export function NewExpression(this: NodePath<t.NewExpression>, node) {
if (this.get("callee").isIdentifier()) {
// only resolve identifier callee
return genericTypeAnnotation(node.callee);
}
}
export function TemplateLiteral() {
return stringTypeAnnotation();
}
export function UnaryExpression(node) {
const operator = node.operator;
if (operator === "void") {
return voidTypeAnnotation();
} else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
return numberTypeAnnotation();
} else if (STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
return stringTypeAnnotation();
} else if (BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
return booleanTypeAnnotation();
}
}
export function BinaryExpression(this: NodePath<t.BinaryExpression>, node) {
const operator = node.operator;
if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
return numberTypeAnnotation();
} else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
return booleanTypeAnnotation();
} else if (operator === "+") {
const right = this.get("right");
const left = this.get("left");
if (left.isBaseType("number") && right.isBaseType("number")) {
// both numbers so this will be a number
return numberTypeAnnotation();
} else if (left.isBaseType("string") || right.isBaseType("string")) {
// one is a string so the result will be a string
return stringTypeAnnotation();
}
// unsure if left and right are strings or numbers so stay on the safe side
return unionTypeAnnotation([
stringTypeAnnotation(),
numberTypeAnnotation(),
]);
}
}
export function LogicalExpression(this: NodePath<t.LogicalExpression>) {
const argumentTypes = [
this.get("left").getTypeAnnotation(),
this.get("right").getTypeAnnotation(),
];
return createUnionType(argumentTypes);
}
export function ConditionalExpression(this: NodePath<t.ConditionalExpression>) {
const argumentTypes = [
this.get("consequent").getTypeAnnotation(),
this.get("alternate").getTypeAnnotation(),
];
return createUnionType(argumentTypes);
}
export function SequenceExpression(this: NodePath<t.SequenceExpression>) {
return this.get("expressions").pop().getTypeAnnotation();
}
export function ParenthesizedExpression(
this: NodePath<t.ParenthesizedExpression>,
) {
return this.get("expression").getTypeAnnotation();
}
export function AssignmentExpression(this: NodePath<t.AssignmentExpression>) {
return this.get("right").getTypeAnnotation();
}
export function UpdateExpression(this: NodePath<t.UpdateExpression>, node) {
const operator = node.operator;
if (operator === "++" || operator === "--") {
return numberTypeAnnotation();
}
}
export function StringLiteral() {
return stringTypeAnnotation();
}
export function NumericLiteral() {
return numberTypeAnnotation();
}
export function BooleanLiteral() {
return booleanTypeAnnotation();
}
export function NullLiteral() {
return nullLiteralTypeAnnotation();
}
export function RegExpLiteral() {
return genericTypeAnnotation(identifier("RegExp"));
}
export function ObjectExpression() {
return genericTypeAnnotation(identifier("Object"));
}
export function ArrayExpression() {
return genericTypeAnnotation(identifier("Array"));
}
export function RestElement() {
return ArrayExpression();
}
RestElement.validParent = true;
function Func() {
return genericTypeAnnotation(identifier("Function"));
}
export {
Func as FunctionExpression,
Func as ArrowFunctionExpression,
Func as FunctionDeclaration,
Func as ClassExpression,
Func as ClassDeclaration,
};
const isArrayFrom = buildMatchMemberExpression("Array.from");
const isObjectKeys = buildMatchMemberExpression("Object.keys");
const isObjectValues = buildMatchMemberExpression("Object.values");
const isObjectEntries = buildMatchMemberExpression("Object.entries");
export function CallExpression(this: NodePath<t.CallExpression>) {
const { callee } = this.node;
if (isObjectKeys(callee)) {
return arrayTypeAnnotation(stringTypeAnnotation());
} else if (
isArrayFrom(callee) ||
isObjectValues(callee) ||
// Detect "var foo = Array()" calls so we can optimize for arrays vs iterables.
isIdentifier(callee, { name: "Array" })
) {
return arrayTypeAnnotation(anyTypeAnnotation());
} else if (isObjectEntries(callee)) {
return arrayTypeAnnotation(
tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]),
);
}
return resolveCall(this.get("callee"));
}
export function TaggedTemplateExpression(
this: NodePath<t.TaggedTemplateExpression>,
) {
return resolveCall(this.get("tag"));
}
function resolveCall(callee) {
callee = callee.resolve();
if (callee.isFunction()) {
const { node } = callee;
if (node.async) {
if (node.generator) {
return genericTypeAnnotation(identifier("AsyncIterator"));
} else {
return genericTypeAnnotation(identifier("Promise"));
}
} else {
if (node.generator) {
return genericTypeAnnotation(identifier("Iterator"));
} else if (callee.node.returnType) {
return callee.node.returnType;
} else {
// todo: get union type of all return arguments
}
}
}
}