Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(transformer): Enforce definitive signature for getDeclarationKeyMap and add a comment to why we can do that #314

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
martinjlowm marked this conversation as resolved.
Show resolved Hide resolved
// 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