1- import type { JSONValue } from '@aws-lambda-powertools/commons/types' ;
21import {
32 isIntegerNumber ,
43 isRecord ,
@@ -13,7 +12,7 @@ import {
1312} from './errors.js' ;
1413import { Expression } from './Expression.js' ;
1514import { Functions } from './Functions.js' ;
16- import type { Node , TreeInterpreterOptions } from './types.js' ;
15+ import type { Node , TreeInterpreterOptions , JSONObject } from './types.js' ;
1716import { isTruthy , sliceArray } from './utils.js' ;
1817
1918/**
@@ -47,7 +46,7 @@ class TreeInterpreter {
4746 * @param node The node to visit.
4847 * @param value The current value to visit.
4948 */
50- public visit ( node : Node , value : JSONValue ) : JSONValue | undefined {
49+ public visit ( node : Node , value : JSONObject ) : JSONObject | null {
5150 const nodeType = node . type ;
5251 if ( nodeType === 'subexpression' ) {
5352 return this . #visitSubexpression( node , value ) ;
@@ -116,7 +115,7 @@ class TreeInterpreter {
116115 * @param node The subexpression node to visit.
117116 * @param value The current value to visit.
118117 */
119- #visitSubexpression( node : Node , value : JSONValue ) : JSONValue {
118+ #visitSubexpression( node : Node , value : JSONObject ) : JSONObject {
120119 let result = value ;
121120 for ( const child of node . children ) {
122121 result = this . visit ( child , result ) ;
@@ -131,14 +130,14 @@ class TreeInterpreter {
131130 * @param node The field node to visit.
132131 * @param value The current value to visit.
133132 */
134- #visitField( node : Node , value : JSONValue ) : JSONValue {
133+ #visitField( node : Node , value : JSONObject ) : JSONObject {
135134 if ( ! node . value ) return null ;
136135 if (
137136 isRecord ( value ) &&
138137 typeof node . value === 'string' &&
139138 node . value in value
140139 ) {
141- return value [ node . value ] ;
140+ return value [ node . value ] as JSONObject ;
142141 } else {
143142 return null ;
144143 }
@@ -150,7 +149,7 @@ class TreeInterpreter {
150149 * @param node The comparator node to visit.
151150 * @param value The current value to visit.
152151 */
153- #visitComparator( node : Node , value : JSONValue ) : JSONValue {
152+ #visitComparator( node : Node , value : JSONObject ) : JSONObject {
154153 const comparator = node . value ;
155154 const left = this . visit ( node . children [ 0 ] , value ) ;
156155 const right = this . visit ( node . children [ 1 ] , value ) ;
@@ -187,7 +186,7 @@ class TreeInterpreter {
187186 * @param node The current node to visit.
188187 * @param value The current value to visit.
189188 */
190- #visitCurrent( _node : Node , value : JSONValue ) : JSONValue {
189+ #visitCurrent( _node : Node , value : JSONObject ) : JSONObject {
191190 return value ;
192191 }
193192
@@ -197,7 +196,7 @@ class TreeInterpreter {
197196 * @param node The expref node to visit.
198197 * @param value The current value to visit.
199198 */
200- #visitExpref( node : Node , _value : JSONValue ) : Expression {
199+ #visitExpref( node : Node , _value : JSONObject ) : Expression {
201200 return new Expression ( node . children [ 0 ] , this ) ;
202201 }
203202
@@ -207,7 +206,7 @@ class TreeInterpreter {
207206 * @param node The function expression node to visit.
208207 * @param value The current value to visit.
209208 */
210- #visitFunctionExpression( node : Node , value : JSONValue ) : JSONValue {
209+ #visitFunctionExpression( node : Node , value : JSONObject ) : JSONObject {
211210 const args = [ ] ;
212211 for ( const child of node . children ) {
213212 args . push ( this . visit ( child , value ) ) ;
@@ -271,7 +270,7 @@ class TreeInterpreter {
271270 * @param node The filter projection node to visit.
272271 * @param value The current value to visit.
273272 */
274- #visitFilterProjection( node : Node , value : JSONValue ) : JSONValue {
273+ #visitFilterProjection( node : Node , value : JSONObject ) : JSONObject {
275274 const base = this . visit ( node . children [ 0 ] , value ) ;
276275 if ( ! Array . isArray ( base ) ) {
277276 return null ;
@@ -296,7 +295,7 @@ class TreeInterpreter {
296295 * @param node The flatten node to visit.
297296 * @param value The current value to visit.
298297 */
299- #visitFlatten( node : Node , value : JSONValue ) : JSONValue {
298+ #visitFlatten( node : Node , value : JSONObject ) : JSONObject {
300299 const base = this . visit ( node . children [ 0 ] , value ) ;
301300 if ( ! Array . isArray ( base ) ) {
302301 return null ;
@@ -319,7 +318,7 @@ class TreeInterpreter {
319318 * @param node The identity node to visit.
320319 * @param value The current value to visit.
321320 */
322- #visitIdentity( _node : Node , value : JSONValue ) : JSONValue {
321+ #visitIdentity( _node : Node , value : JSONObject ) : JSONObject {
323322 return value ;
324323 }
325324
@@ -329,7 +328,7 @@ class TreeInterpreter {
329328 * @param node The index node to visit.
330329 * @param value The current value to visit.
331330 */
332- #visitIndex( node : Node , value : JSONValue ) : JSONValue {
331+ #visitIndex( node : Node , value : JSONObject ) : JSONObject {
333332 if ( ! Array . isArray ( value ) ) {
334333 return null ;
335334 }
@@ -353,7 +352,7 @@ class TreeInterpreter {
353352 * @param node The index expression node to visit.
354353 * @param value The current value to visit.
355354 */
356- #visitIndexExpression( node : Node , value : JSONValue ) : JSONValue {
355+ #visitIndexExpression( node : Node , value : JSONObject ) : JSONObject {
357356 let result = value ;
358357 for ( const child of node . children ) {
359358 result = this . visit ( child , result ) ;
@@ -368,7 +367,7 @@ class TreeInterpreter {
368367 * @param node The slice node to visit.
369368 * @param value The current value to visit.
370369 */
371- #visitSlice( node : Node , value : JSONValue ) : JSONValue {
370+ #visitSlice( node : Node , value : JSONObject ) : JSONObject {
372371 const step = isIntegerNumber ( node . children [ 2 ] ) ? node . children [ 2 ] : 1 ;
373372 if ( step === 0 ) {
374373 throw new Error ( 'Invalid slice, step cannot be 0' ) ;
@@ -394,7 +393,7 @@ class TreeInterpreter {
394393 * @param node The key-value pair node to visit.
395394 * @param value The current value to visit.
396395 */
397- #visitKeyValPair( node : Node , value : JSONValue ) : JSONValue {
396+ #visitKeyValPair( node : Node , value : JSONObject ) : JSONObject {
398397 return this . visit ( node . children [ 0 ] , value ) ;
399398 }
400399
@@ -404,7 +403,7 @@ class TreeInterpreter {
404403 * @param node The literal node to visit.
405404 * @param value The current value to visit.
406405 */
407- #visitLiteral( node : Node , _value : JSONValue ) : JSONValue {
406+ #visitLiteral( node : Node , _value : JSONObject ) : JSONObject {
408407 return node . value ;
409408 }
410409
@@ -414,11 +413,11 @@ class TreeInterpreter {
414413 * @param node The multi-select object node to visit.
415414 * @param value The current value to visit.
416415 */
417- #visitMultiSelectObject( node : Node , value : JSONValue ) : JSONValue {
416+ #visitMultiSelectObject( node : Node , value : JSONObject ) : JSONObject {
418417 if ( Object . is ( value , null ) ) {
419418 return null ;
420419 }
421- const collected : JSONValue = { } ;
420+ const collected : Record < string , JSONObject > = { } ;
422421 for ( const child of node . children ) {
423422 if ( typeof child . value === 'string' ) {
424423 collected [ child . value ] = this . visit ( child , value ) ;
@@ -434,7 +433,7 @@ class TreeInterpreter {
434433 * @param node The multi-select list node to visit.
435434 * @param value The current value to visit.
436435 */
437- #visitMultiSelectList( node : Node , value : JSONValue ) : JSONValue {
436+ #visitMultiSelectList( node : Node , value : JSONObject ) : JSONObject {
438437 if ( Object . is ( value , null ) ) {
439438 return null ;
440439 }
@@ -452,7 +451,7 @@ class TreeInterpreter {
452451 * @param node The or expression node to visit.
453452 * @param value The current value to visit.
454453 */
455- #visitOrExpression( node : Node , value : JSONValue ) : JSONValue {
454+ #visitOrExpression( node : Node , value : JSONObject ) : JSONObject {
456455 const matched = this . visit ( node . children [ 0 ] , value ) ;
457456 if ( ! isTruthy ( matched ) ) {
458457 return this . visit ( node . children [ 1 ] , value ) ;
@@ -467,7 +466,7 @@ class TreeInterpreter {
467466 * @param node The and expression node to visit.
468467 * @param value The current value to visit.
469468 */
470- #visitAndExpression( node : Node , value : JSONValue ) : JSONValue {
469+ #visitAndExpression( node : Node , value : JSONObject ) : JSONObject {
471470 const matched = this . visit ( node . children [ 0 ] , value ) ;
472471 if ( ! isTruthy ( matched ) ) {
473472 return matched ;
@@ -482,7 +481,7 @@ class TreeInterpreter {
482481 * @param node The not expression node to visit.
483482 * @param value The current value to visit.
484483 */
485- #visitNotExpression( node : Node , value : JSONValue ) : JSONValue {
484+ #visitNotExpression( node : Node , value : JSONObject ) : JSONObject {
486485 const originalResult = this . visit ( node . children [ 0 ] , value ) ;
487486 if ( typeof originalResult === 'number' && originalResult === 0 ) {
488487 // Special case for 0, !0 should be false, not true.
@@ -499,7 +498,7 @@ class TreeInterpreter {
499498 * @param node The pipe node to visit.
500499 * @param value The current value to visit.
501500 */
502- #visitPipe( node : Node , value : JSONValue ) : JSONValue {
501+ #visitPipe( node : Node , value : JSONObject ) : JSONObject {
503502 let result = value ;
504503 for ( const child of node . children ) {
505504 result = this . visit ( child , result ) ;
@@ -514,7 +513,7 @@ class TreeInterpreter {
514513 * @param node The projection node to visit.
515514 * @param value The current value to visit.
516515 */
517- #visitProjection( node : Node , value : JSONValue ) : JSONValue {
516+ #visitProjection( node : Node , value : JSONObject ) : JSONObject {
518517 const base = this . visit ( node . children [ 0 ] , value ) ;
519518 if ( ! Array . isArray ( base ) ) {
520519 return null ;
@@ -536,12 +535,12 @@ class TreeInterpreter {
536535 * @param node The value projection node to visit.
537536 * @param value The current value to visit.
538537 */
539- #visitValueProjection( node : Node , value : JSONValue ) : JSONValue {
538+ #visitValueProjection( node : Node , value : JSONObject ) : JSONObject {
540539 const base = this . visit ( node . children [ 0 ] , value ) ;
541540 if ( ! isRecord ( base ) ) {
542541 return null ;
543542 }
544- const values = Object . values ( base ) ;
543+ const values = Object . values ( base ) as JSONObject [ ] ;
545544 const collected = [ ] ;
546545 for ( const item of values ) {
547546 const current = this . visit ( node . children [ 1 ] , item ) ;
0 commit comments