Skip to content

Commit

Permalink
fix(transformer): apply null and warning when property type cannot be…
Browse files Browse the repository at this point in the history
… identified
  • Loading branch information
Pmyl committed Sep 18, 2020
1 parent db1b15c commit 7cc1ec0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/transformer/descriptor/property/propertySignature.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as ts from 'typescript';
import { Scope } from '../../scope/scope';
import { GetDescriptor } from '../descriptor';
import { TransformerLogger } from '../../logger/transformerLogger';
import { GetNullDescriptor } from '../null/null';
import { PropertySignatureCache } from './cache';

type PropertyNode = ts.PropertySignature | ts.PropertyDeclaration;
Expand All @@ -16,9 +18,8 @@ export function GetPropertyDescriptor(
}

if (!node.initializer) {
throw new Error(
`The transformer couldn't determine a property value for \`${node.getText()}' without a specified type nor an initializer value.`
);
TransformerLogger().typeOfPropertyNotFound(node);
return GetNullDescriptor();
}

return GetDescriptor(node.initializer, scope);
Expand Down
13 changes: 13 additions & 0 deletions src/transformer/logger/transformerLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface TransformerLogger {

typeOfFunctionCallNotFound(node: string): void;

typeOfPropertyNotFound(node: ts.Node): void;

indexedAccessTypeFailed(
propertyName: string,
nodeText: string,
Expand Down Expand Up @@ -83,6 +85,17 @@ export function TransformerLogger(): TransformerLogger {
`Cannot find type of function call: ${node} - it will convert to null`
);
},
typeOfPropertyNotFound(node: ts.Node): void {
const createMockNode: ts.Node = GetCurrentCreateMock();

const createMockFileUrl: string = getNodeFileUrl(createMockNode);
const currentNodeFileUrl: string = getNodeFileUrl(node);

logger.warning(
`The transformer could not determine a property value for ${node.getText()} without a specified type nor an initializer value - it will convert to null
${warningPositionLog(createMockFileUrl, currentNodeFileUrl)}`
);
},
indexedAccessTypeFailed(
propertyName: string,
nodeText: string,
Expand Down
26 changes: 26 additions & 0 deletions test/logs/untypedProperty/untypedProperty.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 { InterfacePropFallbackAny } from './untypedProperty.warning.type';

describe('Untyped property Warning', () => {
it('should log a warning and apply null to the property', async () => {
const logs: UnsupportedTypeLog[] = await getLogsByCreateMockFileName(
'untypedProperty.warning.test.ts'
);

createMock<InterfacePropFallbackAny>();
expect(logs.length).toBe(1);

expect(logs[0].header).toContain(
'WARNING: Transformer - The transformer could not determine' +
' a property value for prop; ' +
'without a specified type nor an initializer value - it will convert to null'
);
expect(logs[0].created).toMatch(
/created file:\/\/.*untypedProperty\.warning\.test\.ts:[0-9]*:[0-9]*/
);
expect(logs[0].usedBy).toMatch(
/used by file:\/\/.*untypedProperty\.warning\.type\.ts:[0-9]*:[0-9]*/
);
});
});
4 changes: 4 additions & 0 deletions test/logs/untypedProperty/untypedProperty.warning.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface InterfacePropFallbackAny {
// eslint-disable-next-line @typescript-eslint/typedef
prop;
}

0 comments on commit 7cc1ec0

Please sign in to comment.