Skip to content

Commit 11abd1b

Browse files
committed
chore(transformer): Enforce definitive signature for getDeclarationKeyMap and add a comment to why we can do that
1 parent 7480aad commit 11abd1b

File tree

3 files changed

+12
-38
lines changed

3 files changed

+12
-38
lines changed

src/transformer/descriptor/typeParameter/typeParameter.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ export function GetTypeParameterDescriptor(node: ts.TypeParameterDeclaration, sc
2020
throw new Error(`Failed to determine the owner (parent) of the type parameter: \`${declaration.getText()}'.`);
2121
}
2222

23-
const genericKey: string | undefined = MockDefiner.instance.getDeclarationKeyMap(typeDeclaration);
24-
25-
if (!genericKey) {
26-
throw new Error(
27-
`Failed to look up generic key in MockDefiner for \`${typeDeclaration.getText()}'.`,
28-
);
29-
}
23+
const genericKey: string = MockDefiner.instance.getDeclarationKeyMap(typeDeclaration);
3024

3125
return createFunctionToAccessToGenericValue(genericKey + node.name.escapedText, descriptor);
3226
}

src/transformer/mockDefiner/mockDefiner.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,7 @@ export class MockDefiner {
110110
public createMockFactory(declaration: ts.Declaration): void {
111111
const thisFileName: string = this._fileName;
112112

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);
120114

121115
this._factoryCache.set(declaration, key);
122116

@@ -152,24 +146,22 @@ export class MockDefiner {
152146
return this._getCallGetFactory(key);
153147
}
154148

155-
public getDeclarationKeyMap(declaration: ts.Declaration): string | undefined {
149+
public getDeclarationKeyMap(declaration: ts.Declaration): string {
150+
let key: string | undefined;
151+
156152
if (!this._declarationCache.has(declaration)) {
157-
const key: string = this._factoryUniqueName.createForDeclaration(declaration as PossibleDeclaration);
153+
key = this._factoryUniqueName.createForDeclaration(declaration as PossibleDeclaration);
158154

159155
this._declarationCache.set(declaration, key);
160156
}
161157

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;
163161
}
164162

165163
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);
173165

174166
this._registerMockFactoryCache.set(declaration, key);
175167

@@ -207,13 +199,7 @@ export class MockDefiner {
207199
return cachedFactory;
208200
}
209201

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);
217203

218204
this._factoryCache.set(declaration, key);
219205

src/transformer/mockFactoryCall/mockFactoryCall.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@ export function GetMockFactoryCallIntersection(intersection: ts.IntersectionType
3030
const declarations: ts.Declaration[] | ts.TypeLiteralNode[] = intersection.types.map((type: ts.TypeNode) => {
3131
if (ts.isTypeReferenceNode(type)) {
3232
const declaration: ts.Declaration = TypescriptHelper.GetDeclarationFromNode(type.typeName);
33-
const declarationKey: string | undefined = MockDefiner.instance.getDeclarationKeyMap(declaration);
34-
35-
if (!declarationKey) {
36-
throw new Error(
37-
`Failed to look up declaration key in MockDefiner for \`${declaration.getText()}'.`,
38-
);
39-
}
33+
const declarationKey: string = MockDefiner.instance.getDeclarationKeyMap(declaration);
4034

4135
genericDeclaration.addFromTypeReferenceNode(type, declarationKey);
4236

0 commit comments

Comments
 (0)