Reshape SymbolWalker API

1. Expose visited types and symbols
2. Automatically reset before each walk
This commit is contained in:
Andrew Casey
2017-08-22 11:33:32 -07:00
parent 2c8a5c40b8
commit 801c1f70a2
2 changed files with 18 additions and 12 deletions
+16 -9
View File
@@ -13,27 +13,34 @@ namespace ts {
return getSymbolWalker;
function getSymbolWalker(accept: (symbol: Symbol) => boolean = () => true): SymbolWalker {
let visited: Type[] = [];
let visitedTypes: Type[] = [];
let visitedSymbols: Symbol[] = [];
return {
visitType,
visitSymbol,
reset: (newCallback: (symbol: Symbol) => boolean = () => true) => {
accept = newCallback;
visited = [];
walkType: type =>
{
visitedTypes = [];
visitedSymbols = [];
}
visitType(type);
return { visitedTypes, visitedSymbols };
},
walkSymbol: symbol =>
{
visitedTypes = [];
visitedSymbols = [];
visitSymbol(symbol);
return { visitedTypes, visitedSymbols };
},
};
function visitType(type: Type): void {
if (!type) {
return;
}
if (contains(visited, type)) {
if (contains(visitedTypes, type)) {
return;
}
visited.push(type);
visitedTypes.push(type);
// Reuse visitSymbol to visit the type's symbol,
// but be sure to bail on recuring into the type if accept declines the symbol.
+2 -3
View File
@@ -2673,9 +2673,8 @@ namespace ts {
/* @internal */
export interface SymbolWalker {
visitType(type: Type): void;
visitSymbol(symbol: Symbol): void;
reset(accept?: (symbol: Symbol) => boolean): void;
walkType(root: Type) : { visitedTypes: Type[], visitedSymbols: Symbol[] };
walkSymbol(root: Symbol) : { visitedTypes: Type[], visitedSymbols: Symbol[] };
}
export interface SymbolDisplayBuilder {