@@ -17,10 +17,17 @@ export type ParsingExtensionKey =
1717 | [ typeof InArray , number ]
1818 | [ typeof Root ] ;
1919
20+ export interface ParsingContext {
21+ [ k : string ] : string | string [ ] | undefined | ParsingContext ;
22+ }
23+
2024export interface ParsingExtension {
21- ( value : Json , key : ParsingExtensionKey , context : ParsingExtensionKey [ ] ) :
22- | ParsingExtensionTransform
23- | false ;
25+ (
26+ value : Json ,
27+ key : ParsingExtensionKey ,
28+ parentKeys : ParsingExtensionKey [ ] ,
29+ context : ParsingContext ,
30+ ) : ParsingExtensionTransform | false ;
2431
2532 /**
2633 * A globally unique string that identifies what parsing extension this is.
@@ -36,6 +43,7 @@ export type ParsingExtensionTransform = (
3643 metadata ?: ParsedValueMetadata ,
3744 source ?: ConfigSource ,
3845 extensions ?: ParsingExtension [ ] ,
46+ context ?: ParsingContext ,
3947 ) => Promise < ParsedValue > ,
4048 parent : JsonObject | Json [ ] | undefined ,
4149 source : ConfigSource ,
@@ -73,8 +81,9 @@ export class ParsedValue {
7381 source : ConfigSource ,
7482 extensions ?: ParsingExtension [ ] ,
7583 metadata ?: ParsedValueMetadata ,
84+ context ?: ParsingContext ,
7685 ) : Promise < ParsedValue > {
77- return parseValue ( raw , source , extensions , metadata ) ;
86+ return parseValue ( raw , source , extensions , metadata , context ) ;
7887 }
7988
8089 /** Parses (with extensions) from a plain JSON object */
@@ -346,22 +355,24 @@ export async function parseValue(
346355 source : ConfigSource ,
347356 extensions : ParsingExtension [ ] = [ ] ,
348357 metadata : ParsedValueMetadata = { } ,
358+ context : ParsingContext = { } ,
349359) : Promise < ParsedValue > {
350- return parseValueInner ( value , source , extensions , metadata , [ [ Root ] ] , value ) ;
360+ return parseValueInner ( value , source , extensions , metadata , context , [ [ Root ] ] , value ) ;
351361}
352362
353363async function parseValueInner (
354364 value : Json ,
355365 source : ConfigSource ,
356366 extensions : ParsingExtension [ ] ,
357367 metadata : ParsedValueMetadata = { } ,
358- context : ParsingExtensionKey [ ] ,
368+ context : ParsingContext = { } ,
369+ parentKeys : ParsingExtensionKey [ ] ,
359370 root : Json ,
360371 parent ?: JsonObject | Json [ ] ,
361372 visitedExtensions : Set < ParsingExtension | string > = new Set ( ) ,
362373) : Promise < ParsedValue > {
363- const [ currentKey ] = context . slice ( - 1 ) ;
364- const contextualKeys = context . slice ( 0 , context . length - 1 ) ;
374+ const [ currentKey ] = parentKeys . slice ( - 1 ) ;
375+ const parentKeysNext = parentKeys . slice ( 0 , parentKeys . length - 1 ) ;
365376
366377 let applicableExtension : ParsingExtensionTransform | undefined ;
367378
@@ -377,7 +388,7 @@ async function parseValueInner(
377388 if ( visitedExtensions . has ( extension ) ) continue ;
378389 if ( extension . extensionName && visitedExtensions . has ( extension . extensionName ) ) continue ;
379390
380- const applicable = extension ( value , currentKey , contextualKeys ) ;
391+ const applicable = extension ( value , currentKey , parentKeysNext , context ) ;
381392
382393 if ( applicable ) {
383394 applicableExtension = applicable ;
@@ -397,13 +408,15 @@ async function parseValueInner(
397408 metadataOverride ?: ParsedValueMetadata ,
398409 sourceOverride ?: ConfigSource ,
399410 extensionsOverride ?: ParsingExtension [ ] ,
411+ contextOverride ?: ParsingContext ,
400412 ) =>
401413 parseValueInner (
402414 inner ,
403415 sourceOverride ?? source ,
404416 extensionsOverride ?? extensions ,
405417 { ...metadata , ...metadataOverride } ,
406- context ,
418+ { ...context , ...contextOverride } ,
419+ parentKeys ,
407420 root ,
408421 parent ,
409422 visitedExtensions ,
@@ -421,7 +434,8 @@ async function parseValueInner(
421434 source ,
422435 extensions ,
423436 undefined ,
424- context . concat ( [ [ InArray , index ] ] ) ,
437+ context ,
438+ parentKeys . concat ( [ [ InArray , index ] ] ) ,
425439 root ,
426440 value ,
427441 ) ;
@@ -446,7 +460,8 @@ async function parseValueInner(
446460 source ,
447461 extensions ,
448462 undefined ,
449- context . concat ( [ [ InObject , key ] ] ) ,
463+ context ,
464+ parentKeys . concat ( [ [ InObject , key ] ] ) ,
450465 root ,
451466 value ,
452467 ) ;
0 commit comments