Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use named parameters for node visit context info #3619

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs-old/APIReference-Language.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ visit function.

```js
var editedAST = visit(ast, {
enter(node, key, parent, path, ancestors) {
enter(node, { key, parent, path, ancestors }) {
// @return
// undefined: no action
// false: skip visiting this node
// visitor.BREAK: stop visiting altogether
// null: delete this node
// any value: replace this node with the returned value
},
leave(node, key, parent, path, ancestors) {
leave(node, { key, parent, path, ancestors }) {
// @return
// undefined: no action
// false: no action
Expand Down
14 changes: 7 additions & 7 deletions src/language/__tests__/visitor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ASTVisitor, ASTVisitorKeyMap } from '../visitor';
import { BREAK, visit, visitInParallel } from '../visitor';

function checkVisitorFnArgs(ast: any, args: any, isEdited: boolean = false) {
const [node, key, parent, path, ancestors] = args;
const [node, { key, parent, path, ancestors }] = args;

expect(node).to.be.an.instanceof(Object);
expect(node.kind).to.be.oneOf(Object.values(Kind));
Expand Down Expand Up @@ -67,11 +67,11 @@ describe('Visitor', () => {
const ast = parse('{ a }', { noLocation: true });

visit(ast, {
enter(_node, _key, _parent, path) {
enter(_node, { path }) {
checkVisitorFnArgs(ast, arguments);
visited.push(['enter', path.slice()]);
},
leave(_node, _key, _parent, path) {
leave(_node, { path }) {
checkVisitorFnArgs(ast, arguments);
visited.push(['leave', path.slice()]);
},
Expand All @@ -96,7 +96,7 @@ describe('Visitor', () => {
const visitedNodes: Array<any> = [];

visit(ast, {
enter(node, key, parent, _path, ancestors) {
enter(node, { key, parent, ancestors }) {
const inArray = typeof key === 'number';
if (inArray) {
visitedNodes.push(parent);
Expand All @@ -106,7 +106,7 @@ describe('Visitor', () => {
const expectedAncestors = visitedNodes.slice(0, -2);
expect(ancestors).to.deep.equal(expectedAncestors);
},
leave(_node, key, _parent, _path, ancestors) {
leave(_node, { key, ancestors }) {
const expectedAncestors = visitedNodes.slice(0, -2);
expect(ancestors).to.deep.equal(expectedAncestors);

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

visit(ast, {
enter(node, key, parent) {
enter(node, { key, parent }) {
visited.push([
'enter',
node.kind,
Expand All @@ -523,7 +523,7 @@ describe('Visitor', () => {
argsStack.push([...arguments]);
},

leave(node, key, parent) {
leave(node, { key, parent }) {
visited.push([
'leave',
node.kind,
Expand Down
45 changes: 19 additions & 26 deletions src/language/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,29 @@ interface EnterLeaveVisitor<TVisitedNode extends ASTNode> {
readonly leave?: ASTVisitFn<TVisitedNode>;
}

/**
* A visitor is comprised of visit functions, which are called on each node
* during the visitor's traversal.
*/
export type ASTVisitFn<TVisitedNode extends ASTNode> = (
/** The current node being visiting. */
node: TVisitedNode,
interface VisitContext {
/** The index or key to this node from the parent node or Array. */
key: string | number | undefined,
key: string | number | undefined;
/** The parent immediately above this node, which may be an Array. */
parent: ASTNode | ReadonlyArray<ASTNode> | undefined,
parent: ASTNode | ReadonlyArray<ASTNode> | undefined;
/** The key path to get to this node from the root node. */
path: ReadonlyArray<string | number>,
path: ReadonlyArray<string | number>;
/**
* All nodes and Arrays visited before reaching parent of this node.
* These correspond to array indices in `path`.
* Note: ancestors includes arrays which contain the parent of visited node.
*/
ancestors: ReadonlyArray<ASTNode | ReadonlyArray<ASTNode>>,
ancestors: ReadonlyArray<ASTNode | ReadonlyArray<ASTNode>>;
}

/**
* A visitor is comprised of visit functions, which are called on each node
* during the visitor's traversal.
*/
export type ASTVisitFn<TVisitedNode extends ASTNode> = (
/** The current node being visiting. */
node: TVisitedNode,
context: VisitContext,
) => any;

/**
Expand All @@ -57,18 +61,7 @@ export type ASTReducer<R> = {
type ASTReducerFn<TReducedNode extends ASTNode, R> = (
/** The current node being visiting. */
node: { [K in keyof TReducedNode]: ReducedField<TReducedNode[K], R> },
/** The index or key to this node from the parent node or Array. */
key: string | number | undefined,
/** The parent immediately above this node, which may be an Array. */
parent: ASTNode | ReadonlyArray<ASTNode> | undefined,
/** The key path to get to this node from the root node. */
path: ReadonlyArray<string | number>,
/**
* All nodes and Arrays visited before reaching parent of this node.
* These correspond to array indices in `path`.
* Note: ancestors includes arrays which contain the parent of visited node.
*/
ancestors: ReadonlyArray<ASTNode | ReadonlyArray<ASTNode>>,
context: VisitContext,
) => R;

type ReducedField<T, R> = T extends null | undefined
Expand Down Expand Up @@ -102,15 +95,15 @@ export const BREAK: unknown = Object.freeze({});
*
* ```ts
* const editedAST = visit(ast, {
* enter(node, key, parent, path, ancestors) {
* enter(node, { key, parent, path, ancestors }) {
* // @return
* // undefined: no action
* // false: skip visiting this node
* // visitor.BREAK: stop visiting altogether
* // null: delete this node
* // any value: replace this node with the returned value
* },
* leave(node, key, parent, path, ancestors) {
* leave(node, { key, parent, path, ancestors }) {
* // @return
* // undefined: no action
* // false: no action
Expand Down Expand Up @@ -251,7 +244,7 @@ export function visit(
? enterLeaveMap.get(node.kind)?.leave
: enterLeaveMap.get(node.kind)?.enter;

result = visitFn?.call(visitor, node, key, parent, path, ancestors);
result = visitFn?.call(visitor, node, { key, parent, path, ancestors });

if (result === BREAK) {
break;
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/KnownDirectivesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function KnownDirectivesRule(
}

return {
Directive(node, _key, _parent, _path, ancestors) {
Directive(node, { ancestors }) {
const name = node.name.value;
const locations = locationsMap[name];

Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/KnownTypeNamesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function KnownTypeNamesRule(
];

return {
NamedType(node, _1, parent, _2, ancestors) {
NamedType(node, { parent, ancestors }) {
const typeName = node.name.value;
if (!existingTypesMap[typeName] && !definedTypes[typeName]) {
const definitionNode = ancestors[2] ?? parent;
Expand Down