File tree Expand file tree Collapse file tree 4 files changed +47
-3
lines changed
test/logs/untypedProperty Expand file tree Collapse file tree 4 files changed +47
-3
lines changed Original file line number Diff line number Diff line change 11import * as ts from 'typescript' ;
22import { Scope } from '../../scope/scope' ;
33import { GetDescriptor } from '../descriptor' ;
4+ import { TransformerLogger } from '../../logger/transformerLogger' ;
5+ import { GetNullDescriptor } from '../null/null' ;
46import { PropertySignatureCache } from './cache' ;
57
68type 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 ) ;
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff line change 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+ / c r e a t e d f i l e : \/ \/ .* u n t y p e d P r o p e r t y \. w a r n i n g \. t e s t \. t s : [ 0 - 9 ] * : [ 0 - 9 ] * /
21+ ) ;
22+ expect ( logs [ 0 ] . usedBy ) . toMatch (
23+ / u s e d b y f i l e : \/ \/ .* u n t y p e d P r o p e r t y \. w a r n i n g \. t y p e \. t s : [ 0 - 9 ] * : [ 0 - 9 ] * /
24+ ) ;
25+ } ) ;
26+ } ) ;
Original file line number Diff line number Diff line change 1+ export interface InterfacePropFallbackAny {
2+ // eslint-disable-next-line @typescript-eslint/typedef
3+ prop ;
4+ }
You can’t perform that action at this time.
0 commit comments