Skip to content

Commit 7fae89b

Browse files
committed
Visitor: fix description of "ancestors" arg + test
1 parent 81db410 commit 7fae89b

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/language/__tests__/visitor-test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ describe('Visitor', () => {
7474
checkVisitorFnArgs(ast, arguments);
7575
visited.push(['enter', path.slice()]);
7676
},
77-
7877
leave(node, key, parent, path) {
7978
checkVisitorFnArgs(ast, arguments);
8079
visited.push(['leave', path.slice()]);
@@ -95,6 +94,34 @@ describe('Visitor', () => {
9594
]);
9695
});
9796

97+
it('validates ancestors argument', () => {
98+
const ast = parse('{ a }', { noLocation: true });
99+
const nodesInPath = [];
100+
101+
visit(ast, {
102+
enter(node, key, parent, path, ancestors) {
103+
const inArray = typeof key === 'number';
104+
const expectedAncestors = nodesInPath.slice(0, path.length - 1);
105+
expect(ancestors).to.deep.equal(expectedAncestors);
106+
107+
if (inArray) {
108+
nodesInPath.push(parent);
109+
}
110+
nodesInPath.push(node);
111+
},
112+
leave(node, key, parent, path, ancestors) {
113+
const inArray = typeof key === 'number';
114+
const expectedAncestors = nodesInPath.slice(0, path.length - 1);
115+
expect(ancestors).to.deep.equal(expectedAncestors);
116+
117+
if (inArray) {
118+
nodesInPath.pop();
119+
}
120+
nodesInPath.pop();
121+
},
122+
});
123+
});
124+
98125
it('allows editing a node both on enter and on leave', () => {
99126
const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true });
100127

src/language/visitor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export type VisitFn<TAnyNode, TVisitedNode: TAnyNode = TAnyNode> = (
4040
parent: TAnyNode | $ReadOnlyArray<TAnyNode> | void,
4141
// The key path to get to this node from the root node.
4242
path: $ReadOnlyArray<string | number>,
43-
// All nodes and Arrays visited before reaching this node.
43+
// All nodes and Arrays visited before reaching parent of this node.
4444
// These correspond to array indices in `path`.
45-
// Note: ancestors includes arrays which contain the visited node.
45+
// Note: ancestors includes arrays which contain the parent of visited node.
4646
ancestors: $ReadOnlyArray<TAnyNode | $ReadOnlyArray<TAnyNode>>,
4747
) => any;
4848

0 commit comments

Comments
 (0)