-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnodeMap.ts
More file actions
132 lines (106 loc) · 3.96 KB
/
nodeMap.ts
File metadata and controls
132 lines (106 loc) · 3.96 KB
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
import {
TypeNode,
Node,
isTypeAliasDeclaration,
isInterfaceDeclaration,
InterfaceDeclaration,
isIntersectionTypeNode,
isModuleDeclaration,
ModuleDeclaration,
TypeLiteralNode,
isTypeLiteralNode,
isUnionTypeNode
} from 'typescript';
import {isNull} from 'util';
import {constantsFromModule} from './phpClass';
export class NodeMap {
aliases: TypeAliasMap = new TypeAliasMap();
intersections: IntersectionMap = new IntersectionMap();
unions: UnionMap = new UnionMap();
interfaces: InterfaceMap = new InterfaceMap();
modules: ModuleMap = new ModuleMap();
typeLiterals = new TypeLiteralMap();
hasIntersectionOrInterface(name: string) {
return this.intersections.has(name) || this.interfaces.has(name);
}
}
class TypeAliasMap extends Map<string, TypeNode> {
}
class IntersectionMap extends Map<string, TypeNode> {
}
class UnionMap extends Map<string, string[]> {
}
class TypeLiteralMap extends Map<string, TypeLiteralNode> {
}
class InterfaceMap extends Map<string, InterfaceDeclaration> {
}
class ModuleMap extends Map<string, ModuleDeclaration> {
}
export function createNodeMap(nodes: Node[], filter: RegExp = null): NodeMap {
const map = new NodeMap();
nodes.forEach((node: Node) => {
node.forEachChild((node: Node) => {
if (!isNull(filter) && !filter.test(node.getText())) {
return;
}
if (isTypeAliasDeclaration(node)) {
if (isIntersectionTypeNode(node.type)) {
map.intersections.set(node.name.escapedText.toString(), node.type);
return;
}
if (isUnionTypeNode(node.type)) {
if (node.name.escapedText.toString() ==='TextDocumentContentChangeEvent') {
const incremental = node.type.types[0];
const full = node.type.types[1];
if (!isTypeLiteralNode(incremental) || !isTypeLiteralNode(full)) {
return;
}
map.typeLiterals.set(
'TextDocumentContentChangeIncrementalEvent',
incremental
);
map.typeLiterals.set(
'TextDocumentContentChangeFullEvent',
full
);
map.unions.set(node.name.escapedText.toString(), [
'TextDocumentContentChangeIncrementalEvent',
'TextDocumentContentChangeFullEvent'
]);
return;
}
}
if ([
'InlayHint',
'InlineValueText',
'InlineValueVariableLookup',
'InlineValueEvaluatableExpression'
].includes(node.name.escapedText.toString())) {
if (isTypeLiteralNode(node.type)) {
map.typeLiterals.set(node.name.escapedText.toString(), node.type);
}
}
map.aliases.set(node.name.escapedText.toString(), node.type);
return;
}
if (isModuleDeclaration(node)) {
if (constantsFromModule(node).size === 0) {
return;
}
// type aliases have priority
if (!map.aliases.has(node.name.text)) {
map.modules.set(node.name.text, node);
}
return;
}
if (isInterfaceDeclaration(node)) {
// type aliases have priority
if (!map.aliases.has(node.name.escapedText.toString())) {
map.interfaces.set(node.name.escapedText.toString(), node);
return;
}
}
});
});
return map;
}