diff --git a/src/index.d.ts b/src/index.d.ts index 7b3964b050..1b518ee268 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -227,8 +227,7 @@ export { DirectiveLocationEnum, // Visitor utilities ASTVisitor, - Visitor, - VisitFn, + ASTVisitFn, // AST nodes ASTNode, ASTKindToNode, diff --git a/src/index.js b/src/index.js index 01b9576235..5d3be4b8b0 100644 --- a/src/index.js +++ b/src/index.js @@ -214,8 +214,7 @@ export type { DirectiveLocationEnum, // Visitor utilities ASTVisitor, - Visitor, - VisitFn, + ASTVisitFn, // AST nodes ASTNode, ASTKindToNode, diff --git a/src/language/index.d.ts b/src/language/index.d.ts index b527e4fad6..a5b1157b24 100644 --- a/src/language/index.d.ts +++ b/src/language/index.d.ts @@ -14,8 +14,7 @@ export { getVisitFn, BREAK, ASTVisitor, - Visitor, - VisitFn, + ASTVisitFn, } from './visitor'; export { diff --git a/src/language/index.js b/src/language/index.js index 914c26fb42..71cb5f5c22 100644 --- a/src/language/index.js +++ b/src/language/index.js @@ -19,7 +19,7 @@ export type { ParseOptions } from './parser'; export { print } from './printer'; export { visit, visitInParallel, getVisitFn, BREAK } from './visitor'; -export type { ASTVisitor, Visitor, VisitFn } from './visitor'; +export type { ASTVisitor, ASTVisitFn } from './visitor'; export { Location, Token } from './ast'; export type { diff --git a/src/language/visitor.d.ts b/src/language/visitor.d.ts index 4631ef6a07..9ef0a66a4a 100644 --- a/src/language/visitor.d.ts +++ b/src/language/visitor.d.ts @@ -6,37 +6,35 @@ import { ASTNode, ASTKindToNode } from './ast'; * A visitor is provided to visit, it contains the collection of * relevant functions to be called during the visitor's traversal. */ -export type ASTVisitor = Visitor; -export type Visitor = - | EnterLeaveVisitor - | ShapeMapVisitor; +export type ASTVisitor = EnterLeaveVisitor | ShapeMapVisitor; interface EnterLeave { readonly enter?: T; readonly leave?: T; } -type EnterLeaveVisitor = EnterLeave< - VisitFn | { [K in keyof KindToNode]?: VisitFn } +type EnterLeaveVisitor = EnterLeave< + | ASTVisitFn + | { [K in keyof ASTKindToNode]?: ASTVisitFn } >; -type ShapeMapVisitor = { - [K in keyof KindToNode]?: - | VisitFn - | EnterLeave>; +type ShapeMapVisitor = { + [K in keyof ASTKindToNode]?: + | ASTVisitFn + | EnterLeave>; }; /** * A visitor is comprised of visit functions, which are called on each node * during the visitor's traversal. */ -export type VisitFn = ( +export type ASTVisitFn = ( /** The current node being visiting. */ node: TVisitedNode, /** 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: TAnyNode | ReadonlyArray | undefined, + parent: ASTNode | ReadonlyArray | undefined, /** The key path to get to this node from the root node. */ path: ReadonlyArray, /** @@ -44,7 +42,7 @@ export type VisitFn = ( * These correspond to array indices in `path`. * Note: ancestors includes arrays which contain the parent of visited node. */ - ancestors: ReadonlyArray>, + ancestors: ReadonlyArray>, ) => any; export const BREAK: any; @@ -135,7 +133,7 @@ export const BREAK: any; * } * }) */ -export function visit(root: ASTNode, visitor: Visitor): any; +export function visit(root: ASTNode, visitor: ASTVisitor): any; /** * Creates a new visitor instance which delegates to many visitors to run in @@ -144,15 +142,15 @@ export function visit(root: ASTNode, visitor: Visitor): any; * If a prior visitor edits a node, no following visitors will see that node. */ export function visitInParallel( - visitors: ReadonlyArray>, -): Visitor; + visitors: ReadonlyArray, +): ASTVisitor; /** * Given a visitor instance, if it is leaving or not, and a node kind, return * the function the visitor runtime should call. */ export function getVisitFn( - visitor: Visitor, + visitor: ASTVisitor, kind: string, isLeaving: boolean, -): Maybe>; +): Maybe>; diff --git a/src/language/visitor.js b/src/language/visitor.js index c118131cd8..dd3359ace5 100644 --- a/src/language/visitor.js +++ b/src/language/visitor.js @@ -7,15 +7,14 @@ import { isNode } from './ast'; * A visitor is provided to visit, it contains the collection of * relevant functions to be called during the visitor's traversal. */ -export type ASTVisitor = Visitor; -export type Visitor> = +export type ASTVisitor = | EnterLeave< - | VisitFn - | ShapeMap(Node) => VisitFn>, + | ASTVisitFn + | ShapeMap(Node) => ASTVisitFn>, > | ShapeMap< - KindToNode, - (Node) => VisitFn | EnterLeave>, + ASTKindToNode, + (Node) => ASTVisitFn | EnterLeave>, >; type EnterLeave = {| +enter?: T, +leave?: T |}; type ShapeMap = $Shape<$ObjMap>; @@ -24,19 +23,19 @@ type ShapeMap = $Shape<$ObjMap>; * A visitor is comprised of visit functions, which are called on each node * during the visitor's traversal. */ -export type VisitFn = ( +export type ASTVisitFn = ( // The current node being visiting. node: TVisitedNode, // The index or key to this node from the parent node or Array. key: string | number | void, // The parent immediately above this node, which may be an Array. - parent: TAnyNode | $ReadOnlyArray | void, + parent: ASTNode | $ReadOnlyArray | void, // The key path to get to this node from the root node. path: $ReadOnlyArray, // 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>, + ancestors: $ReadOnlyArray>, ) => any; const QueryDocumentKeys = { @@ -213,7 +212,7 @@ export const BREAK: { ... } = Object.freeze({}); * } * }) */ -export function visit(root: ASTNode, visitor: Visitor): any { +export function visit(root: ASTNode, visitor: ASTVisitor): any { /* eslint-disable no-undef-init */ let stack: any = undefined; let inArray = Array.isArray(root); @@ -284,6 +283,7 @@ export function visit(root: ASTNode, visitor: Visitor): any { } const visitFn = getVisitFn(visitor, node.kind, isLeaving); if (visitFn) { + // $FlowFixMe[incompatible-call] result = visitFn.call(visitor, node, key, parent, path, ancestors); if (result === BREAK) { @@ -342,8 +342,8 @@ export function visit(root: ASTNode, visitor: Visitor): any { * If a prior visitor edits a node, no following visitors will see that node. */ export function visitInParallel( - visitors: $ReadOnlyArray>, -): Visitor { + visitors: $ReadOnlyArray, +): ASTVisitor { const skipping = new Array(visitors.length); return { @@ -389,10 +389,10 @@ export function visitInParallel( * the function the visitor runtime should call. */ export function getVisitFn( - visitor: Visitor, + visitor: ASTVisitor, kind: string, isLeaving: boolean, -): ?VisitFn { +): ?ASTVisitFn { const kindVisitor = visitor[kind]; if (kindVisitor) { if (!isLeaving && typeof kindVisitor === 'function') { @@ -407,6 +407,7 @@ export function getVisitFn( return kindSpecificVisitor; } } else { + // $FlowFixMe[prop-missing] const specificVisitor = isLeaving ? visitor.leave : visitor.enter; if (specificVisitor) { if (typeof specificVisitor === 'function') { diff --git a/src/utilities/TypeInfo.d.ts b/src/utilities/TypeInfo.d.ts index 9d26943d0d..e1a45f24c5 100644 --- a/src/utilities/TypeInfo.d.ts +++ b/src/utilities/TypeInfo.d.ts @@ -1,7 +1,7 @@ import { Maybe } from '../jsutils/Maybe'; -import { Visitor } from '../language/visitor'; -import { ASTNode, ASTKindToNode, FieldNode } from '../language/ast'; +import { ASTVisitor } from '../language/visitor'; +import { ASTNode, FieldNode } from '../language/ast'; import { GraphQLSchema } from '../type/schema'; import { GraphQLDirective } from '../type/directives'; import { @@ -55,5 +55,5 @@ type getFieldDef = ( */ export function visitWithTypeInfo( typeInfo: TypeInfo, - visitor: Visitor, -): Visitor; + visitor: ASTVisitor, +): ASTVisitor; diff --git a/src/utilities/TypeInfo.js b/src/utilities/TypeInfo.js index 805dd7df10..10225d1754 100644 --- a/src/utilities/TypeInfo.js +++ b/src/utilities/TypeInfo.js @@ -1,5 +1,5 @@ -import type { Visitor } from '../language/visitor'; -import type { ASTNode, ASTKindToNode, FieldNode } from '../language/ast'; +import type { ASTVisitor } from '../language/visitor'; +import type { ASTNode, FieldNode } from '../language/ast'; import { Kind } from '../language/kinds'; import { isNode } from '../language/ast'; import { getVisitFn } from '../language/visitor'; @@ -324,8 +324,8 @@ function getFieldDef( */ export function visitWithTypeInfo( typeInfo: TypeInfo, - visitor: Visitor, -): Visitor { + visitor: ASTVisitor, +): ASTVisitor { return { enter(node) { typeInfo.enter(node);