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

Bring visitorKeys back #3250

Merged
merged 3 commits into from Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -234,6 +234,7 @@ export type {
/** Visitor utilities */
ASTVisitor,
ASTVisitFn,
VisitorKeyMap,
/** AST nodes */
ASTNode,
ASTKindToNode,
Expand Down
38 changes: 37 additions & 1 deletion src/language/__tests__/visitor-test.ts
Expand Up @@ -3,10 +3,11 @@ import { describe, it } from 'mocha';

import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery';

import type { ASTNode, SelectionSetNode } from '../ast';
import type { ASTKindToNode, ASTNode, SelectionSetNode } from '../ast';
import { isNode } from '../ast';
import { Kind } from '../kinds';
import { parse } from '../parser';
import type { VisitorKeyMap, ASTVisitor } from '../visitor';
import { visit, visitInParallel, BREAK } from '../visitor';

function checkVisitorFnArgs(ast: any, args: any, isEdited: boolean = false) {
Expand Down Expand Up @@ -893,6 +894,41 @@ describe('Visitor', () => {
]);
});

it('visits only the specified `Kind` in visitorKeyMap', () => {
const visited: Array<[string, string, ReturnType<typeof getValue>]> = [];
IvanGoncharov marked this conversation as resolved.
Show resolved Hide resolved

const visitorKeyMap: VisitorKeyMap<ASTKindToNode> = {
Document: ['definitions'],
OperationDefinition: ['name'],
};

const visitor: ASTVisitor = {
enter(node) {
visited.push(['enter', node.kind, getValue(node)]);
},
leave(node) {
visited.push(['leave', node.kind, getValue(node)]);
},
};

const exampleDocumentAST = parse(/* GraphQL */ `
IvanGoncharov marked this conversation as resolved.
Show resolved Hide resolved
query ExampleOperation {
someField
}
`);

visit(exampleDocumentAST, visitor, visitorKeyMap);

expect(visited).to.deep.equal([
['enter', Kind.DOCUMENT, undefined],
IvanGoncharov marked this conversation as resolved.
Show resolved Hide resolved
['enter', Kind.OPERATION_DEFINITION, undefined],
['enter', Kind.NAME, 'ExampleOperation'],
['leave', Kind.NAME, 'ExampleOperation'],
['leave', Kind.OPERATION_DEFINITION, undefined],
['leave', Kind.DOCUMENT, undefined],
]);
});

describe('visitInParallel', () => {
// Note: nearly identical to the above test of the same test but
// using visitInParallel.
Expand Down
2 changes: 1 addition & 1 deletion src/language/index.ts
Expand Up @@ -19,7 +19,7 @@ export type { ParseOptions } from './parser';
export { print } from './printer';

export { visit, visitInParallel, getVisitFn, BREAK } from './visitor';
export type { ASTVisitor, ASTVisitFn } from './visitor';
export type { ASTVisitor, ASTVisitFn, VisitorKeyMap } from './visitor';

export { Location, Token } from './ast';
export type {
Expand Down
30 changes: 19 additions & 11 deletions src/language/visitor.ts
Expand Up @@ -76,9 +76,14 @@ type ReducedField<T, R> = T extends null | undefined
? ReadonlyArray<R>
: R;

const QueryDocumentKeys = {
Name: [],
/**
* A KeyMap describes each the traversable properties of each kind of node.
*/
export type VisitorKeyMap<KindToNode> = {
IvanGoncharov marked this conversation as resolved.
Show resolved Hide resolved
[P in keyof KindToNode]?: ReadonlyArray<keyof KindToNode[P]>;
};

export const QueryDocumentKeys: VisitorKeyMap<ASTKindToNode> = {
Document: ['definitions'],
OperationDefinition: [
'name',
Expand All @@ -103,12 +108,6 @@ const QueryDocumentKeys = {
'selectionSet',
],

IntValue: [],
FloatValue: [],
StringValue: [],
BooleanValue: [],
NullValue: [],
EnumValue: [],
ListValue: ['values'],
ObjectValue: ['fields'],
ObjectField: ['name', 'value'],
Expand Down Expand Up @@ -242,11 +241,20 @@ export const BREAK: unknown = Object.freeze({});
* })
* ```
*/
export function visit<N extends ASTNode>(root: N, visitor: ASTVisitor): N;
export function visit<R>(root: ASTNode, visitor: ASTReducer<R>): R;
export function visit<N extends ASTNode>(
root: N,
visitor: ASTVisitor,
visitorKeys?: VisitorKeyMap<ASTKindToNode>,
): N;
export function visit<R>(
root: ASTNode,
visitor: ASTReducer<R>,
visitorKeys?: VisitorKeyMap<ASTKindToNode>,
): R;
export function visit(
root: ASTNode,
visitor: ASTVisitor | ASTReducer<any>,
visitorKeys: VisitorKeyMap<ASTKindToNode> = QueryDocumentKeys,
): any {
/* eslint-disable no-undef-init */
let stack: any = undefined;
Expand Down Expand Up @@ -346,7 +354,7 @@ export function visit(
} else {
stack = { inArray, index, keys, edits, prev: stack };
inArray = Array.isArray(node);
keys = inArray ? node : (QueryDocumentKeys as any)[node.kind] ?? [];
keys = inArray ? node : (visitorKeys as any)[node.kind] ?? [];
index = -1;
edits = [];
if (parent) {
Expand Down