Minor cleanup to symbolWalker (#18549)
* Minor cleanup to symbolWalker * Undo inlining
This commit is contained in:
Родитель
d0fd964b69
Коммит
38905f46ce
|
@ -994,11 +994,6 @@ namespace ts {
|
|||
|
||||
/**
|
||||
* Gets the owned, enumerable property keys of a map-like.
|
||||
*
|
||||
* NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
|
||||
* Object.keys instead as it offers better performance.
|
||||
*
|
||||
* @param map A map-like.
|
||||
*/
|
||||
export function getOwnKeys<T>(map: MapLike<T>): string[] {
|
||||
const keys: string[] = [];
|
||||
|
@ -1011,6 +1006,17 @@ namespace ts {
|
|||
return keys;
|
||||
}
|
||||
|
||||
export function getOwnValues<T>(sparseArray: T[]): T[] {
|
||||
const values: T[] = [];
|
||||
for (const key in sparseArray) {
|
||||
if (hasOwnProperty.call(sparseArray, key)) {
|
||||
values.push(sparseArray[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
/** Shims `Array.from`. */
|
||||
export function arrayFrom<T, U>(iterator: Iterator<T>, map: (t: T) => U): U[];
|
||||
export function arrayFrom<T>(iterator: Iterator<T>): T[];
|
||||
|
|
|
@ -14,21 +14,29 @@ namespace ts {
|
|||
return getSymbolWalker;
|
||||
|
||||
function getSymbolWalker(accept: (symbol: Symbol) => boolean = () => true): SymbolWalker {
|
||||
const visitedTypes = createMap<Type>(); // Key is id as string
|
||||
const visitedSymbols = createMap<Symbol>(); // Key is id as string
|
||||
const visitedTypes: Type[] = []; // Sparse array from id to type
|
||||
const visitedSymbols: Symbol[] = []; // Sparse array from id to symbol
|
||||
|
||||
return {
|
||||
walkType: type => {
|
||||
visitedTypes.clear();
|
||||
visitedSymbols.clear();
|
||||
visitType(type);
|
||||
return { visitedTypes: arrayFrom(visitedTypes.values()), visitedSymbols: arrayFrom(visitedSymbols.values()) };
|
||||
try {
|
||||
visitType(type);
|
||||
return { visitedTypes: getOwnValues(visitedTypes), visitedSymbols: getOwnValues(visitedSymbols) };
|
||||
}
|
||||
finally {
|
||||
clear(visitedTypes);
|
||||
clear(visitedSymbols);
|
||||
}
|
||||
},
|
||||
walkSymbol: symbol => {
|
||||
visitedTypes.clear();
|
||||
visitedSymbols.clear();
|
||||
visitSymbol(symbol);
|
||||
return { visitedTypes: arrayFrom(visitedTypes.values()), visitedSymbols: arrayFrom(visitedSymbols.values()) };
|
||||
try {
|
||||
visitSymbol(symbol);
|
||||
return { visitedTypes: getOwnValues(visitedTypes), visitedSymbols: getOwnValues(visitedSymbols) };
|
||||
}
|
||||
finally {
|
||||
clear(visitedTypes);
|
||||
clear(visitedSymbols);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -37,11 +45,10 @@ namespace ts {
|
|||
return;
|
||||
}
|
||||
|
||||
const typeIdString = type.id.toString();
|
||||
if (visitedTypes.has(typeIdString)) {
|
||||
if (visitedTypes[type.id]) {
|
||||
return;
|
||||
}
|
||||
visitedTypes.set(typeIdString, type);
|
||||
visitedTypes[type.id] = type;
|
||||
|
||||
// Reuse visitSymbol to visit the type's symbol,
|
||||
// but be sure to bail on recuring into the type if accept declines the symbol.
|
||||
|
@ -79,18 +86,9 @@ namespace ts {
|
|||
}
|
||||
}
|
||||
|
||||
function visitTypeList(types: Type[]): void {
|
||||
if (!types) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < types.length; i++) {
|
||||
visitType(types[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function visitTypeReference(type: TypeReference): void {
|
||||
visitType(type.target);
|
||||
visitTypeList(type.typeArguments);
|
||||
forEach(type.typeArguments, visitType);
|
||||
}
|
||||
|
||||
function visitTypeParameter(type: TypeParameter): void {
|
||||
|
@ -98,7 +96,7 @@ namespace ts {
|
|||
}
|
||||
|
||||
function visitUnionOrIntersectionType(type: UnionOrIntersectionType): void {
|
||||
visitTypeList(type.types);
|
||||
forEach(type.types, visitType);
|
||||
}
|
||||
|
||||
function visitIndexType(type: IndexType): void {
|
||||
|
@ -122,7 +120,7 @@ namespace ts {
|
|||
if (signature.typePredicate) {
|
||||
visitType(signature.typePredicate.type);
|
||||
}
|
||||
visitTypeList(signature.typeParameters);
|
||||
forEach(signature.typeParameters, visitType);
|
||||
|
||||
for (const parameter of signature.parameters){
|
||||
visitSymbol(parameter);
|
||||
|
@ -133,8 +131,8 @@ namespace ts {
|
|||
|
||||
function visitInterfaceType(interfaceT: InterfaceType): void {
|
||||
visitObjectType(interfaceT);
|
||||
visitTypeList(interfaceT.typeParameters);
|
||||
visitTypeList(getBaseTypes(interfaceT));
|
||||
forEach(interfaceT.typeParameters, visitType);
|
||||
forEach(getBaseTypes(interfaceT), visitType);
|
||||
visitType(interfaceT.thisType);
|
||||
}
|
||||
|
||||
|
@ -161,11 +159,11 @@ namespace ts {
|
|||
if (!symbol) {
|
||||
return;
|
||||
}
|
||||
const symbolIdString = getSymbolId(symbol).toString();
|
||||
if (visitedSymbols.has(symbolIdString)) {
|
||||
const symbolId = getSymbolId(symbol);
|
||||
if (visitedSymbols[symbolId]) {
|
||||
return;
|
||||
}
|
||||
visitedSymbols.set(symbolIdString, symbol);
|
||||
visitedSymbols[symbolId] = symbol;
|
||||
if (!accept(symbol)) {
|
||||
return true;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче