File tree 1 file changed +16
-0
lines changed
1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,14 @@ export type AstVisitor = {
30
30
DIR ?: VisitKindFn < AstDirNode > ;
31
31
FILE ?: VisitKindFn < AstFileNode > ;
32
32
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 ;
33
41
} ;
34
42
35
43
export interface VisitInfo {
@@ -40,6 +48,10 @@ export interface VisitInfo {
40
48
}
41
49
42
50
export function astVisitor ( ast : AstRootNode , visitor : AstVisitor ) : void {
51
+ if ( visitor ?. onStart ) {
52
+ visitor . onStart ( ast , visitor ) ;
53
+ }
54
+
43
55
( Object . keys ( ast . children ) as Array < keyof typeof ast . children > ) . forEach ( ( operation ) => {
44
56
const rootNode = ast . children [ operation ] ;
45
57
if ( ! rootNode ) return ;
@@ -51,6 +63,10 @@ export function astVisitor(ast: AstRootNode, visitor: AstVisitor): void {
51
63
operation,
52
64
} ) ;
53
65
} ) ;
66
+
67
+ if ( visitor ?. onFinish ) {
68
+ visitor . onFinish ( ast , visitor ) ;
69
+ }
54
70
}
55
71
56
72
export function visitNode (
You can’t perform that action at this time.
0 commit comments