@@ -98,6 +98,14 @@ export function getVariableValue(node: ts.Node): string | Record<string, any> {
98
98
return serializeObject ( node ) ;
99
99
}
100
100
101
+ if ( ts . isIdentifier ( node ) ) {
102
+ const declaration = getIdentifierDeclaration ( node ) ;
103
+ if ( ts . isVariableDeclaration ( declaration ) && declaration . initializer ) {
104
+ return getVariableValue ( declaration . initializer ) ;
105
+ }
106
+ // TODO: If this is another imported value from another file, we'll need to go fetch it like in getPropertyValue
107
+ }
108
+
101
109
throw Error ( `Unsuppored Node: cannot get value of node (${ node . getText ( ) } ) of kind ${ node . kind } ` ) ;
102
110
}
103
111
@@ -112,10 +120,11 @@ export function serializeObject(node: ts.Node) {
112
120
if ( typeof propertyName === 'undefined' ) {
113
121
throw new Error ( `Unable to get property name ${ property . getText ( ) } ` ) ;
114
122
}
123
+ const cleanPropertyName = propertyName . replace ( / [ " ' ] / g, '' ) ;
115
124
if ( ts . isPropertyAssignment ( property ) ) {
116
- value [ propertyName ] = getVariableValue ( property . initializer ) ;
125
+ value [ cleanPropertyName ] = getVariableValue ( property . initializer ) ;
117
126
} else {
118
- value [ propertyName ] = getVariableValue ( property ) ;
127
+ value [ cleanPropertyName ] = getVariableValue ( property ) ;
119
128
}
120
129
}
121
130
@@ -222,9 +231,29 @@ export const flattenKeys = (obj: any, keyPath: any[] = []): any => {
222
231
} ;
223
232
224
233
export function difference ( actual : any , expected : any ) {
225
- function changes ( obj : any , base : any ) {
234
+ function changes ( obj : { [ key : string ] : any } , base : { [ key : string ] : any } ) {
226
235
return transform ( obj , function ( result , value , key ) {
227
- if ( key && ! isEqual ( value , base [ key ] ) ) {
236
+ if ( key && / @ @ I N D E X @ @ / . test ( `${ key } ` ) ) {
237
+ // The type definition is an Index Signature, fuzzy searching for similar keys
238
+ const regexp = new RegExp ( `${ key } ` . replace ( / @ @ I N D E X @ @ / g, '(.+)?' ) ) ;
239
+ const keysInBase = Object . keys ( base )
240
+ . map ( ( k ) => {
241
+ const match = k . match ( regexp ) ;
242
+ return match && match [ 0 ] ;
243
+ } )
244
+ . filter ( ( s ) : s is string => ! ! s ) ;
245
+
246
+ if ( keysInBase . length === 0 ) {
247
+ // Mark this key as wrong because we couldn't find any matching keys
248
+ result [ key ] = value ;
249
+ }
250
+
251
+ keysInBase . forEach ( ( k ) => {
252
+ if ( ! isEqual ( value , base [ k ] ) ) {
253
+ result [ k ] = isObject ( value ) && isObject ( base [ k ] ) ? changes ( value , base [ k ] ) : value ;
254
+ }
255
+ } ) ;
256
+ } else if ( key && ! isEqual ( value , base [ key ] ) ) {
228
257
result [ key ] = isObject ( value ) && isObject ( base [ key ] ) ? changes ( value , base [ key ] ) : value ;
229
258
}
230
259
} ) ;
0 commit comments