@@ -10,7 +10,6 @@ import {
1010 getAllPdas ,
1111 getAllPrograms ,
1212 InstructionNode ,
13- ProgramNode ,
1413 resolveNestedTypeNode ,
1514 structTypeNodeFromInstructionArgumentNodes ,
1615} from '@codama/nodes' ;
@@ -19,8 +18,10 @@ import {
1918 extendVisitor ,
2019 getResolvedInstructionInputsVisitor ,
2120 LinkableDictionary ,
21+ NodeStack ,
2222 pipe ,
23- recordLinkablesVisitor ,
23+ recordLinkablesOnFirstVisitVisitor ,
24+ recordNodeStackVisitor ,
2425 staticVisitor ,
2526 visit ,
2627} from '@codama/visitors-core' ;
@@ -86,7 +87,7 @@ export type GlobalFragmentScope = {
8687
8788export function getRenderMapVisitor ( options : GetRenderMapOptions = { } ) {
8889 const linkables = new LinkableDictionary ( ) ;
89- let program : ProgramNode | null = null ;
90+ const stack = new NodeStack ( ) ;
9091
9192 const nameTransformers = {
9293 ...DEFAULT_NAME_TRANSFORMERS ,
@@ -140,14 +141,14 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
140141 v =>
141142 extendVisitor ( v , {
142143 visitAccount ( node ) {
143- if ( ! program ) {
144+ if ( ! stack . getProgram ( ) ) {
144145 throw new Error ( 'Account must be visited inside a program.' ) ;
145146 }
146147
147148 const scope = {
148149 ...globalScope ,
149150 accountNode : node ,
150- programNode : program ,
151+ accountStack : stack . clone ( ) ,
151152 typeManifest : visit ( node , typeManifestVisitor ) ,
152153 } ;
153154
@@ -221,7 +222,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
221222 } ,
222223
223224 visitInstruction ( node ) {
224- if ( ! program ) {
225+ if ( ! stack . getProgram ( ) ) {
225226 throw new Error ( 'Instruction must be visited inside a program.' ) ;
226227 }
227228
@@ -237,7 +238,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
237238 } ) ,
238239 ) ,
239240 instructionNode : node ,
240- programNode : program ,
241+ instructionStack : stack ,
241242 renamedArgs : getRenamedArgsMap ( node ) ,
242243 resolvedInputs : visit ( node , resolvedInstructionInputVisitor ) ,
243244 } ;
@@ -290,11 +291,11 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
290291 } ,
291292
292293 visitPda ( node ) {
293- if ( ! program ) {
294+ if ( ! stack . getProgram ( ) ) {
294295 throw new Error ( 'Account must be visited inside a program.' ) ;
295296 }
296297
297- const scope = { ...globalScope , pdaNode : node , programNode : program } ;
298+ const scope = { ...globalScope , pdaNode : node , pdaStack : stack } ;
298299 const pdaFunctionFragment = getPdaFunctionFragment ( scope ) ;
299300 const imports = new ImportMap ( ) . mergeWith ( pdaFunctionFragment ) ;
300301
@@ -308,7 +309,6 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
308309 } ,
309310
310311 visitProgram ( node , { self } ) {
311- program = node ;
312312 const customDataDefinedType = [
313313 ...getDefinedTypeNodesToExtract ( node . accounts , customAccountData ) ,
314314 ...getDefinedTypeNodesToExtract ( node . instructions , customInstructionData ) ,
@@ -349,11 +349,10 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
349349 ) ;
350350
351351 renderMap . mergeWith (
352- ...getAllInstructionsWithSubs ( program , {
352+ ...getAllInstructionsWithSubs ( node , {
353353 leavesOnly : ! renderParentInstructions ,
354354 } ) . map ( ix => visit ( ix , self ) ) ,
355355 ) ;
356- program = null ;
357356 return renderMap ;
358357 } ,
359358
@@ -435,7 +434,8 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
435434 . mergeWith ( ...getAllPrograms ( node ) . map ( p => visit ( p , self ) ) ) ;
436435 } ,
437436 } ) ,
438- v => recordLinkablesVisitor ( v , linkables ) ,
437+ v => recordNodeStackVisitor ( v , stack ) ,
438+ v => recordLinkablesOnFirstVisitVisitor ( v , linkables ) ,
439439 ) ;
440440}
441441
0 commit comments