11import Maybe from '../tsutils/Maybe' ;
2- import { ASTNode , ASTKindToNode } from './ast' ;
32import { TypeInfo } from '../utilities/TypeInfo' ;
3+ import { ASTNode , ASTKindToNode } from './ast' ;
4+
5+ /**
6+ * A visitor is provided to visit, it contains the collection of
7+ * relevant functions to be called during the visitor's traversal.
8+ */
9+ export type ASTVisitor = Visitor < ASTKindToNode > ;
10+ export type Visitor < KindToNode , Nodes = KindToNode [ keyof KindToNode ] > =
11+ | EnterLeaveVisitor < KindToNode , Nodes >
12+ | ShapeMapVisitor < KindToNode , Nodes > ;
413
514interface EnterLeave < T > {
615 readonly enter ?: T ;
@@ -17,27 +26,23 @@ type ShapeMapVisitor<KindToNode, Nodes> = {
1726 | EnterLeave < VisitFn < Nodes , KindToNode [ K ] > > ;
1827} ;
1928
20- export type ASTVisitor = Visitor < ASTKindToNode > ;
21- export type Visitor < KindToNode , Nodes = KindToNode [ keyof KindToNode ] > =
22- | EnterLeaveVisitor < KindToNode , Nodes >
23- | ShapeMapVisitor < KindToNode , Nodes > ;
24-
2529/**
2630 * A visitor is comprised of visit functions, which are called on each node
2731 * during the visitor's traversal.
2832 */
2933export type VisitFn < TAnyNode , TVisitedNode = TAnyNode > = (
30- // The current node being visiting.
34+ /** The current node being visiting.*/
3135 node : TVisitedNode ,
32- // The index or key to this node from the parent node or Array.
36+ /** The index or key to this node from the parent node or Array. */
3337 key : string | number | undefined ,
34- // The parent immediately above this node, which may be an Array.
38+ /** The parent immediately above this node, which may be an Array. */
3539 parent : TAnyNode | ReadonlyArray < TAnyNode > | undefined ,
36- // The key path to get to this node from the root node.
40+ /** The key path to get to this node from the root node. */
3741 path : ReadonlyArray < string | number > ,
38- // All nodes and Arrays visited before reaching parent of this node.
39- // These correspond to array indices in `path`.
40- // Note: ancestors includes arrays which contain the parent of visited node.
42+ /** All nodes and Arrays visited before reaching parent of this node.
43+ * These correspond to array indices in `path`.
44+ * Note: ancestors includes arrays which contain the parent of visited node.
45+ */
4146 ancestors : ReadonlyArray < TAnyNode | ReadonlyArray < TAnyNode > > ,
4247) => any ;
4348
@@ -46,9 +51,96 @@ export type VisitFn<TAnyNode, TVisitedNode = TAnyNode> = (
4651 */
4752export type VisitorKeyMap < T > = { [ P in keyof T ] : ReadonlyArray < keyof T [ P ] > } ;
4853
49- export const QueryDocumentKeys : { [ key : string ] : string [ ] } ;
54+ // TODO: Should be `[]`, but that requires TypeScript@3
55+ type EmptyTuple = never [ ] ;
56+
57+ export const QueryDocumentKeys : {
58+ Name : EmptyTuple ;
59+
60+ Document : [ 'definitions' ] ;
61+ // Prettier forces trailing commas, but TS pre 3.2 doesn't allow them.
62+ // prettier-ignore
63+ OperationDefinition : [
64+ 'name' ,
65+ 'variableDefinitions' ,
66+ 'directives' ,
67+ 'selectionSet'
68+ ] ;
69+ VariableDefinition : [ 'variable' , 'type' , 'defaultValue' , 'directives' ] ;
70+ Variable : [ 'name' ] ;
71+ SelectionSet : [ 'selections' ] ;
72+ Field : [ 'alias' , 'name' , 'arguments' , 'directives' , 'selectionSet' ] ;
73+ Argument : [ 'name' , 'value' ] ;
74+
75+ FragmentSpread : [ 'name' , 'directives' ] ;
76+ InlineFragment : [ 'typeCondition' , 'directives' , 'selectionSet' ] ;
77+ // prettier-ignore
78+ FragmentDefinition : [
79+ 'name' ,
80+ // Note: fragment variable definitions are experimental and may be changed
81+ // or removed in the future.
82+ 'variableDefinitions' ,
83+ 'typeCondition' ,
84+ 'directives' ,
85+ 'selectionSet'
86+ ] ;
87+
88+ IntValue : EmptyTuple ;
89+ FloatValue : EmptyTuple ;
90+ StringValue : EmptyTuple ;
91+ BooleanValue : EmptyTuple ;
92+ NullValue : EmptyTuple ;
93+ EnumValue : EmptyTuple ;
94+ ListValue : [ 'values' ] ;
95+ ObjectValue : [ 'fields' ] ;
96+ ObjectField : [ 'name' , 'value' ] ;
97+
98+ Directive : [ 'name' , 'arguments' ] ;
99+
100+ NamedType : [ 'name' ] ;
101+ ListType : [ 'type' ] ;
102+ NonNullType : [ 'type' ] ;
103+
104+ SchemaDefinition : [ 'directives' , 'operationTypes' ] ;
105+ OperationTypeDefinition : [ 'type' ] ;
106+
107+ ScalarTypeDefinition : [ 'description' , 'name' , 'directives' ] ;
108+ // prettier-ignore
109+ ObjectTypeDefinition : [
110+ 'description' ,
111+ 'name' ,
112+ 'interfaces' ,
113+ 'directives' ,
114+ 'fields'
115+ ] ;
116+ FieldDefinition : [ 'description' , 'name' , 'arguments' , 'type' , 'directives' ] ;
117+ // prettier-ignore
118+ InputValueDefinition : [
119+ 'description' ,
120+ 'name' ,
121+ 'type' ,
122+ 'defaultValue' ,
123+ 'directives'
124+ ] ;
125+ InterfaceTypeDefinition : [ 'description' , 'name' , 'directives' , 'fields' ] ;
126+ UnionTypeDefinition : [ 'description' , 'name' , 'directives' , 'types' ] ;
127+ EnumTypeDefinition : [ 'description' , 'name' , 'directives' , 'values' ] ;
128+ EnumValueDefinition : [ 'description' , 'name' , 'directives' ] ;
129+ InputObjectTypeDefinition : [ 'description' , 'name' , 'directives' , 'fields' ] ;
130+
131+ DirectiveDefinition : [ 'description' , 'name' , 'arguments' , 'locations' ] ;
132+
133+ SchemaExtension : [ 'directives' , 'operationTypes' ] ;
134+
135+ ScalarTypeExtension : [ 'name' , 'directives' ] ;
136+ ObjectTypeExtension : [ 'name' , 'interfaces' , 'directives' , 'fields' ] ;
137+ InterfaceTypeExtension : [ 'name' , 'directives' , 'fields' ] ;
138+ UnionTypeExtension : [ 'name' , 'directives' , 'types' ] ;
139+ EnumTypeExtension : [ 'name' , 'directives' , 'values' ] ;
140+ InputObjectTypeExtension : [ 'name' , 'directives' , 'fields' ] ;
141+ } ;
50142
51- export const BREAK : any ;
143+ export const BREAK : { } ;
52144
53145/**
54146 * visit() will walk through an AST using a depth first traversal, calling
@@ -149,7 +241,7 @@ export function visit(
149241 * If a prior visitor edits a node, no following visitors will see that node.
150242 */
151243export function visitInParallel (
152- visitors : Array < Visitor < ASTKindToNode > > ,
244+ visitors : ReadonlyArray < Visitor < ASTKindToNode > > ,
153245) : Visitor < ASTKindToNode > ;
154246
155247/**
0 commit comments