Skip to content

Commit 090ba36

Browse files
visit: remove template arguments for possible nodes (#2931)
1 parent b67ed17 commit 090ba36

File tree

8 files changed

+43
-47
lines changed

8 files changed

+43
-47
lines changed

src/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ export {
227227
DirectiveLocationEnum,
228228
// Visitor utilities
229229
ASTVisitor,
230-
Visitor,
231-
VisitFn,
230+
ASTVisitFn,
232231
// AST nodes
233232
ASTNode,
234233
ASTKindToNode,

src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ export type {
214214
DirectiveLocationEnum,
215215
// Visitor utilities
216216
ASTVisitor,
217-
Visitor,
218-
VisitFn,
217+
ASTVisitFn,
219218
// AST nodes
220219
ASTNode,
221220
ASTKindToNode,

src/language/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ export {
1414
getVisitFn,
1515
BREAK,
1616
ASTVisitor,
17-
Visitor,
18-
VisitFn,
17+
ASTVisitFn,
1918
} from './visitor';
2019

2120
export {

src/language/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type { ParseOptions } from './parser';
1919
export { print } from './printer';
2020

2121
export { visit, visitInParallel, getVisitFn, BREAK } from './visitor';
22-
export type { ASTVisitor, Visitor, VisitFn } from './visitor';
22+
export type { ASTVisitor, ASTVisitFn } from './visitor';
2323

2424
export { Location, Token } from './ast';
2525
export type {

src/language/visitor.d.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,43 @@ import { ASTNode, ASTKindToNode } from './ast';
66
* A visitor is provided to visit, it contains the collection of
77
* relevant functions to be called during the visitor's traversal.
88
*/
9-
export type ASTVisitor = Visitor<ASTKindToNode>;
10-
export type Visitor<KindToNode, Nodes = KindToNode[keyof KindToNode]> =
11-
| EnterLeaveVisitor<KindToNode, Nodes>
12-
| ShapeMapVisitor<KindToNode, Nodes>;
9+
export type ASTVisitor = EnterLeaveVisitor | ShapeMapVisitor;
1310

1411
interface EnterLeave<T> {
1512
readonly enter?: T;
1613
readonly leave?: T;
1714
}
1815

19-
type EnterLeaveVisitor<KindToNode, Nodes> = EnterLeave<
20-
VisitFn<Nodes> | { [K in keyof KindToNode]?: VisitFn<Nodes, KindToNode[K]> }
16+
type EnterLeaveVisitor = EnterLeave<
17+
| ASTVisitFn<ASTNode>
18+
| { [K in keyof ASTKindToNode]?: ASTVisitFn<ASTKindToNode[K]> }
2119
>;
2220

23-
type ShapeMapVisitor<KindToNode, Nodes> = {
24-
[K in keyof KindToNode]?:
25-
| VisitFn<Nodes, KindToNode[K]>
26-
| EnterLeave<VisitFn<Nodes, KindToNode[K]>>;
21+
type ShapeMapVisitor = {
22+
[K in keyof ASTKindToNode]?:
23+
| ASTVisitFn<ASTKindToNode[K]>
24+
| EnterLeave<ASTVisitFn<ASTKindToNode[K]>>;
2725
};
2826

2927
/**
3028
* A visitor is comprised of visit functions, which are called on each node
3129
* during the visitor's traversal.
3230
*/
33-
export type VisitFn<TAnyNode, TVisitedNode = TAnyNode> = (
31+
export type ASTVisitFn<TVisitedNode extends ASTNode> = (
3432
/** The current node being visiting. */
3533
node: TVisitedNode,
3634
/** The index or key to this node from the parent node or Array. */
3735
key: string | number | undefined,
3836
/** The parent immediately above this node, which may be an Array. */
39-
parent: TAnyNode | ReadonlyArray<TAnyNode> | undefined,
37+
parent: ASTNode | ReadonlyArray<ASTNode> | undefined,
4038
/** The key path to get to this node from the root node. */
4139
path: ReadonlyArray<string | number>,
4240
/**
4341
* All nodes and Arrays visited before reaching parent of this node.
4442
* These correspond to array indices in `path`.
4543
* Note: ancestors includes arrays which contain the parent of visited node.
4644
*/
47-
ancestors: ReadonlyArray<TAnyNode | ReadonlyArray<TAnyNode>>,
45+
ancestors: ReadonlyArray<ASTNode | ReadonlyArray<ASTNode>>,
4846
) => any;
4947

5048
export const BREAK: any;
@@ -135,7 +133,7 @@ export const BREAK: any;
135133
* }
136134
* })
137135
*/
138-
export function visit(root: ASTNode, visitor: Visitor<ASTKindToNode>): any;
136+
export function visit(root: ASTNode, visitor: ASTVisitor): any;
139137

140138
/**
141139
* Creates a new visitor instance which delegates to many visitors to run in
@@ -144,15 +142,15 @@ export function visit(root: ASTNode, visitor: Visitor<ASTKindToNode>): any;
144142
* If a prior visitor edits a node, no following visitors will see that node.
145143
*/
146144
export function visitInParallel(
147-
visitors: ReadonlyArray<Visitor<ASTKindToNode>>,
148-
): Visitor<ASTKindToNode>;
145+
visitors: ReadonlyArray<ASTVisitor>,
146+
): ASTVisitor;
149147

150148
/**
151149
* Given a visitor instance, if it is leaving or not, and a node kind, return
152150
* the function the visitor runtime should call.
153151
*/
154152
export function getVisitFn(
155-
visitor: Visitor<any>,
153+
visitor: ASTVisitor,
156154
kind: string,
157155
isLeaving: boolean,
158-
): Maybe<VisitFn<any>>;
156+
): Maybe<ASTVisitFn<ASTNode>>;

src/language/visitor.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import { isNode } from './ast';
77
* A visitor is provided to visit, it contains the collection of
88
* relevant functions to be called during the visitor's traversal.
99
*/
10-
export type ASTVisitor = Visitor<ASTKindToNode>;
11-
export type Visitor<KindToNode, Nodes = $Values<KindToNode>> =
10+
export type ASTVisitor =
1211
| EnterLeave<
13-
| VisitFn<Nodes>
14-
| ShapeMap<KindToNode, <Node>(Node) => VisitFn<Nodes, Node>>,
12+
| ASTVisitFn<ASTNode>
13+
| ShapeMap<ASTKindToNode, <Node>(Node) => ASTVisitFn<Node>>,
1514
>
1615
| ShapeMap<
17-
KindToNode,
18-
<Node>(Node) => VisitFn<Nodes, Node> | EnterLeave<VisitFn<Nodes, Node>>,
16+
ASTKindToNode,
17+
<Node>(Node) => ASTVisitFn<Node> | EnterLeave<ASTVisitFn<Node>>,
1918
>;
2019
type EnterLeave<T> = {| +enter?: T, +leave?: T |};
2120
type ShapeMap<O, F> = $Shape<$ObjMap<O, F>>;
@@ -24,19 +23,19 @@ type ShapeMap<O, F> = $Shape<$ObjMap<O, F>>;
2423
* A visitor is comprised of visit functions, which are called on each node
2524
* during the visitor's traversal.
2625
*/
27-
export type VisitFn<TAnyNode, TVisitedNode: TAnyNode = TAnyNode> = (
26+
export type ASTVisitFn<TVisitedNode: ASTNode> = (
2827
// The current node being visiting.
2928
node: TVisitedNode,
3029
// The index or key to this node from the parent node or Array.
3130
key: string | number | void,
3231
// The parent immediately above this node, which may be an Array.
33-
parent: TAnyNode | $ReadOnlyArray<TAnyNode> | void,
32+
parent: ASTNode | $ReadOnlyArray<ASTNode> | void,
3433
// The key path to get to this node from the root node.
3534
path: $ReadOnlyArray<string | number>,
3635
// All nodes and Arrays visited before reaching parent of this node.
3736
// These correspond to array indices in `path`.
3837
// Note: ancestors includes arrays which contain the parent of visited node.
39-
ancestors: $ReadOnlyArray<TAnyNode | $ReadOnlyArray<TAnyNode>>,
38+
ancestors: $ReadOnlyArray<ASTNode | $ReadOnlyArray<ASTNode>>,
4039
) => any;
4140

4241
const QueryDocumentKeys = {
@@ -213,7 +212,7 @@ export const BREAK: { ... } = Object.freeze({});
213212
* }
214213
* })
215214
*/
216-
export function visit(root: ASTNode, visitor: Visitor<ASTKindToNode>): any {
215+
export function visit(root: ASTNode, visitor: ASTVisitor): any {
217216
/* eslint-disable no-undef-init */
218217
let stack: any = undefined;
219218
let inArray = Array.isArray(root);
@@ -284,6 +283,7 @@ export function visit(root: ASTNode, visitor: Visitor<ASTKindToNode>): any {
284283
}
285284
const visitFn = getVisitFn(visitor, node.kind, isLeaving);
286285
if (visitFn) {
286+
// $FlowFixMe[incompatible-call]
287287
result = visitFn.call(visitor, node, key, parent, path, ancestors);
288288

289289
if (result === BREAK) {
@@ -342,8 +342,8 @@ export function visit(root: ASTNode, visitor: Visitor<ASTKindToNode>): any {
342342
* If a prior visitor edits a node, no following visitors will see that node.
343343
*/
344344
export function visitInParallel(
345-
visitors: $ReadOnlyArray<Visitor<ASTKindToNode>>,
346-
): Visitor<ASTKindToNode> {
345+
visitors: $ReadOnlyArray<ASTVisitor>,
346+
): ASTVisitor {
347347
const skipping = new Array(visitors.length);
348348

349349
return {
@@ -389,10 +389,10 @@ export function visitInParallel(
389389
* the function the visitor runtime should call.
390390
*/
391391
export function getVisitFn(
392-
visitor: Visitor<any>,
392+
visitor: ASTVisitor,
393393
kind: string,
394394
isLeaving: boolean,
395-
): ?VisitFn<any> {
395+
): ?ASTVisitFn<ASTNode> {
396396
const kindVisitor = visitor[kind];
397397
if (kindVisitor) {
398398
if (!isLeaving && typeof kindVisitor === 'function') {
@@ -407,6 +407,7 @@ export function getVisitFn(
407407
return kindSpecificVisitor;
408408
}
409409
} else {
410+
// $FlowFixMe[prop-missing]
410411
const specificVisitor = isLeaving ? visitor.leave : visitor.enter;
411412
if (specificVisitor) {
412413
if (typeof specificVisitor === 'function') {

src/utilities/TypeInfo.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Maybe } from '../jsutils/Maybe';
22

3-
import { Visitor } from '../language/visitor';
4-
import { ASTNode, ASTKindToNode, FieldNode } from '../language/ast';
3+
import { ASTVisitor } from '../language/visitor';
4+
import { ASTNode, FieldNode } from '../language/ast';
55
import { GraphQLSchema } from '../type/schema';
66
import { GraphQLDirective } from '../type/directives';
77
import {
@@ -55,5 +55,5 @@ type getFieldDef = (
5555
*/
5656
export function visitWithTypeInfo(
5757
typeInfo: TypeInfo,
58-
visitor: Visitor<ASTKindToNode>,
59-
): Visitor<ASTKindToNode>;
58+
visitor: ASTVisitor,
59+
): ASTVisitor;

src/utilities/TypeInfo.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Visitor } from '../language/visitor';
2-
import type { ASTNode, ASTKindToNode, FieldNode } from '../language/ast';
1+
import type { ASTVisitor } from '../language/visitor';
2+
import type { ASTNode, FieldNode } from '../language/ast';
33
import { Kind } from '../language/kinds';
44
import { isNode } from '../language/ast';
55
import { getVisitFn } from '../language/visitor';
@@ -324,8 +324,8 @@ function getFieldDef(
324324
*/
325325
export function visitWithTypeInfo(
326326
typeInfo: TypeInfo,
327-
visitor: Visitor<ASTKindToNode>,
328-
): Visitor<ASTKindToNode> {
327+
visitor: ASTVisitor,
328+
): ASTVisitor {
329329
return {
330330
enter(node) {
331331
typeInfo.enter(node);

0 commit comments

Comments
 (0)