Skip to content

Commit 57390f8

Browse files
committed
Refactor type resolution logic in getTypeOfSymbol for improved readability and maintainability
1 parent 0aac720 commit 57390f8

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

src/compiler/checker.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12282,36 +12282,33 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1228212282
}
1228312283

1228412284
function getTypeOfSymbol(symbol: Symbol): Type {
12285+
// Retrieve the check flags for the given symbol
1228512286
const checkFlags = getCheckFlags(symbol);
12286-
if (checkFlags & CheckFlags.DeferredType) {
12287-
return getTypeOfSymbolWithDeferredType(symbol);
12288-
}
12289-
if (checkFlags & CheckFlags.Instantiated) {
12290-
return getTypeOfInstantiatedSymbol(symbol);
12291-
}
12292-
if (checkFlags & CheckFlags.Mapped) {
12293-
return getTypeOfMappedSymbol(symbol as MappedSymbol);
12294-
}
12295-
if (checkFlags & CheckFlags.ReverseMapped) {
12296-
return getTypeOfReverseMappedSymbol(symbol as ReverseMappedSymbol);
12297-
}
12298-
if (symbol.flags & (SymbolFlags.Variable | SymbolFlags.Property)) {
12299-
return getTypeOfVariableOrParameterOrProperty(symbol);
12300-
}
12301-
if (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) {
12302-
return getTypeOfFuncClassEnumModule(symbol);
12303-
}
12304-
if (symbol.flags & SymbolFlags.EnumMember) {
12305-
return getTypeOfEnumMember(symbol);
12306-
}
12307-
if (symbol.flags & SymbolFlags.Accessor) {
12308-
return getTypeOfAccessors(symbol);
12309-
}
12310-
if (symbol.flags & SymbolFlags.Alias) {
12311-
return getTypeOfAlias(symbol);
12312-
}
12287+
12288+
// Define a mapping of flag checks to their respective type resolver functions
12289+
const typeResolvers: [number, (sym: Symbol) => Type][] = [
12290+
// Check for deferred type symbols
12291+
[CheckFlags.DeferredType, getTypeOfSymbolWithDeferredType], // Check for instantiated symbols
12292+
[CheckFlags.Instantiated, getTypeOfInstantiatedSymbol], // Check for mapped symbols (cast to MappedSymbol)
12293+
[CheckFlags.Mapped, (sym) => getTypeOfMappedSymbol(sym as MappedSymbol)], // Check for reverse-mapped symbols (cast to ReverseMappedSymbol)
12294+
[CheckFlags.ReverseMapped, (sym) => getTypeOfReverseMappedSymbol(sym as ReverseMappedSymbol)], // Check for variables and properties
12295+
[SymbolFlags.Variable | SymbolFlags.Property, getTypeOfVariableOrParameterOrProperty], // Check for functions, methods, classes, enums, and value modules
12296+
[SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule, getTypeOfFuncClassEnumModule], // Check for enum members
12297+
[SymbolFlags.EnumMember, getTypeOfEnumMember], // Check for accessor symbols
12298+
[SymbolFlags.Accessor, getTypeOfAccessors], // Check for alias symbols
12299+
[SymbolFlags.Alias, getTypeOfAlias],
12300+
];
12301+
12302+
// Iterate through the mapping and return the corresponding type if a flag matches
12303+
for (const [flag, resolver] of typeResolvers) {
12304+
if ((checkFlags & flag) || (symbol.flags & flag)) {
12305+
return resolver(symbol);
12306+
}
12307+
}
12308+
12309+
// Return the error type if no conditions match
1231312310
return errorType;
12314-
}
12311+
}
1231512312

1231612313
function getNonMissingTypeOfSymbol(symbol: Symbol) {
1231712314
return removeMissingType(getTypeOfSymbol(symbol), !!(symbol.flags & SymbolFlags.Optional));

0 commit comments

Comments
 (0)