/
useFlowType.js
57 lines (51 loc) · 1.49 KB
/
useFlowType.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
const schema = [];
const create = (context) => {
const markTypeAsUsed = (node) => {
context.markVariableAsUsed(node.id.name);
};
const markTypeAsUsedWithGenericType = (node) => {
let typeId;
let scope;
let variable;
if (node.id.type === 'Identifier') {
typeId = node.id;
} else if (node.id.type === 'QualifiedTypeIdentifier') {
typeId = node.id;
do {
typeId = typeId.qualification;
} while (typeId.qualification);
}
for (scope = context.getScope(); scope; scope = scope.upper) {
variable = scope.set.get(typeId.name);
if (variable && variable.defs.length) {
context.markVariableAsUsed(typeId.name);
break;
}
}
};
return {
DeclareClass: markTypeAsUsed,
DeclareFunction: markTypeAsUsed,
DeclareModule: markTypeAsUsed,
DeclareVariable: markTypeAsUsed,
GenericTypeAnnotation: markTypeAsUsedWithGenericType,
TypeParameterDeclaration (node) {
for (const param of node.params) {
if (param.default && param.default.typeParameters) {
if (param.default.type === 'GenericTypeAnnotation') {
markTypeAsUsedWithGenericType(param.default);
}
for (const typeParameterNode of param.default.typeParameters.params) {
if (typeParameterNode.type === 'GenericTypeAnnotation') {
markTypeAsUsedWithGenericType(typeParameterNode);
}
}
}
}
},
};
};
export default {
create,
schema,
};