@@ -110,13 +110,7 @@ export class MockDefiner {
110
110
public createMockFactory ( declaration : ts . Declaration ) : void {
111
111
const thisFileName : string = this . _fileName ;
112
112
113
- const key : string | undefined = this . getDeclarationKeyMap ( declaration ) ;
114
-
115
- if ( ! key ) {
116
- throw new Error (
117
- `Failed to obtain key while creating mock factory for \`${ declaration . getText ( ) } '.` ,
118
- ) ;
119
- }
113
+ const key : string = this . getDeclarationKeyMap ( declaration ) ;
120
114
121
115
this . _factoryCache . set ( declaration , key ) ;
122
116
@@ -152,24 +146,22 @@ export class MockDefiner {
152
146
return this . _getCallGetFactory ( key ) ;
153
147
}
154
148
155
- public getDeclarationKeyMap ( declaration : ts . Declaration ) : string | undefined {
149
+ public getDeclarationKeyMap ( declaration : ts . Declaration ) : string {
150
+ let key : string | undefined ;
151
+
156
152
if ( ! this . _declarationCache . has ( declaration ) ) {
157
- const key : string = this . _factoryUniqueName . createForDeclaration ( declaration as PossibleDeclaration ) ;
153
+ key = this . _factoryUniqueName . createForDeclaration ( declaration as PossibleDeclaration ) ;
158
154
159
155
this . _declarationCache . set ( declaration , key ) ;
160
156
}
161
157
162
- return this . _declarationCache . get ( declaration ) ;
158
+ // NOTE: TypeScript does not support inference through has/get, but we know
159
+ // for a fact that the result here is a string!
160
+ return ( key || this . _declarationCache . get ( declaration ) ) as string ;
163
161
}
164
162
165
163
public storeRegisterMockFor ( declaration : ts . Declaration , factory : ts . FunctionExpression ) : void {
166
- const key : string | undefined = this . getDeclarationKeyMap ( declaration ) ;
167
-
168
- if ( ! key ) {
169
- throw new Error (
170
- `Failed to obtain key while storing mock for \`${ declaration . getText ( ) } '.` ,
171
- ) ;
172
- }
164
+ const key : string = this . getDeclarationKeyMap ( declaration ) ;
173
165
174
166
this . _registerMockFactoryCache . set ( declaration , key ) ;
175
167
@@ -207,13 +199,7 @@ export class MockDefiner {
207
199
return cachedFactory ;
208
200
}
209
201
210
- const key : string | undefined = this . getDeclarationKeyMap ( declaration ) ;
211
-
212
- if ( ! key ) {
213
- throw new Error (
214
- `Failed to obtain key while resolving factory identifier (internal) for \`${ declaration . getText ( ) } '.` ,
215
- ) ;
216
- }
202
+ const key : string = this . getDeclarationKeyMap ( declaration ) ;
217
203
218
204
this . _factoryCache . set ( declaration , key ) ;
219
205
0 commit comments