@@ -22,25 +22,29 @@ interface EnterLeaveVisitor<TVisitedNode extends ASTNode> {
22
22
readonly leave ?: ASTVisitFn < TVisitedNode > ;
23
23
}
24
24
25
- /**
26
- * A visitor is comprised of visit functions, which are called on each node
27
- * during the visitor's traversal.
28
- */
29
- export type ASTVisitFn < TVisitedNode extends ASTNode > = (
30
- /** The current node being visiting. */
31
- node : TVisitedNode ,
25
+ interface VisitContext {
32
26
/** The index or key to this node from the parent node or Array. */
33
- key : string | number | undefined ,
27
+ key : string | number | undefined ;
34
28
/** The parent immediately above this node, which may be an Array. */
35
- parent : ASTNode | ReadonlyArray < ASTNode > | undefined ,
29
+ parent : ASTNode | ReadonlyArray < ASTNode > | undefined ;
36
30
/** The key path to get to this node from the root node. */
37
- path : ReadonlyArray < string | number > ,
31
+ path : ReadonlyArray < string | number > ;
38
32
/**
39
33
* All nodes and Arrays visited before reaching parent of this node.
40
34
* These correspond to array indices in `path`.
41
35
* Note: ancestors includes arrays which contain the parent of visited node.
42
36
*/
43
- ancestors : ReadonlyArray < ASTNode | ReadonlyArray < ASTNode > > ,
37
+ ancestors : ReadonlyArray < ASTNode | ReadonlyArray < ASTNode > > ;
38
+ }
39
+
40
+ /**
41
+ * A visitor is comprised of visit functions, which are called on each node
42
+ * during the visitor's traversal.
43
+ */
44
+ export type ASTVisitFn < TVisitedNode extends ASTNode > = (
45
+ /** The current node being visiting. */
46
+ node : TVisitedNode ,
47
+ context : VisitContext ,
44
48
) => any ;
45
49
46
50
/**
@@ -57,18 +61,7 @@ export type ASTReducer<R> = {
57
61
type ASTReducerFn < TReducedNode extends ASTNode , R > = (
58
62
/** The current node being visiting. */
59
63
node : { [ K in keyof TReducedNode ] : ReducedField < TReducedNode [ K ] , R > } ,
60
- /** The index or key to this node from the parent node or Array. */
61
- key : string | number | undefined ,
62
- /** The parent immediately above this node, which may be an Array. */
63
- parent : ASTNode | ReadonlyArray < ASTNode > | undefined ,
64
- /** The key path to get to this node from the root node. */
65
- path : ReadonlyArray < string | number > ,
66
- /**
67
- * All nodes and Arrays visited before reaching parent of this node.
68
- * These correspond to array indices in `path`.
69
- * Note: ancestors includes arrays which contain the parent of visited node.
70
- */
71
- ancestors : ReadonlyArray < ASTNode | ReadonlyArray < ASTNode > > ,
64
+ context : VisitContext ,
72
65
) => R ;
73
66
74
67
type ReducedField < T , R > = T extends null | undefined
@@ -102,15 +95,15 @@ export const BREAK: unknown = Object.freeze({});
102
95
*
103
96
* ```ts
104
97
* const editedAST = visit(ast, {
105
- * enter(node, key, parent, path, ancestors) {
98
+ * enter(node, { key, parent, path, ancestors } ) {
106
99
* // @return
107
100
* // undefined: no action
108
101
* // false: skip visiting this node
109
102
* // visitor.BREAK: stop visiting altogether
110
103
* // null: delete this node
111
104
* // any value: replace this node with the returned value
112
105
* },
113
- * leave(node, key, parent, path, ancestors) {
106
+ * leave(node, { key, parent, path, ancestors } ) {
114
107
* // @return
115
108
* // undefined: no action
116
109
* // false: no action
@@ -251,7 +244,7 @@ export function visit(
251
244
? enterLeaveMap . get ( node . kind ) ?. leave
252
245
: enterLeaveMap . get ( node . kind ) ?. enter ;
253
246
254
- result = visitFn ?. call ( visitor , node , key , parent , path , ancestors ) ;
247
+ result = visitFn ?. call ( visitor , node , { key, parent, path, ancestors } ) ;
255
248
256
249
if ( result === BREAK ) {
257
250
break ;
0 commit comments