From 7cc1ec0deb9e1832d68732d7dbb40bd6bf87ad43 Mon Sep 17 00:00:00 2001 From: Pmyl Date: Fri, 18 Sep 2020 18:47:29 +0100 Subject: [PATCH] fix(transformer): apply null and warning when property type cannot be identified --- .../descriptor/property/propertySignature.ts | 7 ++--- src/transformer/logger/transformerLogger.ts | 13 ++++++++++ .../untypedProperty.warning.test.ts | 26 +++++++++++++++++++ .../untypedProperty.warning.type.ts | 4 +++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 test/logs/untypedProperty/untypedProperty.warning.test.ts create mode 100644 test/logs/untypedProperty/untypedProperty.warning.type.ts diff --git a/src/transformer/descriptor/property/propertySignature.ts b/src/transformer/descriptor/property/propertySignature.ts index 35f56da00..e13c253e9 100644 --- a/src/transformer/descriptor/property/propertySignature.ts +++ b/src/transformer/descriptor/property/propertySignature.ts @@ -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; @@ -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); diff --git a/src/transformer/logger/transformerLogger.ts b/src/transformer/logger/transformerLogger.ts index c1db7045c..39cfc5eee 100644 --- a/src/transformer/logger/transformerLogger.ts +++ b/src/transformer/logger/transformerLogger.ts @@ -14,6 +14,8 @@ export interface TransformerLogger { typeOfFunctionCallNotFound(node: string): void; + typeOfPropertyNotFound(node: ts.Node): void; + indexedAccessTypeFailed( propertyName: string, nodeText: string, @@ -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, diff --git a/test/logs/untypedProperty/untypedProperty.warning.test.ts b/test/logs/untypedProperty/untypedProperty.warning.test.ts new file mode 100644 index 000000000..11109c145 --- /dev/null +++ b/test/logs/untypedProperty/untypedProperty.warning.test.ts @@ -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(); + 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]*/ + ); + }); +}); diff --git a/test/logs/untypedProperty/untypedProperty.warning.type.ts b/test/logs/untypedProperty/untypedProperty.warning.type.ts new file mode 100644 index 000000000..2efb68db5 --- /dev/null +++ b/test/logs/untypedProperty/untypedProperty.warning.type.ts @@ -0,0 +1,4 @@ +export interface InterfacePropFallbackAny { + // eslint-disable-next-line @typescript-eslint/typedef + prop; +}