@@ -204,13 +204,16 @@ export function addVisitorToCompiled(visitor: Visitor): void {
204204 }
205205
206206 // Exit if is empty visitor
207- const keys = Object . keys ( visitor ) ;
208- if ( keys . length === 0 ) return ;
207+ const keys = Object . keys ( visitor ) ,
208+ keysLen = keys . length ;
209+ if ( keysLen === 0 ) return ;
209210
210211 hasActiveVisitors = true ;
211212
212213 // Populate visitors array from provided object
213- for ( let name of keys ) {
214+ for ( let i = 0 ; i < keysLen ; i ++ ) {
215+ let name = keys [ i ] ;
216+
214217 const visitFn = visitor [ name ] ;
215218 if ( typeof visitFn !== 'function' ) {
216219 throw new TypeError ( `'${ name } ' property of visitor object is not a function` ) ;
@@ -295,14 +298,19 @@ export function finalizeCompiledVisitor() {
295298
296299 // Merge visit functions for node types which have multiple visitors from different rules,
297300 // or enter+exit functions for leaf nodes
298- for ( const typeId of mergedLeafVisitorTypeIds ) {
301+ for ( let i = mergedLeafVisitorTypeIds . length - 1 ; i >= 0 ; i -- ) {
302+ const typeId = mergedLeafVisitorTypeIds [ i ] ;
299303 compiledVisitor [ typeId ] = mergeVisitFns ( compiledVisitor [ typeId ] as unknown as VisitFn [ ] ) ;
300304 }
301- for ( const typeId of mergedEnterVisitorTypeIds ) {
305+
306+ for ( let i = mergedEnterVisitorTypeIds . length - 1 ; i >= 0 ; i -- ) {
307+ const typeId = mergedEnterVisitorTypeIds [ i ] ;
302308 const enterExit = compiledVisitor [ typeId ] as CompilingNonLeafVisitorEntry ;
303309 enterExit . enter = mergeVisitFns ( enterExit . enter as VisitFn [ ] ) ;
304310 }
305- for ( const typeId of mergedExitVisitorTypeIds ) {
311+
312+ for ( let i = mergedExitVisitorTypeIds . length - 1 ; i >= 0 ; i -- ) {
313+ const typeId = mergedExitVisitorTypeIds [ i ] ;
306314 const enterExit = compiledVisitor [ typeId ] as CompilingNonLeafVisitorEntry ;
307315 enterExit . exit = mergeVisitFns ( enterExit . exit as VisitFn [ ] ) ;
308316 }
0 commit comments