Skip to content

Commit

Permalink
fix(transformer): do not fail when accessing index of extended this f…
Browse files Browse the repository at this point in the history
…or a computed property
  • Loading branch information
Pmyl authored Sep 17, 2020
1 parent c42157d commit 02e7b12
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/transformer/descriptor/indexedAccess/indexedAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}

Expand Down
30 changes: 26 additions & 4 deletions src/transformer/logger/transformerLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand All @@ -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) => {
Expand Down Expand Up @@ -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)}`
);
},
};
Expand Down
26 changes: 26 additions & 0 deletions test/logs/indexedAccess/indexedAccess.warning.test.ts
Original file line number Diff line number Diff line change
@@ -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<OtherInterface>();

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]*/
);
});
});
7 changes: 7 additions & 0 deletions test/logs/indexedAccess/indexedAccess.warning.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface OtherInterface extends StandardInterface {
'[]': string;
}

export interface StandardInterface {
prop<K extends keyof this>(): this[K];
}

0 comments on commit 02e7b12

Please sign in to comment.