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 1
1
import * as ts from 'typescript' ;
2
2
import { Scope } from '../../scope/scope' ;
3
3
import { GetDescriptor } from '../descriptor' ;
4
+ import { TransformerLogger } from '../../logger/transformerLogger' ;
5
+ import { GetNullDescriptor } from '../null/null' ;
4
6
import { PropertySignatureCache } from './cache' ;
5
7
6
8
type PropertyNode = ts . PropertySignature | ts . PropertyDeclaration ;
@@ -16,9 +18,8 @@ export function GetPropertyDescriptor(
16
18
}
17
19
18
20
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 ( ) ;
22
23
}
23
24
24
25
return GetDescriptor ( node . initializer , scope ) ;
Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ export interface TransformerLogger {
14
14
15
15
typeOfFunctionCallNotFound ( node : string ) : void ;
16
16
17
+ typeOfPropertyNotFound ( node : ts . Node ) : void ;
18
+
17
19
indexedAccessTypeFailed (
18
20
propertyName : string ,
19
21
nodeText : string ,
@@ -83,6 +85,17 @@ export function TransformerLogger(): TransformerLogger {
83
85
`Cannot find type of function call: ${ node } - it will convert to null`
84
86
) ;
85
87
} ,
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
+ } ,
86
99
indexedAccessTypeFailed (
87
100
propertyName : string ,
88
101
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