-
Notifications
You must be signed in to change notification settings - Fork 1
/
type-deduction.js
43 lines (38 loc) · 1.52 KB
/
type-deduction.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
const { getType } = require('./util');
// returns array
function deduceType(node, maps, currentFunction, t) {
// if we can get literal type, do that
const literalType = getType(node, t);
if (literalType.type !== 'TSAnyKeyword') return [literalType];
const [variableToTypeMap, functionToTypeMap, argumentToTypeMap] = maps;
let returnType;
// const id = node.find(x => x.type === 'Identifier');
if (node.type === 'Identifier') {
const { name, type } = node;
if (variableToTypeMap[name]) {
returnType = variableToTypeMap[name];
}
if (functionToTypeMap[name]) {
returnType = functionToTypeMap[name];
}
// we want this to override variables
if (
currentFunction &&
argumentToTypeMap[`${currentFunction.name}::${name}`]
) {
returnType = argumentToTypeMap[`${currentFunction.name}::${name}`];
}
} else if (node.left) {
returnType =
deduceType(node.left, maps, currentFunction, t) ||
deduceType(node.right, maps, currentFunction, t);
} else if (node.type === 'CallExpression') {
return deduceType(node.callee, maps, currentFunction, t);
} else if (node.type === 'UnaryExpression') {
if (node.operator === '!') return [t.tsBooleanKeyword()];
return deduceType(node.argument, maps, currentFunction, t);
}
if (/*VERBOSE*/ false) console.log('return type: ', returnType);
return returnType;
}
module.exports = { deduceType };