Debian Codegraph @

packages/babel-traverse/test/inference.js

raw symbols SCIP
1 import { parse } from "@babel/parser";
2 import * as t from "@babel/types";
3
4 import _traverse from "../lib/index.js";
5 const traverse = _traverse.default || _traverse;
6
7 function getPath(code, options) {
8 const ast = parse(code, options);
9 let path;
10 traverse(ast, {
11 Program: function (_path) {
12 path = _path;
13 _path.stop();
14 },
15 });
16 return path;
17 }
18
19 function flowGetPath(code) {
20 return getPath(code, { plugins: ["flow"] });
21 }
22
23 function tsGetPath(code) {
24 return getPath(code, { plugins: ["typescript"] });
25 }
26
27 describe("inference with Flow", function () {
28 describe("baseTypeStrictlyMatches", function () {
29 it("should work with null", function () {
30 const path = flowGetPath("var x = null; x === null")
31 .get("body")[1]
32 .get("expression");
33 const left = path.get("left");
34 const right = path.get("right");
35 const strictMatch = left.baseTypeStrictlyMatches(right);
36
37 expect(strictMatch).toBeTruthy();
38 });
39
40 it("should work with numbers", function () {
41 const path = flowGetPath("var x = 1; x === 2")
42 .get("body")[1]
43 .get("expression");
44 const left = path.get("left");
45 const right = path.get("right");
46 const strictMatch = left.baseTypeStrictlyMatches(right);
47
48 expect(strictMatch).toBeTruthy();
49 });
50
51 it("should bail when type changes", function () {
52 const path = flowGetPath(
53 "var x = 1; if (foo) x = null;else x = 3; x === 2",
54 )
55 .get("body")[2]
56 .get("expression");
57 const left = path.get("left");
58 const right = path.get("right");
59
60 const strictMatch = left.baseTypeStrictlyMatches(right);
61
62 expect(strictMatch).toBeFalsy();
63 });
64
65 it("should differentiate between null and undefined", function () {
66 const path = flowGetPath("var x; x === null")
67 .get("body")[1]
68 .get("expression");
69 const left = path.get("left");
70 const right = path.get("right");
71 const strictMatch = left.baseTypeStrictlyMatches(right);
72
73 expect(strictMatch).toBeFalsy();
74 });
75 });
76 describe("getTypeAnnotation", function () {
77 it("should infer from type cast", function () {
78 const path = flowGetPath("(x: number)").get("body")[0].get("expression");
79 expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
80 });
81 it("should infer string from template literal", function () {
82 const path = flowGetPath("`hey`").get("body")[0].get("expression");
83 expect(t.isStringTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
84 });
85 it("should infer number from +x", function () {
86 const path = flowGetPath("+x").get("body")[0].get("expression");
87 expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
88 });
89 it("should infer T from new T", function () {
90 const path = flowGetPath("new T").get("body")[0].get("expression");
91 const type = path.getTypeAnnotation();
92 expect(
93 t.isGenericTypeAnnotation(type) && type.id.name === "T",
94 ).toBeTruthy();
95 });
96 it("should infer number from ++x", function () {
97 const path = flowGetPath("++x").get("body")[0].get("expression");
98 expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
99 });
100 it("should infer number from --x", function () {
101 const path = flowGetPath("--x").get("body")[0].get("expression");
102 expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
103 });
104 it("should infer void from void x", function () {
105 const path = flowGetPath("void x").get("body")[0].get("expression");
106 expect(t.isVoidTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
107 });
108 it("should infer string from typeof x", function () {
109 const path = flowGetPath("typeof x").get("body")[0].get("expression");
110 expect(t.isStringTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
111 });
112 it("should infer boolean from !x", function () {
113 const path = flowGetPath("!x").get("body")[0].get("expression");
114 expect(t.isBooleanTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
115 });
116 it("should infer type of sequence expression", function () {
117 const path = flowGetPath("a,1").get("body")[0].get("expression");
118 expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
119 });
120 it("should infer type of logical expression", function () {
121 const path = flowGetPath("'a' && 1").get("body")[0].get("expression");
122 const type = path.getTypeAnnotation();
123 expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
124 expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy();
125 expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
126 });
127 it("should infer type of conditional expression", function () {
128 const path = flowGetPath("q ? true : 0").get("body")[0].get("expression");
129 const type = path.getTypeAnnotation();
130 expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
131 expect(t.isBooleanTypeAnnotation(type.types[0])).toBeTruthy();
132 expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
133 });
134 it("should infer RegExp from RegExp literal", function () {
135 const path = flowGetPath("/.+/").get("body")[0].get("expression");
136 const type = path.getTypeAnnotation();
137 expect(
138 t.isGenericTypeAnnotation(type) && type.id.name === "RegExp",
139 ).toBeTruthy();
140 });
141 it("should infer Object from object expression", function () {
142 const path = flowGetPath("({ a: 5 })").get("body")[0].get("expression");
143 const type = path.getTypeAnnotation();
144 expect(
145 t.isGenericTypeAnnotation(type) && type.id.name === "Object",
146 ).toBeTruthy();
147 });
148 it("should infer Array from array expression", function () {
149 const path = flowGetPath("[ 5 ]").get("body")[0].get("expression");
150 const type = path.getTypeAnnotation();
151 expect(
152 t.isGenericTypeAnnotation(type) && type.id.name === "Array",
153 ).toBeTruthy();
154 });
155 it("should infer Function from function", function () {
156 const path = flowGetPath("(function (): string {})")
157 .get("body")[0]
158 .get("expression");
159 const type = path.getTypeAnnotation();
160 expect(
161 t.isGenericTypeAnnotation(type) && type.id.name === "Function",
162 ).toBeTruthy();
163 });
164 it("should infer call return type using function", function () {
165 const path = flowGetPath("(function (): string {})()")
166 .get("body")[0]
167 .get("expression");
168 const type = path.getTypeAnnotation();
169 expect(t.isStringTypeAnnotation(type)).toBeTruthy();
170 });
171 it("should infer call return type using async function", function () {
172 const path = flowGetPath("(async function (): string {})()")
173 .get("body")[0]
174 .get("expression");
175 const type = path.getTypeAnnotation();
176 expect(
177 t.isGenericTypeAnnotation(type) && type.id.name === "Promise",
178 ).toBeTruthy();
179 });
180 it("should infer call return type using async generator function", function () {
181 const path = flowGetPath("(async function * (): string {})()")
182 .get("body")[0]
183 .get("expression");
184 const type = path.getTypeAnnotation();
185 expect(
186 t.isGenericTypeAnnotation(type) && type.id.name === "AsyncIterator",
187 ).toBeTruthy();
188 });
189 it("should infer number from x/y", function () {
190 const path = flowGetPath("x/y").get("body")[0].get("expression");
191 const type = path.getTypeAnnotation();
192 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
193 });
194 it("should infer boolean from x instanceof y", function () {
195 const path = flowGetPath("x instanceof y")
196 .get("body")[0]
197 .get("expression");
198 const type = path.getTypeAnnotation();
199 expect(t.isBooleanTypeAnnotation(type)).toBeTruthy();
200 });
201 it("should infer number from 1 + 2", function () {
202 const path = flowGetPath("1 + 2").get("body")[0].get("expression");
203 const type = path.getTypeAnnotation();
204 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
205 });
206 it("should infer string|number from x + y", function () {
207 const path = flowGetPath("x + y").get("body")[0].get("expression");
208 const type = path.getTypeAnnotation();
209 expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
210 expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy();
211 expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
212 });
213 it("should infer type of tagged template literal", function () {
214 const path = flowGetPath("(function (): RegExp {}) `hey`")
215 .get("body")[0]
216 .get("expression");
217 const type = path.getTypeAnnotation();
218 expect(
219 t.isGenericTypeAnnotation(type) && type.id.name === "RegExp",
220 ).toBeTruthy();
221 });
222 it("should infer constant identifier", function () {
223 const path = flowGetPath("const x = 0; x").get("body.1.expression");
224 const type = path.getTypeAnnotation();
225 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
226 });
227 it("should infer indirect constant identifier", function () {
228 const path = flowGetPath("const x = 0; const y = x; y").get(
229 "body.2.expression",
230 );
231 const type = path.getTypeAnnotation();
232 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
233 });
234 it("should infer identifier type from if statement (===)", function () {
235 const path = flowGetPath(
236 `function test(x) {
237 if (x === true) x;
238 }`,
239 ).get("body.0.body.body.0.consequent.expression");
240 const type = path.getTypeAnnotation();
241 expect(t.isBooleanTypeAnnotation(type)).toBeTruthy();
242 });
243 it("should infer identifier type from if statement (typeof)", function () {
244 let path = flowGetPath(
245 `function test(x) {
246 if (typeof x == 'string') x;
247 }`,
248 ).get("body.0.body.body.0.consequent.expression");
249 let type = path.getTypeAnnotation();
250 expect(t.isStringTypeAnnotation(type)).toBeTruthy();
251 path = flowGetPath(
252 `function test(x) {
253 if (typeof x === 'number') x;
254 }`,
255 ).get("body.0.body.body.0.consequent.expression");
256 type = path.getTypeAnnotation();
257 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
258 });
259 it("should infer identifier type from if statement (&&)", function () {
260 let path = flowGetPath(
261 `function test(x) {
262 if (typeof x == 'string' && x === 3) x;
263 }`,
264 ).get("body.0.body.body.0.consequent.expression");
265 let type = path.getTypeAnnotation();
266 expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
267 expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy();
268 expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
269 path = flowGetPath(
270 `function test(x) {
271 if (true && x === 3) x;
272 }`,
273 ).get("body.0.body.body.0.consequent.expression");
274 type = path.getTypeAnnotation();
275 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
276 path = flowGetPath(
277 `function test(x) {
278 if (x === 'test' && true) x;
279 }`,
280 ).get("body.0.body.body.0.consequent.expression");
281 type = path.getTypeAnnotation();
282 expect(t.isStringTypeAnnotation(type)).toBeTruthy();
283 });
284 it("should infer identifier type from if statement (||)", function () {
285 const path = flowGetPath(
286 `function test(x) {
287 if (typeof x == 'string' || x === 3) x;
288 }`,
289 ).get("body.0.body.body.0.consequent.expression");
290 const type = path.getTypeAnnotation();
291 expect(t.isAnyTypeAnnotation(type)).toBeTruthy();
292 });
293 it("should not infer identifier type from incorrect binding", function () {
294 const path = flowGetPath(
295 `function outer(x) {
296 if (x === 3) {
297 function inner(x) {
298 x;
299 }
300 }
301 }`,
302 ).get("body.0.body.body.0.consequent.body.0.body.body.0.expression");
303 const type = path.getTypeAnnotation();
304 expect(t.isAnyTypeAnnotation(type)).toBeTruthy();
305 });
306 it("should not cause a stack overflow when two variable depend on eachother", function () {
307 const path = flowGetPath(`
308 var b, c;
309 while (0) {
310 c = 1;
311 b = c;
312 }
313 c = b;
314 `).get("body.2.expression");
315
316 expect(path.toString()).toBe("c = b");
317
318 // Note: this could technically be "number | void", but the cycle detection
319 // logic just bails out to "any" to avoid infinite loops.
320 expect(path.getTypeAnnotation()).toEqual({ type: "AnyTypeAnnotation" });
321 });
322 });
323 describe(`isGenericType("Array")`, () => {
324 it.each([
325 "var x = Array()",
326 "var x = Array.from([])",
327 "var x = Object.keys({})",
328 "var x = Object.values({})",
329 "var x = Object.entries({})",
330 "var x = new Array",
331 "const x = [];",
332 "const x = (function (): Array<T> { return []; })()",
333 "const x = (function (): T[] { return []; })()",
334 "const x = (v: Array<T>)",
335 "const x = ({}, [])",
336 "let x = (y: [number, string])",
337 "var x = true ? a() : b(); function a(): Array<number> {}; function b(): Array<number> {}",
338 "var x = a() || b(); function a(): Array<number> {}; function b(): Array<number> {}",
339 ])(`NodePath(%p).isGenericType("Array") should be true`, input => {
340 const path = flowGetPath(input).get("body.0.declarations.0");
341 expect(path.isGenericType("Array")).toBe(true);
342 });
343 it.each([
344 "x = Array()",
345 "x = Array.from([])",
346 "x = Object.keys({})",
347 "x = Object.values({})",
348 "x = Object.entries({})",
349 "x = new Array",
350 "x = [];",
351 "x = (function (): Array<T> { return []; })()",
352 "x = (function (): T[] { return []; })()",
353 "x = (v: Array<T>)",
354 "x = ({}, [])",
355 "x = (y: [number, string])",
356 ])(`NodePath(%p).isGenericType("Array") should be true`, input => {
357 const path = flowGetPath(input).get("body.0.expression");
358 expect(path.isGenericType("Array")).toBe(true);
359 });
360 it.each(["const x = ({}, [])"])(
361 `With { createParenthesizedExpressions: true}, NodePath(%p).isGenericType("Array") should be true`,
362 input => {
363 const path = getPath(input, {
364 plugins: ["flow"],
365 createParenthesizedExpressions: true,
366 }).get("body.0.declarations.0");
367 expect(path.isGenericType("Array")).toBe(true);
368 },
369 );
370 });
371 });
372
373 describe("inference with TypeScript", function () {
374 describe("baseTypeStrictlyMatches", function () {
375 it("should work with null", function () {
376 const path = tsGetPath("var x = null; x === null")
377 .get("body")[1]
378 .get("expression");
379 const left = path.get("left");
380 const right = path.get("right");
381 const strictMatch = left.baseTypeStrictlyMatches(right);
382
383 expect(strictMatch).toBeTruthy();
384 });
385
386 it("should work with numbers", function () {
387 const path = tsGetPath("var x = 1; x === 2")
388 .get("body")[1]
389 .get("expression");
390 const left = path.get("left");
391 const right = path.get("right");
392 const strictMatch = left.baseTypeStrictlyMatches(right);
393
394 expect(strictMatch).toBeTruthy();
395 });
396
397 it("should bail when type changes", function () {
398 const path = tsGetPath("var x = 1; if (foo) x = null;else x = 3; x === 2")
399 .get("body")[2]
400 .get("expression");
401 const left = path.get("left");
402 const right = path.get("right");
403
404 const strictMatch = left.baseTypeStrictlyMatches(right);
405
406 expect(strictMatch).toBeFalsy();
407 });
408
409 it("should differentiate between null and undefined", function () {
410 const path = tsGetPath("var x; x === null")
411 .get("body")[1]
412 .get("expression");
413 const left = path.get("left");
414 const right = path.get("right");
415 const strictMatch = left.baseTypeStrictlyMatches(right);
416
417 expect(strictMatch).toBeFalsy();
418 });
419 });
420 describe("getTypeAnnotation", function () {
421 it("should infer from type cast", function () {
422 const path = tsGetPath("x as number").get("body")[0].get("expression");
423 expect(t.isTSNumberKeyword(path.getTypeAnnotation())).toBeTruthy();
424 });
425 it("should infer from non-null expression", function () {
426 const path = tsGetPath("x as number").get("body")[0].get("expression");
427 expect(t.isTSNumberKeyword(path.getTypeAnnotation())).toBeTruthy();
428 });
429 it("should infer string from template literal", function () {
430 const path = tsGetPath("`hey`").get("body")[0].get("expression");
431 expect(t.isStringTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
432 });
433 it("should infer number from +x", function () {
434 const path = tsGetPath("+x").get("body")[0].get("expression");
435 expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
436 });
437 it("should infer T from new T", function () {
438 const path = tsGetPath("new T").get("body")[0].get("expression");
439 const type = path.getTypeAnnotation();
440 expect(
441 t.isGenericTypeAnnotation(type) && type.id.name === "T",
442 ).toBeTruthy();
443 });
444 it("should infer number from ++x", function () {
445 const path = tsGetPath("++x").get("body")[0].get("expression");
446 expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
447 });
448 it("should infer number from --x", function () {
449 const path = tsGetPath("--x").get("body")[0].get("expression");
450 expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
451 });
452 it("should infer void from void x", function () {
453 const path = tsGetPath("void x").get("body")[0].get("expression");
454 expect(t.isVoidTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
455 });
456 it("should infer string from typeof x", function () {
457 const path = tsGetPath("typeof x").get("body")[0].get("expression");
458 expect(t.isStringTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
459 });
460 it("should infer boolean from !x", function () {
461 const path = tsGetPath("!x").get("body")[0].get("expression");
462 expect(t.isBooleanTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
463 });
464 it("should infer type of sequence expression", function () {
465 const path = tsGetPath("a,1").get("body")[0].get("expression");
466 expect(t.isNumberTypeAnnotation(path.getTypeAnnotation())).toBeTruthy();
467 });
468 it("should infer type of logical expression", function () {
469 const path = tsGetPath("'a' && 1").get("body")[0].get("expression");
470 const type = path.getTypeAnnotation();
471 expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
472 expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy();
473 expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
474 });
475 it("should infer type of conditional expression", function () {
476 const path = tsGetPath("q ? true : 0").get("body")[0].get("expression");
477 const type = path.getTypeAnnotation();
478 expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
479 expect(t.isBooleanTypeAnnotation(type.types[0])).toBeTruthy();
480 expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
481 });
482 it("should infer RegExp from RegExp literal", function () {
483 const path = tsGetPath("/.+/").get("body")[0].get("expression");
484 const type = path.getTypeAnnotation();
485 expect(
486 t.isGenericTypeAnnotation(type) && type.id.name === "RegExp",
487 ).toBeTruthy();
488 });
489 it("should infer Object from object expression", function () {
490 const path = tsGetPath("({ a: 5 })").get("body")[0].get("expression");
491 const type = path.getTypeAnnotation();
492 expect(
493 t.isGenericTypeAnnotation(type) && type.id.name === "Object",
494 ).toBeTruthy();
495 });
496 it("should infer Array from array expression", function () {
497 const path = tsGetPath("[ 5 ]").get("body")[0].get("expression");
498 const type = path.getTypeAnnotation();
499 expect(
500 t.isGenericTypeAnnotation(type) && type.id.name === "Array",
501 ).toBeTruthy();
502 });
503 it("should infer Function from function", function () {
504 const path = tsGetPath("(function (): string {})")
505 .get("body")[0]
506 .get("expression");
507 const type = path.getTypeAnnotation();
508 expect(
509 t.isGenericTypeAnnotation(type) && type.id.name === "Function",
510 ).toBeTruthy();
511 });
512 it("should infer call return type using function", function () {
513 const path = tsGetPath("(function (): string {})()")
514 .get("body")[0]
515 .get("expression");
516 const type = path.getTypeAnnotation();
517 expect(type.type).toBe("TSStringKeyword");
518 });
519 it("should infer call return type using async function", function () {
520 const path = tsGetPath("(async function (): string {})()")
521 .get("body")[0]
522 .get("expression");
523 const type = path.getTypeAnnotation();
524 expect(
525 t.isGenericTypeAnnotation(type) && type.id.name === "Promise",
526 ).toBeTruthy();
527 });
528 it("should infer call return type using async generator function", function () {
529 const path = tsGetPath("(async function * (): string {})()")
530 .get("body")[0]
531 .get("expression");
532 const type = path.getTypeAnnotation();
533 expect(
534 t.isGenericTypeAnnotation(type) && type.id.name === "AsyncIterator",
535 ).toBeTruthy();
536 });
537 it("should infer number from x/y", function () {
538 const path = tsGetPath("x/y").get("body")[0].get("expression");
539 const type = path.getTypeAnnotation();
540 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
541 });
542 it("should infer boolean from x instanceof y", function () {
543 const path = tsGetPath("x instanceof y").get("body")[0].get("expression");
544 const type = path.getTypeAnnotation();
545 expect(t.isBooleanTypeAnnotation(type)).toBeTruthy();
546 });
547 it("should infer number from 1 + 2", function () {
548 const path = tsGetPath("1 + 2").get("body")[0].get("expression");
549 const type = path.getTypeAnnotation();
550 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
551 });
552 it("should infer string|number from x + y", function () {
553 const path = tsGetPath("x + y").get("body")[0].get("expression");
554 const type = path.getTypeAnnotation();
555 expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
556 expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy();
557 expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
558 });
559 it("should infer type of tagged template literal", function () {
560 const path = tsGetPath("(function (): RegExp {}) `hey`")
561 .get("body")[0]
562 .get("expression");
563 const type = path.getTypeAnnotation();
564 expect(type.type).toBe("TSTypeReference");
565 expect(type.typeName.name).toBe("RegExp");
566 });
567 it("should infer constant identifier", function () {
568 const path = tsGetPath("const x = 0; x").get("body.1.expression");
569 const type = path.getTypeAnnotation();
570 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
571 });
572 it("should infer indirect constant identifier", function () {
573 const path = tsGetPath("const x = 0; const y = x; y").get(
574 "body.2.expression",
575 );
576 const type = path.getTypeAnnotation();
577 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
578 });
579 it("should infer identifier type from if statement (===)", function () {
580 const path = tsGetPath(
581 `function test(x) {
582 if (x === true) x;
583 }`,
584 ).get("body.0.body.body.0.consequent.expression");
585 const type = path.getTypeAnnotation();
586 expect(t.isBooleanTypeAnnotation(type)).toBeTruthy();
587 });
588 it("should infer identifier type from if statement (typeof)", function () {
589 let path = tsGetPath(
590 `function test(x) {
591 if (typeof x == 'string') x;
592 }`,
593 ).get("body.0.body.body.0.consequent.expression");
594 let type = path.getTypeAnnotation();
595 expect(t.isStringTypeAnnotation(type)).toBeTruthy();
596 path = tsGetPath(
597 `function test(x) {
598 if (typeof x === 'number') x;
599 }`,
600 ).get("body.0.body.body.0.consequent.expression");
601 type = path.getTypeAnnotation();
602 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
603 });
604 it("should infer identifier type from if statement (&&)", function () {
605 let path = tsGetPath(
606 `function test(x) {
607 if (typeof x == 'string' && x === 3) x;
608 }`,
609 ).get("body.0.body.body.0.consequent.expression");
610 let type = path.getTypeAnnotation();
611 expect(t.isUnionTypeAnnotation(type)).toBeTruthy();
612 expect(t.isStringTypeAnnotation(type.types[0])).toBeTruthy();
613 expect(t.isNumberTypeAnnotation(type.types[1])).toBeTruthy();
614 path = tsGetPath(
615 `function test(x) {
616 if (true && x === 3) x;
617 }`,
618 ).get("body.0.body.body.0.consequent.expression");
619 type = path.getTypeAnnotation();
620 expect(t.isNumberTypeAnnotation(type)).toBeTruthy();
621 path = tsGetPath(
622 `function test(x) {
623 if (x === 'test' && true) x;
624 }`,
625 ).get("body.0.body.body.0.consequent.expression");
626 type = path.getTypeAnnotation();
627 expect(t.isStringTypeAnnotation(type)).toBeTruthy();
628 });
629 it("should infer identifier type from if statement (||)", function () {
630 const path = tsGetPath(
631 `function test(x) {
632 if (typeof x == 'string' || x === 3) x;
633 }`,
634 ).get("body.0.body.body.0.consequent.expression");
635 const type = path.getTypeAnnotation();
636 expect(t.isAnyTypeAnnotation(type)).toBeTruthy();
637 });
638 it("should not infer identifier type from incorrect binding", function () {
639 const path = tsGetPath(
640 `function outer(x) {
641 if (x === 3) {
642 function inner(x) {
643 x;
644 }
645 }
646 }`,
647 ).get("body.0.body.body.0.consequent.body.0.body.body.0.expression");
648 const type = path.getTypeAnnotation();
649 expect(t.isAnyTypeAnnotation(type)).toBeTruthy();
650 });
651 it("should not cause a stack overflow when two variable depend on eachother", function () {
652 const path = tsGetPath(`
653 var b, c;
654 while (0) {
655 c = 1;
656 b = c;
657 }
658 c = b;
659 `).get("body.2.expression");
660
661 expect(path.toString()).toBe("c = b");
662
663 // Note: this could technically be "number | void", but the cycle detection
664 // logic just bails out to "any" to avoid infinite loops.
665 expect(path.getTypeAnnotation()).toEqual({ type: "AnyTypeAnnotation" });
666 });
667 });
668 describe(`isGenericType("Array")`, () => {
669 it.each([
670 "var x = Array()",
671 "var x = Array.from([])",
672 "var x = Object.keys({})",
673 "var x = Object.values({})",
674 "var x = Object.entries({})",
675 "var x = new Array",
676 "const x = [];",
677 "const x = (function (): Array<T> { return []; })()",
678 "const x = (function (): T[] { return []; })()",
679 "const x = (v as Array<T>)",
680 "const x = ({}, [])",
681 "var x = true ? a() : b(); function a(): Array<number> {}; function b(): Array<number> {}",
682 "var x = a() || b(); function a(): Array<number> {}; function b(): Array<number> {}",
683 "let x = []!",
684 ])(`NodePath(%p).isGenericType("Array") should be true`, input => {
685 const path = tsGetPath(input).get("body.0.declarations.0");
686 expect(path.isGenericType("Array")).toBe(true);
687 });
688 it.each([
689 "x = Array()",
690 "x = Array.from([])",
691 "x = Object.keys({})",
692 "x = Object.values({})",
693 "x = Object.entries({})",
694 "x = new Array",
695 "x = [];",
696 "x = (function (): Array<T> { return []; })()",
697 "x = (function (): T[] { return []; })()",
698 "x = (v as Array<T>)",
699 "x = ({}, [])",
700 ])(`NodePath(%p).isGenericType("Array") should be true`, input => {
701 const path = tsGetPath(input).get("body.0.expression");
702 expect(path.isGenericType("Array")).toBe(true);
703 });
704 });
705 });