@@ -14,21 +14,29 @@ namespace ts {
14
14
return getSymbolWalker ;
15
15
16
16
function getSymbolWalker ( accept : ( symbol : Symbol ) => boolean = ( ) => true ) : SymbolWalker {
17
- const visitedTypes = createMap < Type > ( ) ; // Key is id as string
18
- const visitedSymbols = createMap < Symbol > ( ) ; // Key is id as string
17
+ const visitedTypes : Type [ ] = [ ] ; // Sparse array from id to type
18
+ const visitedSymbols : Symbol [ ] = [ ] ; // Sparse array from id to symbol
19
19
20
20
return {
21
21
walkType : type => {
22
- visitedTypes . clear ( ) ;
23
- visitedSymbols . clear ( ) ;
24
- visitType ( type ) ;
25
- return { visitedTypes : arrayFrom ( visitedTypes . values ( ) ) , visitedSymbols : arrayFrom ( visitedSymbols . values ( ) ) } ;
22
+ try {
23
+ visitType ( type ) ;
24
+ return { visitedTypes : getOwnValues ( visitedTypes ) , visitedSymbols : getOwnValues ( visitedSymbols ) } ;
25
+ }
26
+ finally {
27
+ clear ( visitedTypes ) ;
28
+ clear ( visitedSymbols ) ;
29
+ }
26
30
} ,
27
31
walkSymbol : symbol => {
28
- visitedTypes . clear ( ) ;
29
- visitedSymbols . clear ( ) ;
30
- visitSymbol ( symbol ) ;
31
- return { visitedTypes : arrayFrom ( visitedTypes . values ( ) ) , visitedSymbols : arrayFrom ( visitedSymbols . values ( ) ) } ;
32
+ try {
33
+ visitSymbol ( symbol ) ;
34
+ return { visitedTypes : getOwnValues ( visitedTypes ) , visitedSymbols : getOwnValues ( visitedSymbols ) } ;
35
+ }
36
+ finally {
37
+ clear ( visitedTypes ) ;
38
+ clear ( visitedSymbols ) ;
39
+ }
32
40
} ,
33
41
} ;
34
42
@@ -37,11 +45,10 @@ namespace ts {
37
45
return ;
38
46
}
39
47
40
- const typeIdString = type . id . toString ( ) ;
41
- if ( visitedTypes . has ( typeIdString ) ) {
48
+ if ( visitedTypes [ type . id ] ) {
42
49
return ;
43
50
}
44
- visitedTypes . set ( typeIdString , type ) ;
51
+ visitedTypes [ type . id ] = type ;
45
52
46
53
// Reuse visitSymbol to visit the type's symbol,
47
54
// but be sure to bail on recuring into the type if accept declines the symbol.
@@ -66,43 +73,22 @@ namespace ts {
66
73
}
67
74
}
68
75
if ( type . flags & TypeFlags . TypeParameter ) {
69
- visitTypeParameter ( type as TypeParameter ) ;
76
+ visitType ( getConstraintFromTypeParameter ( type as TypeParameter ) ) ;
70
77
}
71
78
if ( type . flags & TypeFlags . UnionOrIntersection ) {
72
- visitUnionOrIntersectionType ( type as UnionOrIntersectionType ) ;
79
+ forEach ( ( type as UnionOrIntersectionType ) . types , visitType ) ;
73
80
}
74
81
if ( type . flags & TypeFlags . Index ) {
75
- visitIndexType ( type as IndexType ) ;
82
+ visitType ( ( type as IndexType ) . type ) ;
76
83
}
77
84
if ( type . flags & TypeFlags . IndexedAccess ) {
78
85
visitIndexedAccessType ( type as IndexedAccessType ) ;
79
86
}
80
87
}
81
88
82
- function visitTypeList ( types : Type [ ] ) : void {
83
- if ( ! types ) {
84
- return ;
85
- }
86
- for ( let i = 0 ; i < types . length ; i ++ ) {
87
- visitType ( types [ i ] ) ;
88
- }
89
- }
90
-
91
89
function visitTypeReference ( type : TypeReference ) : void {
92
90
visitType ( type . target ) ;
93
- visitTypeList ( type . typeArguments ) ;
94
- }
95
-
96
- function visitTypeParameter ( type : TypeParameter ) : void {
97
- visitType ( getConstraintFromTypeParameter ( type ) ) ;
98
- }
99
-
100
- function visitUnionOrIntersectionType ( type : UnionOrIntersectionType ) : void {
101
- visitTypeList ( type . types ) ;
102
- }
103
-
104
- function visitIndexType ( type : IndexType ) : void {
105
- visitType ( type . type ) ;
91
+ forEach ( type . typeArguments , visitType ) ;
106
92
}
107
93
108
94
function visitIndexedAccessType ( type : IndexedAccessType ) : void {
@@ -122,7 +108,7 @@ namespace ts {
122
108
if ( signature . typePredicate ) {
123
109
visitType ( signature . typePredicate . type ) ;
124
110
}
125
- visitTypeList ( signature . typeParameters ) ;
111
+ forEach ( signature . typeParameters , visitType ) ;
126
112
127
113
for ( const parameter of signature . parameters ) {
128
114
visitSymbol ( parameter ) ;
@@ -133,8 +119,8 @@ namespace ts {
133
119
134
120
function visitInterfaceType ( interfaceT : InterfaceType ) : void {
135
121
visitObjectType ( interfaceT ) ;
136
- visitTypeList ( interfaceT . typeParameters ) ;
137
- visitTypeList ( getBaseTypes ( interfaceT ) ) ;
122
+ forEach ( interfaceT . typeParameters , visitType ) ;
123
+ forEach ( getBaseTypes ( interfaceT ) , visitType ) ;
138
124
visitType ( interfaceT . thisType ) ;
139
125
}
140
126
@@ -161,11 +147,11 @@ namespace ts {
161
147
if ( ! symbol ) {
162
148
return ;
163
149
}
164
- const symbolIdString = getSymbolId ( symbol ) . toString ( ) ;
165
- if ( visitedSymbols . has ( symbolIdString ) ) {
150
+ const symbolId = getSymbolId ( symbol ) ;
151
+ if ( visitedSymbols [ symbolId ] ) {
166
152
return ;
167
153
}
168
- visitedSymbols . set ( symbolIdString , symbol ) ;
154
+ visitedSymbols [ symbolId ] = symbol ;
169
155
if ( ! accept ( symbol ) ) {
170
156
return true ;
171
157
}
0 commit comments