Skip to content

Bring visitorKeys back #3250

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

Merged
merged 3 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Review changes
  • Loading branch information
IvanGoncharov committed Sep 20, 2021
commit 48b0738138c87bf99d62469a24cc1aff9bd14a42
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export type {
/** Visitor utilities */
ASTVisitor,
ASTVisitFn,
VisitorKeyMap,
ASTVisitorKeyMap,
/** AST nodes */
ASTNode,
ASTKindToNode,
Expand Down
74 changes: 37 additions & 37 deletions src/language/__tests__/visitor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { describe, it } from 'mocha';

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

import type { ASTKindToNode, ASTNode, SelectionSetNode } from '../ast';
import type { ASTNode, SelectionSetNode } from '../ast';
import type { ASTVisitorKeyMap, ASTVisitor } from '../visitor';
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 @@ -463,6 +463,41 @@ describe('Visitor', () => {
]);
});

it('visits only the specified `Kind` in visitorKeyMap', () => {
const visited: Array<any> = [];

const visitorKeyMap: ASTVisitorKeyMap = {
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(`
query ExampleOperation {
someField
}
`);

visit(exampleDocumentAST, visitor, visitorKeyMap);

expect(visited).to.deep.equal([
['enter', 'Document', undefined],
['enter', 'OperationDefinition', undefined],
['enter', 'Name', 'ExampleOperation'],
['leave', 'Name', 'ExampleOperation'],
['leave', 'OperationDefinition', undefined],
['leave', 'Document', undefined],
]);
});

it('Legacy: visits variables defined in fragments', () => {
const ast = parse('fragment a($v: Boolean = false) on t { f }', {
noLocation: true,
Expand Down Expand Up @@ -894,41 +929,6 @@ describe('Visitor', () => {
]);
});

it('visits only the specified `Kind` in visitorKeyMap', () => {
const visited: Array<[string, string, ReturnType<typeof getValue>]> = [];

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 */ `
query ExampleOperation {
someField
}
`);

visit(exampleDocumentAST, visitor, visitorKeyMap);

expect(visited).to.deep.equal([
['enter', Kind.DOCUMENT, undefined],
['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
Original file line number Diff line number Diff line change
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, VisitorKeyMap } from './visitor';
export type { ASTVisitor, ASTVisitFn, ASTVisitorKeyMap } from './visitor';

export { Location, Token } from './ast';
export type {
Expand Down
12 changes: 6 additions & 6 deletions src/language/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ type ReducedField<T, R> = T extends null | undefined
/**
* A KeyMap describes each the traversable properties of each kind of node.
*/
export type VisitorKeyMap<KindToNode> = {
[P in keyof KindToNode]?: ReadonlyArray<keyof KindToNode[P]>;
export type ASTVisitorKeyMap = {
[P in keyof ASTKindToNode]?: ReadonlyArray<keyof ASTKindToNode[P]>;
};

export const QueryDocumentKeys: VisitorKeyMap<ASTKindToNode> = {
export const QueryDocumentKeys: ASTVisitorKeyMap = {
Document: ['definitions'],
OperationDefinition: [
'name',
Expand Down Expand Up @@ -244,17 +244,17 @@ export const BREAK: unknown = Object.freeze({});
export function visit<N extends ASTNode>(
root: N,
visitor: ASTVisitor,
visitorKeys?: VisitorKeyMap<ASTKindToNode>,
visitorKeys?: ASTVisitorKeyMap,
): N;
export function visit<R>(
root: ASTNode,
visitor: ASTReducer<R>,
visitorKeys?: VisitorKeyMap<ASTKindToNode>,
visitorKeys?: ASTVisitorKeyMap,
): R;
export function visit(
root: ASTNode,
visitor: ASTVisitor | ASTReducer<any>,
visitorKeys: VisitorKeyMap<ASTKindToNode> = QueryDocumentKeys,
visitorKeys: ASTVisitorKeyMap = QueryDocumentKeys,
): any {
/* eslint-disable no-undef-init */
let stack: any = undefined;
Expand Down