📄 TypeVisitor¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 24 |
| 🧱 Classes | 1 |
| 📦 Imports | 8 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/scope-manager/src/referencer/TypeVisitor.ts
📦 Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/types |
AST_NODE_TYPES |
@typescript-eslint/types |
Scope |
../scope |
Referencer |
./Referencer |
ParameterDefinition |
../definition |
TypeDefinition |
../definition |
ScopeType |
../scope |
Visitor |
./Visitor |
Functions¶
TypeVisitor.visit(referencer: Referencer, node: TSESTree.Node): void¶
Parameters:
referencerReferencernodeTSESTree.Node
Returns: void
Calls:
typeReferencer.visit
Code
TypeVisitor.visitFunctionType(node: | TSESTree.TSCallSignatureDeclaration |…): void¶
Parameters:
node| TSESTree.TSCallSignatureDeclaration | TSESTree.TSConstructorType | TSESTree.TSConstructSignatureDeclaration | TSESTree.TSFunctionType | TSESTree.TSMethodSignature
Returns: void
Calls:
this.#referencer.scopeManager.nestFunctionTypeScopethis.visitthis.visitPatternthis.#referencer .currentScope() .defineIdentifierthis.#referencer.close
Internal Comments:
// arguments and type parameters can only be referenced from within the function (x6)
// a parameter name creates a value type variable which can be referenced later via typeof arg (x7)
// there are a few special cases where the type annotation is owned by the parameter, not the pattern
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
Code
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
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!didVisitAnnotation && 'typeAnnotation' in param) {
this.visit(param.typeAnnotation);
}
}
this.visit(node.returnType);
this.#referencer.close(node);
}
TypeVisitor.visitPropertyKey(node: TSESTree.TSMethodSignature | TSESTree.T…): void¶
Parameters:
nodeTSESTree.TSMethodSignature | TSESTree.TSPropertySignature
Returns: void
Calls:
this.#referencer.visit
Internal Comments:
Code
TypeVisitor.Identifier(node: TSESTree.Identifier): void¶
Parameters:
nodeTSESTree.Identifier
Returns: void
Calls:
this.#referencer.currentScope().referenceType
Code
TypeVisitor.MemberExpression(node: TSESTree.MemberExpression): void¶
Parameters:
nodeTSESTree.MemberExpression
Returns: void
Calls:
this.visit
Code
TypeVisitor.TSCallSignatureDeclaration(node: TSESTree.TSCallSignatureDeclaration): void¶
Parameters:
nodeTSESTree.TSCallSignatureDeclaration
Returns: void
Calls:
this.visitFunctionType
Code
TypeVisitor.TSConditionalType(node: TSESTree.TSConditionalType): void¶
Parameters:
nodeTSESTree.TSConditionalType
Returns: void
Calls:
this.#referencer.scopeManager.nestConditionalTypeScopethis.visitChildrenthis.#referencer.closethis.visit
Internal Comments:
// conditional types can define inferred type parameters (x6)
// which are only accessible from inside the conditional parameter (x6)
// type parameters inferred in the condition clause are not accessible within the false branch (x4)
Code
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);
}
TypeVisitor.TSConstructorType(node: TSESTree.TSConstructorType): void¶
Parameters:
nodeTSESTree.TSConstructorType
Returns: void
Calls:
this.visitFunctionType
Code
TypeVisitor.TSConstructSignatureDeclaration(node: TSESTree.TSConstructSignatureDeclaration): void¶
Parameters:
nodeTSESTree.TSConstructSignatureDeclaration
Returns: void
Calls:
this.visitFunctionType
Code
TypeVisitor.TSFunctionType(node: TSESTree.TSFunctionType): void¶
Parameters:
nodeTSESTree.TSFunctionType
Returns: void
Calls:
this.visitFunctionType
Code
TypeVisitor.TSImportType(node: TSESTree.TSImportType): void¶
Parameters:
nodeTSESTree.TSImportType
Returns: void
Calls:
this.visit
Internal Comments:
// the TS parser allows any type to be the parameter, but it's a syntax error - so we can ignore it (x4)
Code
TypeVisitor.TSIndexSignature(node: TSESTree.TSIndexSignature): void¶
Parameters:
nodeTSESTree.TSIndexSignature
Returns: void
Calls:
this.visit
Code
TypeVisitor.TSInferType(node: TSESTree.TSInferType): void¶
Parameters:
nodeTSESTree.TSInferType
Returns: void
Calls:
this.#referencer.currentScopescope.defineIdentifierthis.visit
Internal Comments:
/*
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
*/
// search up the scope tree to figure out if we're in a nested type scope (x2)
// ensure valid type parents only (x3)
Code
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 as Scope | undefined;
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),
);
this.visit(typeParameter.constraint);
}
TypeVisitor.TSInterfaceDeclaration(node: TSESTree.TSInterfaceDeclaration): void¶
Parameters:
nodeTSESTree.TSInterfaceDeclaration
Returns: void
Calls:
this.#referencer .currentScope() .defineIdentifierthis.#referencer.scopeManager.nestTypeScopethis.visitnode.extends.forEachthis.#referencer.close
Internal Comments:
Code
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);
this.visit(node.body);
if (node.typeParameters) {
this.#referencer.close(node);
}
}
TypeVisitor.TSMappedType(node: TSESTree.TSMappedType): void¶
Parameters:
nodeTSESTree.TSMappedType
Returns: void
Calls:
this.#referencer.scopeManager.nestMappedTypeScopethis.#referencer .currentScope() .defineIdentifierthis.visitthis.#referencer.close
Internal Comments:
Code
protected TSMappedType(node: TSESTree.TSMappedType): void {
// mapped types key can only be referenced within their return value
this.#referencer.scopeManager.nestMappedTypeScope(node);
this.#referencer
.currentScope()
.defineIdentifier(node.key, new TypeDefinition(node.key, node));
this.visit(node.constraint);
this.visit(node.nameType);
this.visit(node.typeAnnotation);
this.#referencer.close(node);
}
TypeVisitor.TSMethodSignature(node: TSESTree.TSMethodSignature): void¶
Parameters:
nodeTSESTree.TSMethodSignature
Returns: void
Calls:
this.visitPropertyKeythis.visitFunctionType
Code
TypeVisitor.TSNamedTupleMember(node: TSESTree.TSNamedTupleMember): void¶
Parameters:
nodeTSESTree.TSNamedTupleMember
Returns: void
Calls:
this.visit
Code
TypeVisitor.TSPropertySignature(node: TSESTree.TSPropertySignature): void¶
Parameters:
nodeTSESTree.TSPropertySignature
Returns: void
Calls:
this.visitPropertyKeythis.visit
Code
TypeVisitor.TSQualifiedName(node: TSESTree.TSQualifiedName): void¶
Parameters:
nodeTSESTree.TSQualifiedName
Returns: void
Calls:
this.visit
Code
TypeVisitor.TSTypeAliasDeclaration(node: TSESTree.TSTypeAliasDeclaration): void¶
Parameters:
nodeTSESTree.TSTypeAliasDeclaration
Returns: void
Calls:
this.#referencer .currentScope() .defineIdentifierthis.#referencer.scopeManager.nestTypeScopethis.visitthis.#referencer.close
Internal Comments:
Code
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);
}
}
TypeVisitor.TSTypeParameter(node: TSESTree.TSTypeParameter): void¶
Parameters:
nodeTSESTree.TSTypeParameter
Returns: void
Calls:
this.#referencer .currentScope() .defineIdentifierthis.visit
Code
TypeVisitor.TSTypePredicate(node: TSESTree.TSTypePredicate): void¶
Parameters:
nodeTSESTree.TSTypePredicate
Returns: void
Calls:
this.#referencer.currentScope().referenceValuethis.visit
Code
TypeVisitor.TSTypeAnnotation(node: TSESTree.TSTypeAnnotation): void¶
Parameters:
nodeTSESTree.TSTypeAnnotation
Returns: void
Calls:
this.visitChildren
Internal Comments:
Code
TypeVisitor.TSTypeQuery(node: TSESTree.TSTypeQuery): void¶
Parameters:
nodeTSESTree.TSTypeQuery
Returns: void
Calls:
this.visitthis.#referencer.currentScope().referenceValue
Code
protected TSTypeQuery(node: TSESTree.TSTypeQuery): void {
let entityName:
TSESTree.Identifier | TSESTree.ThisExpression | TSESTree.TSImportType;
if (node.exprName.type === AST_NODE_TYPES.TSQualifiedName) {
let iter = node.exprName;
while (iter.left.type === AST_NODE_TYPES.TSQualifiedName) {
iter = iter.left;
}
entityName = iter.left;
} else {
entityName = node.exprName;
if (node.exprName.type === AST_NODE_TYPES.TSImportType) {
this.visit(node.exprName);
}
}
if (entityName.type === AST_NODE_TYPES.Identifier) {
this.#referencer.currentScope().referenceValue(entityName);
}
this.visit(node.typeArguments);
}
Classes¶
TypeVisitor¶
Extends: Visitor
Methods (24) — full entries under Functions
| Method | Signature |
|---|---|
visit |
(referencer: Referencer, node: TSESTree.Node): void |
visitFunctionType |
(node: \| TSESTree.TSCallSignatureDeclaration \| TSESTree.TSConstructorType \| TSESTree.TSConstru... |
visitPropertyKey |
(node: TSESTree.TSMethodSignature \| TSESTree.TSPropertySignature): void |
Identifier |
(node: TSESTree.Identifier): void |
MemberExpression |
(node: TSESTree.MemberExpression): void |
TSCallSignatureDeclaration |
(node: TSESTree.TSCallSignatureDeclaration): void |
TSConditionalType |
(node: TSESTree.TSConditionalType): void |
TSConstructorType |
(node: TSESTree.TSConstructorType): void |
TSConstructSignatureDeclaration |
(node: TSESTree.TSConstructSignatureDeclaration): void |
TSFunctionType |
(node: TSESTree.TSFunctionType): void |
TSImportType |
(node: TSESTree.TSImportType): void |
TSIndexSignature |
(node: TSESTree.TSIndexSignature): void |
TSInferType |
(node: TSESTree.TSInferType): void |
TSInterfaceDeclaration |
(node: TSESTree.TSInterfaceDeclaration): void |
TSMappedType |
(node: TSESTree.TSMappedType): void |
TSMethodSignature |
(node: TSESTree.TSMethodSignature): void |
TSNamedTupleMember |
(node: TSESTree.TSNamedTupleMember): void |
TSPropertySignature |
(node: TSESTree.TSPropertySignature): void |
TSQualifiedName |
(node: TSESTree.TSQualifiedName): void |
TSTypeAliasDeclaration |
(node: TSESTree.TSTypeAliasDeclaration): void |
TSTypeParameter |
(node: TSESTree.TSTypeParameter): void |
TSTypePredicate |
(node: TSESTree.TSTypePredicate): void |
TSTypeAnnotation |
(node: TSESTree.TSTypeAnnotation): void |
TSTypeQuery |
(node: TSESTree.TSTypeQuery): void |
Generated by Syntax Scribe