Skip to content

Commit f2e4053

Browse files
committed
feat(AstVisitor): add onStart, onFinish callbacks to AstVisitor. It allows to add logging/reporting when you apply it to your AST.
1 parent d025dda commit f2e4053

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/astVisitor.ts

+16
Original file line numberDiff line numberDiff 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

3543
export interface VisitInfo {
@@ -40,6 +48,10 @@ export interface VisitInfo {
4048
}
4149

4250
export 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

5672
export function visitNode(

0 commit comments

Comments
 (0)