File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,14 @@ export type AstVisitor = {
3030 DIR ?: VisitKindFn < AstDirNode > ;
3131 FILE ?: VisitKindFn < AstFileNode > ;
3232 ROOT_TYPE ?: VisitKindFn < AstRootTypeNode > ;
33+ /**
34+ * Callback will be called when you start apply visitor to AST.
35+ */
36+ onStart ?: ( ast : AstRootNode , visitor : AstVisitor ) => void ;
37+ /**
38+ * Callback will be called when visitor finishes traversing AST.
39+ */
40+ onFinish ?: ( ast : AstRootNode , visitor : AstVisitor ) => void ;
3341} ;
3442
3543export interface VisitInfo {
@@ -40,6 +48,10 @@ export interface VisitInfo {
4048}
4149
4250export function astVisitor ( ast : AstRootNode , visitor : AstVisitor ) : void {
51+ if ( visitor ?. onStart ) {
52+ visitor . onStart ( ast , visitor ) ;
53+ }
54+
4355 ( Object . keys ( ast . children ) as Array < keyof typeof ast . children > ) . forEach ( ( operation ) => {
4456 const rootNode = ast . children [ operation ] ;
4557 if ( ! rootNode ) return ;
@@ -51,6 +63,10 @@ export function astVisitor(ast: AstRootNode, visitor: AstVisitor): void {
5163 operation,
5264 } ) ;
5365 } ) ;
66+
67+ if ( visitor ?. onFinish ) {
68+ visitor . onFinish ( ast , visitor ) ;
69+ }
5470}
5571
5672export function visitNode (
You can’t perform that action at this time.
0 commit comments