-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
inference.js
339 lines (327 loc) 路 13.2 KB
/
inference.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import traverse from "../lib";
import { parse } from "@babel/parser";
import * as t from "@babel/types";
function getPath(code) {
const ast = parse(code, { plugins: ["flow"] });
let path;
traverse(ast, {
Program: function (_path) {
path = _path;
_path.stop();
},
});
return path;
}
describe("inference", function () {
describe("baseTypeStrictlyMatches", function () {
it("it should work with null", function () {
const path = getPath("var x = null; x === null")
.get("body")[1]
.get("expression");
const left = path.get("left");
const right = path.get("right");
const strictMatch = left.baseTypeStrictlyMatches(right);
expect(strictMatch).toBeTruthy();
});
it("it should work with numbers", function () {
const path = getPath("var x = 1; x === 2")
.get("body")[1]
.get("expression");
const left = path.get("left");
const right = path.get("right");
const strictMatch = left.baseTypeStrictlyMatches(right);
expect(strictMatch).toBeTruthy();
});
it("it should bail when type changes", function () {
const path = getPath("var x = 1; if (foo) x = null;else x = 3; x === 2")
.get("body")[2]
.get("expression");
const left = path.get("left");
const right = path.get("right");
const strictMatch = left.baseTypeStrictlyMatches(right);
expect(strictMatch).toBeFalsy();
});
it("it should differentiate between null and undefined", function () {
const path = getPath("var x; x === null")
.get("body")[1]
.get("expression");
const left = path.get("left");
const right = path.get("right");
const strictMatch = left.baseTypeStrictlyMatches(right);
expect(strictMatch).toBeFalsy();
});
});
describe("getTypeAnnotation", function () {
it("should infer from type cast", function () {
const path = getPath("(x: number)").get("body")[0].get("expression");
expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
});
it("should infer string from template literal", function () {
const path = getPath("`hey`").get("body")[0].get("expression");
expect(t.isStringTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
});
it("should infer number from +x", function () {
const path = getPath("+x").get("body")[0].get("expression");
expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
});
it("should infer T from new T", function () {
const path = getPath("new T").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "T",
).toBeTruthy();
});
it("should infer number from ++x", function () {
const path = getPath("++x").get("body")[0].get("expression");
expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
});
it("should infer number from --x", function () {
const path = getPath("--x").get("body")[0].get("expression");
expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
});
it("should infer void from void x", function () {
const path = getPath("void x").get("body")[0].get("expression");
expect(t.isVoidTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
});
it("should infer string from typeof x", function () {
const path = getPath("typeof x").get("body")[0].get("expression");
expect(t.isStringTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
});
it("should infer boolean from !x", function () {
const path = getPath("!x").get("body")[0].get("expression");
expect(t.isBooleanTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
});
it("should infer type of sequence expression", function () {
const path = getPath("a,1").get("body")[0].get("expression");
expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
});
it("should infer type of logical expression", function () {
const path = getPath("'a' && 1").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy();
expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
});
it("should infer type of conditional expression", function () {
const path = getPath("q ? true : 0").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
expect(t.isBooleanTypeAnnotation(type.types[0])).toBeTruthy();
expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
});
it("should infer RegExp from RegExp literal", function () {
const path = getPath("/.+/").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "RegExp",
).toBeTruthy();
});
it("should infer Object from object expression", function () {
const path = getPath("({ a: 5 })").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "Object",
).toBeTruthy();
});
it("should infer Array from array expression", function () {
const path = getPath("[ 5 ]").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "Array",
).toBeTruthy();
});
it("should infer Function from function", function () {
const path = getPath("(function (): string {})")
.get("body")[0]
.get("expression");
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "Function",
).toBeTruthy();
});
it("should infer Array type from outside of function declaration", function () {
const path = getPath(`
const arr = []; function func() { const a = arr }`).get(
"body.1.body.body.0.declarations.0",
);
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "Array",
).toBeTruthy();
});
it("should infer Array type from outside of function expression", function () {
const path = getPath(`
const arr = []; const func = function () { const a = arr }`).get(
"body.1.declarations.0.init.body.body.0.declarations.0",
);
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "Array",
).toBeTruthy();
});
it("should infer Array type from outside of arrow function", function () {
const path = getPath(`
const arr = []; const func = () => { const a = arr }`).get(
"body.1.declarations.0.init.body.body.0.declarations.0",
);
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "Array",
).toBeTruthy();
});
it("should infer call return type using function", function () {
const path = getPath("(function (): string {})()")
.get("body")[0]
.get("expression");
const type = path.getTypeAnnotation();
expect(t.isStringTypeAnnotation(type)).toBeTruthy();
});
it("should infer call return type using async function", function () {
const path = getPath("(async function (): string {})()")
.get("body")[0]
.get("expression");
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "Promise",
).toBeTruthy();
});
it("should infer call return type using async generator function", function () {
const path = getPath("(async function * (): string {})()")
.get("body")[0]
.get("expression");
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "AsyncIterator",
).toBeTruthy();
});
it("should infer number from x/y", function () {
const path = getPath("x/y").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
});
it("should infer boolean from x instanceof y", function () {
const path = getPath("x instanceof y").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(t.isBooleanTypeAnnotation(type)).toBeTruthy();
});
it("should infer number from 1 + 2", function () {
const path = getPath("1 + 2").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
});
it("should infer string|number from x + y", function () {
const path = getPath("x + y").get("body")[0].get("expression");
const type = path.getTypeAnnotation();
expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy();
expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
});
it("should infer type of tagged template literal", function () {
const path = getPath("(function (): RegExp {}) `hey`")
.get("body")[0]
.get("expression");
const type = path.getTypeAnnotation();
expect(
t.isGenericTypeAnnotation(type) && type.id.name === "RegExp",
).toBeTruthy();
});
it("should infer constant identifier", function () {
const path = getPath("const x = 0; x").get("body.1.expression");
const type = path.getTypeAnnotation();
expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
});
it("should infer indirect constant identifier", function () {
const path = getPath("const x = 0; const y = x; y").get(
"body.2.expression",
);
const type = path.getTypeAnnotation();
expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
});
it("should infer identifier type from if statement (===)", function () {
const path = getPath(
`function test(x) {
if (x === true) x;
}`,
).get("body.0.body.body.0.consequent.expression");
const type = path.getTypeAnnotation();
expect(t.isBooleanTypeAnnotation(type)).toBeTruthy();
});
it("should infer identifier type from if statement (typeof)", function () {
let path = getPath(
`function test(x) {
if (typeof x == 'string') x;
}`,
).get("body.0.body.body.0.consequent.expression");
let type = path.getTypeAnnotation();
expect(t.isStringTypeAnnotation(type)).toBeTruthy();
path = getPath(
`function test(x) {
if (typeof x === 'number') x;
}`,
).get("body.0.body.body.0.consequent.expression");
type = path.getTypeAnnotation();
expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
});
it("should infer identifier type from if statement (&&)", function () {
let path = getPath(
`function test(x) {
if (typeof x == 'string' && x === 3) x;
}`,
).get("body.0.body.body.0.consequent.expression");
let type = path.getTypeAnnotation();
expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy();
expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
path = getPath(
`function test(x) {
if (true && x === 3) x;
}`,
).get("body.0.body.body.0.consequent.expression");
type = path.getTypeAnnotation();
expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
path = getPath(
`function test(x) {
if (x === 'test' && true) x;
}`,
).get("body.0.body.body.0.consequent.expression");
type = path.getTypeAnnotation();
expect(t.isStringTypeAnnotation(type)).toBeTruthy();
});
it("should infer identifier type from if statement (||)", function () {
const path = getPath(
`function test(x) {
if (typeof x == 'string' || x === 3) x;
}`,
).get("body.0.body.body.0.consequent.expression");
const type = path.getTypeAnnotation();
expect(t.isAnyTypeAnnotation(type)).toBeTruthy();
});
it("should not infer identifier type from incorrect binding", function () {
const path = getPath(
`function outer(x) {
if (x === 3) {
function inner(x) {
x;
}
}
}`,
).get("body.0.body.body.0.consequent.body.0.body.body.0.expression");
const type = path.getTypeAnnotation();
expect(t.isAnyTypeAnnotation(type)).toBeTruthy();
});
it("should not cause a stack overflow when two variable depend on eachother", function () {
const path = getPath(`
var b, c;
while (0) {
c = 1;
b = c;
}
c = b;
`).get("body.2.expression");
expect(path.toString()).toBe("c = b");
// Note: this could technically be "number | void", but the cycle detection
// logic just bails out to "any" to avoid infinite loops.
expect(path.getTypeAnnotation()).toEqual({ type: "AnyTypeAnnotation" });
});
});
});