Skip to content

Commit

Permalink
chore(transformer): Enforce definitive signature for getDeclarationKe…
Browse files Browse the repository at this point in the history
…yMap and add a comment to why we can do that
  • Loading branch information
martinjlowm committed May 16, 2020
1 parent 7480aad commit 11abd1b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 38 deletions.
8 changes: 1 addition & 7 deletions src/transformer/descriptor/typeParameter/typeParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ export function GetTypeParameterDescriptor(node: ts.TypeParameterDeclaration, sc
throw new Error(`Failed to determine the owner (parent) of the type parameter: \`${declaration.getText()}'.`);
}

const genericKey: string | undefined = MockDefiner.instance.getDeclarationKeyMap(typeDeclaration);

if (!genericKey) {
throw new Error(
`Failed to look up generic key in MockDefiner for \`${typeDeclaration.getText()}'.`,
);
}
const genericKey: string = MockDefiner.instance.getDeclarationKeyMap(typeDeclaration);

return createFunctionToAccessToGenericValue(genericKey + node.name.escapedText, descriptor);
}
Expand Down
34 changes: 10 additions & 24 deletions src/transformer/mockDefiner/mockDefiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,7 @@ export class MockDefiner {
public createMockFactory(declaration: ts.Declaration): void {
const thisFileName: string = this._fileName;

const key: string | undefined = this.getDeclarationKeyMap(declaration);

if (!key) {
throw new Error(
`Failed to obtain key while creating mock factory for \`${declaration.getText()}'.`,
);
}
const key: string = this.getDeclarationKeyMap(declaration);

this._factoryCache.set(declaration, key);

Expand Down Expand Up @@ -152,24 +146,22 @@ export class MockDefiner {
return this._getCallGetFactory(key);
}

public getDeclarationKeyMap(declaration: ts.Declaration): string | undefined {
public getDeclarationKeyMap(declaration: ts.Declaration): string {
let key: string | undefined;

if (!this._declarationCache.has(declaration)) {
const key: string = this._factoryUniqueName.createForDeclaration(declaration as PossibleDeclaration);
key = this._factoryUniqueName.createForDeclaration(declaration as PossibleDeclaration);

this._declarationCache.set(declaration, key);
}

return this._declarationCache.get(declaration);
// NOTE: TypeScript does not support inference through has/get, but we know
// for a fact that the result here is a string!
return (key || this._declarationCache.get(declaration)) as string;
}

public storeRegisterMockFor(declaration: ts.Declaration, factory: ts.FunctionExpression): void {
const key: string | undefined = this.getDeclarationKeyMap(declaration);

if (!key) {
throw new Error(
`Failed to obtain key while storing mock for \`${declaration.getText()}'.`,
);
}
const key: string = this.getDeclarationKeyMap(declaration);

this._registerMockFactoryCache.set(declaration, key);

Expand Down Expand Up @@ -207,13 +199,7 @@ export class MockDefiner {
return cachedFactory;
}

const key: string | undefined = this.getDeclarationKeyMap(declaration);

if (!key) {
throw new Error(
`Failed to obtain key while resolving factory identifier (internal) for \`${declaration.getText()}'.`,
);
}
const key: string = this.getDeclarationKeyMap(declaration);

this._factoryCache.set(declaration, key);

Expand Down
8 changes: 1 addition & 7 deletions src/transformer/mockFactoryCall/mockFactoryCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ export function GetMockFactoryCallIntersection(intersection: ts.IntersectionType
const declarations: ts.Declaration[] | ts.TypeLiteralNode[] = intersection.types.map((type: ts.TypeNode) => {
if (ts.isTypeReferenceNode(type)) {
const declaration: ts.Declaration = TypescriptHelper.GetDeclarationFromNode(type.typeName);
const declarationKey: string | undefined = MockDefiner.instance.getDeclarationKeyMap(declaration);

if (!declarationKey) {
throw new Error(
`Failed to look up declaration key in MockDefiner for \`${declaration.getText()}'.`,
);
}
const declarationKey: string = MockDefiner.instance.getDeclarationKeyMap(declaration);

genericDeclaration.addFromTypeReferenceNode(type, declarationKey);

Expand Down

0 comments on commit 11abd1b

Please sign in to comment.