forked from typescript-eslint/typescript-eslint
/
TypeVisitor.ts
285 lines (240 loc) · 8.78 KB
/
TypeVisitor.ts
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
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/types';
import { Referencer } from './Referencer';
import { Visitor } from './Visitor';
import { ParameterDefinition, TypeDefinition } from '../definition';
import { ScopeType } from '../scope';
class TypeVisitor extends Visitor {
readonly #referencer: Referencer;
constructor(referencer: Referencer) {
super(referencer);
this.#referencer = referencer;
}
static visit(referencer: Referencer, node: TSESTree.Node): void {
const typeReferencer = new TypeVisitor(referencer);
typeReferencer.visit(node);
}
///////////////////
// Visit helpers //
///////////////////
protected visitFunctionType(
node:
| TSESTree.TSCallSignatureDeclaration
| TSESTree.TSConstructorType
| TSESTree.TSConstructSignatureDeclaration
| TSESTree.TSFunctionType
| TSESTree.TSMethodSignature,
): void {
// arguments and type parameters can only be referenced from within the function
this.#referencer.scopeManager.nestFunctionTypeScope(node);
this.visit(node.typeParameters);
for (const param of node.params) {
let didVisitAnnotation = false;
this.visitPattern(param, (pattern, info) => {
// a parameter name creates a value type variable which can be referenced later via typeof arg
this.#referencer
.currentScope()
.defineIdentifier(
pattern,
new ParameterDefinition(pattern, node, info.rest),
);
if (pattern.typeAnnotation) {
this.visit(pattern.typeAnnotation);
didVisitAnnotation = true;
}
});
// there are a few special cases where the type annotation is owned by the parameter, not the pattern
if (!didVisitAnnotation && 'typeAnnotation' in param) {
this.visit(param.typeAnnotation);
}
}
this.visit(node.returnType);
this.#referencer.close(node);
}
protected visitPropertyKey(
node: TSESTree.TSMethodSignature | TSESTree.TSPropertySignature,
): void {
if (!node.computed) {
return;
}
// computed members are treated as value references, and TS expects they have a literal type
this.#referencer.visit(node.key);
}
/////////////////////
// Visit selectors //
/////////////////////
protected Identifier(node: TSESTree.Identifier): void {
this.#referencer.currentScope().referenceType(node);
}
protected MemberExpression(node: TSESTree.MemberExpression): void {
this.visit(node.object);
// don't visit the property
}
protected TSCallSignatureDeclaration(
node: TSESTree.TSCallSignatureDeclaration,
): void {
this.visitFunctionType(node);
}
protected TSConditionalType(node: TSESTree.TSConditionalType): void {
// conditional types can define inferred type parameters
// which are only accessible from inside the conditional parameter
this.#referencer.scopeManager.nestConditionalTypeScope(node);
// type parameters inferred in the condition clause are not accessible within the false branch
this.visitChildren(node, ['falseType']);
this.#referencer.close(node);
this.visit(node.falseType);
}
protected TSConstructorType(node: TSESTree.TSConstructorType): void {
this.visitFunctionType(node);
}
protected TSConstructSignatureDeclaration(
node: TSESTree.TSConstructSignatureDeclaration,
): void {
this.visitFunctionType(node);
}
protected TSFunctionType(node: TSESTree.TSFunctionType): void {
this.visitFunctionType(node);
}
protected TSImportType(node: TSESTree.TSImportType): void {
// the TS parser allows any type to be the parameter, but it's a syntax error - so we can ignore it
this.visit(node.typeParameters);
// the qualifier is just part of a standard EntityName, so it should not be visited
}
protected TSIndexSignature(node: TSESTree.TSIndexSignature): void {
for (const param of node.parameters) {
if (param.type === AST_NODE_TYPES.Identifier) {
this.visit(param.typeAnnotation);
}
}
this.visit(node.typeAnnotation);
}
protected TSInferType(node: TSESTree.TSInferType): void {
const typeParameter = node.typeParameter;
let scope = this.#referencer.currentScope();
/*
In cases where there is a sub-type scope created within a conditional type, then the generic should be defined in the
conditional type's scope, not the child type scope.
If we define it within the child type's scope then it won't be able to be referenced outside the child type
*/
if (
scope.type === ScopeType.functionType ||
scope.type === ScopeType.mappedType
) {
// search up the scope tree to figure out if we're in a nested type scope
let currentScope = scope.upper;
while (currentScope) {
if (
currentScope.type === ScopeType.functionType ||
currentScope.type === ScopeType.mappedType
) {
// ensure valid type parents only
currentScope = currentScope.upper;
continue;
}
if (currentScope.type === ScopeType.conditionalType) {
scope = currentScope;
break;
}
break;
}
}
scope.defineIdentifier(
typeParameter.name,
new TypeDefinition(typeParameter.name, typeParameter),
);
}
protected TSInterfaceDeclaration(
node: TSESTree.TSInterfaceDeclaration,
): void {
this.#referencer
.currentScope()
.defineIdentifier(node.id, new TypeDefinition(node.id, node));
if (node.typeParameters) {
// type parameters cannot be referenced from outside their current scope
this.#referencer.scopeManager.nestTypeScope(node);
this.visit(node.typeParameters);
}
node.extends?.forEach(this.visit, this);
node.implements?.forEach(this.visit, this);
this.visit(node.body);
if (node.typeParameters) {
this.#referencer.close(node);
}
}
protected TSMappedType(node: TSESTree.TSMappedType): void {
// mapped types key can only be referenced within their return value
this.#referencer.scopeManager.nestMappedTypeScope(node);
this.visitChildren(node);
this.#referencer.close(node);
}
protected TSMethodSignature(node: TSESTree.TSMethodSignature): void {
this.visitPropertyKey(node);
this.visitFunctionType(node);
}
protected TSNamedTupleMember(node: TSESTree.TSNamedTupleMember): void {
this.visit(node.elementType);
// we don't visit the label as the label only exists for the purposes of documentation
}
protected TSPropertySignature(node: TSESTree.TSPropertySignature): void {
this.visitPropertyKey(node);
this.visit(node.typeAnnotation);
}
protected TSQualifiedName(node: TSESTree.TSQualifiedName): void {
this.visit(node.left);
// we don't visit the right as it a name on the thing, not a name to reference
}
protected TSTypeAliasDeclaration(
node: TSESTree.TSTypeAliasDeclaration,
): void {
this.#referencer
.currentScope()
.defineIdentifier(node.id, new TypeDefinition(node.id, node));
if (node.typeParameters) {
// type parameters cannot be referenced from outside their current scope
this.#referencer.scopeManager.nestTypeScope(node);
this.visit(node.typeParameters);
}
this.visit(node.typeAnnotation);
if (node.typeParameters) {
this.#referencer.close(node);
}
}
protected TSTypeParameter(node: TSESTree.TSTypeParameter): void {
this.#referencer
.currentScope()
.defineIdentifier(node.name, new TypeDefinition(node.name, node));
this.visit(node.constraint);
this.visit(node.default);
}
protected TSTypePredicate(node: TSESTree.TSTypePredicate): void {
if (node.parameterName.type !== AST_NODE_TYPES.TSThisType) {
this.#referencer.currentScope().referenceValue(node.parameterName);
}
this.visit(node.typeAnnotation);
}
// a type query `typeof foo` is a special case that references a _non-type_ variable,
protected TSTypeQuery(node: TSESTree.TSTypeQuery): void {
if (node.exprName.type === AST_NODE_TYPES.Identifier) {
this.#referencer.currentScope().referenceValue(node.exprName);
} else if (
(node.exprName.type as unknown) === AST_NODE_TYPES.ThisExpression
) {
// technically exprName is either Identifier or QualifiedName, but eslint does not recognize `typeof this`,
// so we have translated it to ThisExpression.
return;
} else {
let expr = node.exprName.left;
while (expr.type === TSESTree.AST_NODE_TYPES.TSQualifiedName) {
expr = expr.left;
}
if ((expr.type as unknown) === AST_NODE_TYPES.ThisExpression) {
return;
}
this.#referencer.currentScope().referenceValue(expr);
}
}
protected TSTypeAnnotation(node: TSESTree.TSTypeAnnotation): void {
// check
this.visitChildren(node);
}
}
export { TypeVisitor };