diff --git a/src/transformer/descriptor/indexedAccess/indexedAccess.ts b/src/transformer/descriptor/indexedAccess/indexedAccess.ts index 860afce45..eb38403aa 100644 --- a/src/transformer/descriptor/indexedAccess/indexedAccess.ts +++ b/src/transformer/descriptor/indexedAccess/indexedAccess.ts @@ -23,8 +23,9 @@ export function GetIndexedAccessTypeDescriptor( switch (declaration.kind) { case ts.SyntaxKind.TypeParameter: const propertyNameIdentifier: ts.PropertyName = PropertySignatureCache.instance.get(); - propertyName = (propertyNameIdentifier as ts.Identifier) - .escapedText as string; + propertyName = TypescriptHelper.GetStringPropertyName( + propertyNameIdentifier + ); break; case ts.SyntaxKind.TypeAliasDeclaration: propertyName = (((declaration as ts.TypeAliasDeclaration) @@ -61,7 +62,11 @@ export function GetIndexedAccessTypeDescriptor( if (!propertySymbol) { // FIXME: Currently not all IndexedAccessType transformation are supported. // See https://github.com/Typescript-TDD/ts-auto-mock/issues/201 for more details. - TransformerLogger().indexedAccessTypeFailed(propertyName, node.getText()); + TransformerLogger().indexedAccessTypeFailed( + propertyName, + node.getText(), + node + ); return GetNullDescriptor(); } diff --git a/src/transformer/logger/transformerLogger.ts b/src/transformer/logger/transformerLogger.ts index 5fefcdbed..c1db7045c 100644 --- a/src/transformer/logger/transformerLogger.ts +++ b/src/transformer/logger/transformerLogger.ts @@ -14,7 +14,11 @@ export interface TransformerLogger { typeOfFunctionCallNotFound(node: string): void; - indexedAccessTypeFailed(propertyName: string, nodeText: string): void; + indexedAccessTypeFailed( + propertyName: string, + nodeText: string, + currentNode: ts.Node + ): void; } const notSupportedTypeMessage: ( @@ -26,7 +30,15 @@ const notSupportedTypeMessage: ( createMockFileUrl: string, currentNodeFileUrl: string ) => `Not supported type: ${type} - it will convert to null -created ${createMockFileUrl} +${warningPositionLog(createMockFileUrl, currentNodeFileUrl)}`; + +const warningPositionLog: ( + createMockFileUrl: string, + currentNodeFileUrl: string +) => string = ( + createMockFileUrl: string, + currentNodeFileUrl: string +) => `created ${createMockFileUrl} used by ${currentNodeFileUrl}`; export const getNodeFileUrl: (node: ts.Node) => string = (node: ts.Node) => { @@ -71,9 +83,19 @@ export function TransformerLogger(): TransformerLogger { `Cannot find type of function call: ${node} - it will convert to null` ); }, - indexedAccessTypeFailed(propertyName: string, nodeText: string): void { + indexedAccessTypeFailed( + propertyName: string, + nodeText: string, + currentNode: ts.Node + ): void { + const createMockNode: ts.Node = GetCurrentCreateMock(); + + const createMockFileUrl: string = getNodeFileUrl(createMockNode); + const currentNodeFileUrl: string = getNodeFileUrl(currentNode); + logger.warning( - `IndexedAccessType transformation failed: cannot find property ${propertyName} of - ${nodeText}` + `IndexedAccessType transformation failed: cannot find property ${propertyName} of - ${nodeText} +${warningPositionLog(createMockFileUrl, currentNodeFileUrl)}` ); }, }; diff --git a/test/logs/indexedAccess/indexedAccess.warning.test.ts b/test/logs/indexedAccess/indexedAccess.warning.test.ts new file mode 100644 index 000000000..c889d98ed --- /dev/null +++ b/test/logs/indexedAccess/indexedAccess.warning.test.ts @@ -0,0 +1,26 @@ +import { createMock } from 'ts-auto-mock'; +import { getLogsByCreateMockFileName, UnsupportedTypeLog } from '../utils/log'; +import { OtherInterface } from './indexedAccess.warning.type'; + +describe('IndexedAccess Warning', () => { + it('should log unsupported warning and assign null for `this` in extended interface with computed property name', async () => { + const mock: OtherInterface = createMock(); + + expect(mock.prop<'[]'>()).toBeNull(); + + const logs: UnsupportedTypeLog[] = await getLogsByCreateMockFileName( + 'indexedAccess.warning.test.ts' + ); + expect(logs.length).toBe(1); + + expect(logs[0].header).toContain( + 'WARNING: Transformer - IndexedAccessType transformation failed: cannot find property [] of - this[K]' + ); + expect(logs[0].created).toMatch( + /created file:\/\/.*indexedAccess\.warning\.test\.ts:[0-9]*:[0-9]*/ + ); + expect(logs[0].usedBy).toMatch( + /used by file:\/\/.*indexedAccess\.warning\.type\.ts:[0-9]*:[0-9]*/ + ); + }); +}); diff --git a/test/logs/indexedAccess/indexedAccess.warning.type.ts b/test/logs/indexedAccess/indexedAccess.warning.type.ts new file mode 100644 index 000000000..f3dad7bb5 --- /dev/null +++ b/test/logs/indexedAccess/indexedAccess.warning.type.ts @@ -0,0 +1,7 @@ +export interface OtherInterface extends StandardInterface { + '[]': string; +} + +export interface StandardInterface { + prop(): this[K]; +}