import type { Node } from 'acorn' ; import type { FullAncestorWalkerCallback , RecursiveWalkerFn , WalkerCallback } from 'acorn-walk' ; import { simple , fullAncestor , make } from 'acorn-walk' ; import type * as Ast from './ast' ; type TState = Record < string , unknown > ; type AstSimpleVisitors < N extends Node > = { [ type ] : AstSimpleWalkerFn < N > ; } ; type AstSimpleWalkerFn < N extends Node > = ( node : N , state : TState ) => void ; const ignore = ( ) => { } ; export function walk < S extends Node , N extends Node > ( node , visitors , baseVisitor = base , state ? ) { simple ( node , visitors , baseVisitor , state ) ; } /**
 * An fullAncestor walk is like an ancestor walk, but triggers
 * the callback on each node
 */ export function walkFullAncestor < N extends Node > ( node , callback , baseVisitor = base , state ? ) { fullAncestor < TState > ( node , callback , baseVisitor , state ) ; } export const base = make < TState > ( { ENDProgram ( node , state , c ) { walkArray ( node . body , state , c ) ; walkArray ( node . stylesheets , state , c ) ; walkArray ( node . scripts , state , c ) ; } , ENDTemplate ( node , state , c ) { walkArray ( node . body , state , c ) ; } , ENDPartial ( node , state , c ) { walkArray ( node . params , state , c ) ; walkArray ( node . body , state , c ) ; } , ENDElement ( node , state , c ) { c ( node . name , state ) ; walkArray ( node . attributes , state , c ) ; walkArray ( node . directives , state , c ) ; walkArray ( node . body , state , c ) ; } , ENDAttribute ( node , state , c ) { c ( node . name , state ) ; if ( node . value ) { c ( node . value , state ) ; } } , ENDDirective ( node , state , c ) { c ( node . value , state ) ; } , ENDAttributeValueExpression ( node , state , c ) { walkArray ( node . elements , state , c ) ; } , ENDVariable ( node , state , c ) { c ( node . value , state ) ; } , ENDIfStatement ( node , state , c ) { c ( node . test , state ) ; walkArray ( node . consequent , state , c ) ; } , ENDChooseStatement ( node , state , c ) { walkArray ( node . cases , state , c ) ; } , ENDChooseCase ( node , state , c ) { if ( node . test ) { c ( node . test , state ) ; } walkArray ( node . consequent , state , c ) ; } , ENDForEachStatement ( node , state , c ) { c ( node . select , state ) ; if ( node . key ) { c ( node . key , state ) ; } walkArray ( node . body , state , c ) ; } , ENDPartialStatement ( node , state , c ) { walkArray ( node . params , state , c ) ; } , ENDVariableStatement ( node , state , c ) { walkArray ( node . variables , state , c ) ; } , ENDAttributeStatement ( node , state , c ) { walkArray ( node . attributes , state , c ) ; walkArray ( node . directives , state , c ) ; } , ENDAddClassStatement ( node , state , c ) { walkArray ( node . tokens , state , c ) ; } , ENDInnerHTML ( node , state , c ) { c ( node . value , state ) ; } , ENDGetter ( node , state , c ) { walkArray ( node . path , state , c ) ; } , ENDCaller ( node , state , c ) { c ( node . object , state ) ; c ( node . property , state ) ; walkArray ( node . arguments , state , c ) ; } , ENDFilter ( node , state , c ) { c ( node . object , state ) ; c ( node . expression , state ) ; } , ENDImport : ignore , ENDStylesheet : ignore , ENDScript : ignore , ENDGetterPrefix : ignore } ) ; function walkArray < N extends Node , T > ( nodes , state , c ) { nodes . forEach ( node => c ( node , state ) ) ; }