From e0353eee3d9655b8f291b2bb0114d73b5052331e Mon Sep 17 00:00:00 2001 From: Martin Jesper Low Madsen Date: Sat, 9 May 2020 13:48:49 +0200 Subject: [PATCH] chore(transformer): Enforce definitive signature for getDeclarationKeyMap and add a comment to why we can do that --- .../descriptor/typeParameter/typeParameter.ts | 8 +---- src/transformer/mockDefiner/mockDefiner.ts | 34 ++++++------------- .../mockFactoryCall/mockFactoryCall.ts | 8 +---- 3 files changed, 12 insertions(+), 38 deletions(-) diff --git a/src/transformer/descriptor/typeParameter/typeParameter.ts b/src/transformer/descriptor/typeParameter/typeParameter.ts index 39f2cd49c..744e18655 100644 --- a/src/transformer/descriptor/typeParameter/typeParameter.ts +++ b/src/transformer/descriptor/typeParameter/typeParameter.ts @@ -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); } diff --git a/src/transformer/mockDefiner/mockDefiner.ts b/src/transformer/mockDefiner/mockDefiner.ts index 56bde1e5b..a4e878ef5 100644 --- a/src/transformer/mockDefiner/mockDefiner.ts +++ b/src/transformer/mockDefiner/mockDefiner.ts @@ -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); @@ -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); @@ -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); diff --git a/src/transformer/mockFactoryCall/mockFactoryCall.ts b/src/transformer/mockFactoryCall/mockFactoryCall.ts index cfb79b3d6..c49f74142 100644 --- a/src/transformer/mockFactoryCall/mockFactoryCall.ts +++ b/src/transformer/mockFactoryCall/mockFactoryCall.ts @@ -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);