Skip to content

Commit 7cc1ec0

Browse files
committed
fix(transformer): apply null and warning when property type cannot be identified
1 parent db1b15c commit 7cc1ec0

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

src/transformer/descriptor/property/propertySignature.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as ts from 'typescript';
22
import { Scope } from '../../scope/scope';
33
import { GetDescriptor } from '../descriptor';
4+
import { TransformerLogger } from '../../logger/transformerLogger';
5+
import { GetNullDescriptor } from '../null/null';
46
import { PropertySignatureCache } from './cache';
57

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

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

2425
return GetDescriptor(node.initializer, scope);

src/transformer/logger/transformerLogger.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface TransformerLogger {
1414

1515
typeOfFunctionCallNotFound(node: string): void;
1616

17+
typeOfPropertyNotFound(node: ts.Node): void;
18+
1719
indexedAccessTypeFailed(
1820
propertyName: string,
1921
nodeText: string,
@@ -83,6 +85,17 @@ export function TransformerLogger(): TransformerLogger {
8385
`Cannot find type of function call: ${node} - it will convert to null`
8486
);
8587
},
88+
typeOfPropertyNotFound(node: ts.Node): void {
89+
const createMockNode: ts.Node = GetCurrentCreateMock();
90+
91+
const createMockFileUrl: string = getNodeFileUrl(createMockNode);
92+
const currentNodeFileUrl: string = getNodeFileUrl(node);
93+
94+
logger.warning(
95+
`The transformer could not determine a property value for ${node.getText()} without a specified type nor an initializer value - it will convert to null
96+
${warningPositionLog(createMockFileUrl, currentNodeFileUrl)}`
97+
);
98+
},
8699
indexedAccessTypeFailed(
87100
propertyName: string,
88101
nodeText: string,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createMock } from 'ts-auto-mock';
2+
import { getLogsByCreateMockFileName, UnsupportedTypeLog } from '../utils/log';
3+
import { InterfacePropFallbackAny } from './untypedProperty.warning.type';
4+
5+
describe('Untyped property Warning', () => {
6+
it('should log a warning and apply null to the property', async () => {
7+
const logs: UnsupportedTypeLog[] = await getLogsByCreateMockFileName(
8+
'untypedProperty.warning.test.ts'
9+
);
10+
11+
createMock<InterfacePropFallbackAny>();
12+
expect(logs.length).toBe(1);
13+
14+
expect(logs[0].header).toContain(
15+
'WARNING: Transformer - The transformer could not determine' +
16+
' a property value for prop; ' +
17+
'without a specified type nor an initializer value - it will convert to null'
18+
);
19+
expect(logs[0].created).toMatch(
20+
/created file:\/\/.*untypedProperty\.warning\.test\.ts:[0-9]*:[0-9]*/
21+
);
22+
expect(logs[0].usedBy).toMatch(
23+
/used by file:\/\/.*untypedProperty\.warning\.type\.ts:[0-9]*:[0-9]*/
24+
);
25+
});
26+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface InterfacePropFallbackAny {
2+
// eslint-disable-next-line @typescript-eslint/typedef
3+
prop;
4+
}

0 commit comments

Comments
 (0)