Skip to content

Commit b3762f6

Browse files
authored
use named parameters for node visit context info
1 parent 338e3dd commit b3762f6

File tree

5 files changed

+29
-36
lines changed

5 files changed

+29
-36
lines changed

docs-old/APIReference-Language.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@ visit function.
215215

216216
```js
217217
var editedAST = visit(ast, {
218-
enter(node, key, parent, path, ancestors) {
218+
enter(node, { key, parent, path, ancestors }) {
219219
// @return
220220
// undefined: no action
221221
// false: skip visiting this node
222222
// visitor.BREAK: stop visiting altogether
223223
// null: delete this node
224224
// any value: replace this node with the returned value
225225
},
226-
leave(node, key, parent, path, ancestors) {
226+
leave(node, { key, parent, path, ancestors }) {
227227
// @return
228228
// undefined: no action
229229
// false: no action

src/language/__tests__/visitor-test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ describe('Visitor', () => {
6767
const ast = parse('{ a }', { noLocation: true });
6868

6969
visit(ast, {
70-
enter(_node, _key, _parent, path) {
70+
enter(_node, { path }) {
7171
checkVisitorFnArgs(ast, arguments);
7272
visited.push(['enter', path.slice()]);
7373
},
74-
leave(_node, _key, _parent, path) {
74+
leave(_node, { path }) {
7575
checkVisitorFnArgs(ast, arguments);
7676
visited.push(['leave', path.slice()]);
7777
},
@@ -96,7 +96,7 @@ describe('Visitor', () => {
9696
const visitedNodes: Array<any> = [];
9797

9898
visit(ast, {
99-
enter(node, key, parent, _path, ancestors) {
99+
enter(node, { key, parent, ancestors }) {
100100
const inArray = typeof key === 'number';
101101
if (inArray) {
102102
visitedNodes.push(parent);
@@ -106,7 +106,7 @@ describe('Visitor', () => {
106106
const expectedAncestors = visitedNodes.slice(0, -2);
107107
expect(ancestors).to.deep.equal(expectedAncestors);
108108
},
109-
leave(_node, key, _parent, _path, ancestors) {
109+
leave(_node, { key, ancestors }) {
110110
const expectedAncestors = visitedNodes.slice(0, -2);
111111
expect(ancestors).to.deep.equal(expectedAncestors);
112112

@@ -511,7 +511,7 @@ describe('Visitor', () => {
511511
const argsStack: Array<any> = [];
512512

513513
visit(ast, {
514-
enter(node, key, parent) {
514+
enter(node, { key, parent }) {
515515
visited.push([
516516
'enter',
517517
node.kind,
@@ -523,7 +523,7 @@ describe('Visitor', () => {
523523
argsStack.push([...arguments]);
524524
},
525525

526-
leave(node, key, parent) {
526+
leave(node, { key, parent }) {
527527
visited.push([
528528
'leave',
529529
node.kind,

src/language/visitor.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,29 @@ interface EnterLeaveVisitor<TVisitedNode extends ASTNode> {
2222
readonly leave?: ASTVisitFn<TVisitedNode>;
2323
}
2424

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 {
3226
/** The index or key to this node from the parent node or Array. */
33-
key: string | number | undefined,
27+
key: string | number | undefined;
3428
/** The parent immediately above this node, which may be an Array. */
35-
parent: ASTNode | ReadonlyArray<ASTNode> | undefined,
29+
parent: ASTNode | ReadonlyArray<ASTNode> | undefined;
3630
/** The key path to get to this node from the root node. */
37-
path: ReadonlyArray<string | number>,
31+
path: ReadonlyArray<string | number>;
3832
/**
3933
* All nodes and Arrays visited before reaching parent of this node.
4034
* These correspond to array indices in `path`.
4135
* Note: ancestors includes arrays which contain the parent of visited node.
4236
*/
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,
4448
) => any;
4549

4650
/**
@@ -57,18 +61,7 @@ export type ASTReducer<R> = {
5761
type ASTReducerFn<TReducedNode extends ASTNode, R> = (
5862
/** The current node being visiting. */
5963
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,
7265
) => R;
7366

7467
type ReducedField<T, R> = T extends null | undefined
@@ -102,15 +95,15 @@ export const BREAK: unknown = Object.freeze({});
10295
*
10396
* ```ts
10497
* const editedAST = visit(ast, {
105-
* enter(node, key, parent, path, ancestors) {
98+
* enter(node, { key, parent, path, ancestors }) {
10699
* // @return
107100
* // undefined: no action
108101
* // false: skip visiting this node
109102
* // visitor.BREAK: stop visiting altogether
110103
* // null: delete this node
111104
* // any value: replace this node with the returned value
112105
* },
113-
* leave(node, key, parent, path, ancestors) {
106+
* leave(node, { key, parent, path, ancestors }) {
114107
* // @return
115108
* // undefined: no action
116109
* // false: no action
@@ -251,7 +244,7 @@ export function visit(
251244
? enterLeaveMap.get(node.kind)?.leave
252245
: enterLeaveMap.get(node.kind)?.enter;
253246

254-
result = visitFn?.call(visitor, node, key, parent, path, ancestors);
247+
result = visitFn?.call(visitor, node, { key, parent, path, ancestors });
255248

256249
if (result === BREAK) {
257250
break;

src/validation/rules/KnownDirectivesRule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function KnownDirectivesRule(
4545
}
4646

4747
return {
48-
Directive(node, _key, _parent, _path, ancestors) {
48+
Directive(node, { ancestors }) {
4949
const name = node.name.value;
5050
const locations = locationsMap[name];
5151

src/validation/rules/KnownTypeNamesRule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function KnownTypeNamesRule(
4646
];
4747

4848
return {
49-
NamedType(node, _1, parent, _2, ancestors) {
49+
NamedType(node, { parent, ancestors }) {
5050
const typeName = node.name.value;
5151
if (!existingTypesMap[typeName] && !definedTypes[typeName]) {
5252
const definitionNode = ancestors[2] ?? parent;

0 commit comments

Comments
 (0)