1
1
import Maybe from '../tsutils/Maybe' ;
2
- import { ASTNode , ASTKindToNode } from './ast' ;
3
2
import { 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 > ;
4
13
5
14
interface EnterLeave < T > {
6
15
readonly enter ?: T ;
@@ -17,27 +26,23 @@ type ShapeMapVisitor<KindToNode, Nodes> = {
17
26
| EnterLeave < VisitFn < Nodes , KindToNode [ K ] > > ;
18
27
} ;
19
28
20
- export type ASTVisitor = Visitor < ASTKindToNode > ;
21
- export type Visitor < KindToNode , Nodes = KindToNode [ keyof KindToNode ] > =
22
- | EnterLeaveVisitor < KindToNode , Nodes >
23
- | ShapeMapVisitor < KindToNode , Nodes > ;
24
-
25
29
/**
26
30
* A visitor is comprised of visit functions, which are called on each node
27
31
* during the visitor's traversal.
28
32
*/
29
33
export type VisitFn < TAnyNode , TVisitedNode = TAnyNode > = (
30
- // The current node being visiting.
34
+ /** The current node being visiting.*/
31
35
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. */
33
37
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. */
35
39
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. */
37
41
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
+ */
41
46
ancestors : ReadonlyArray < TAnyNode | ReadonlyArray < TAnyNode > > ,
42
47
) => any ;
43
48
@@ -46,9 +51,93 @@ export type VisitFn<TAnyNode, TVisitedNode = TAnyNode> = (
46
51
*/
47
52
export type VisitorKeyMap < T > = { [ P in keyof T ] : ReadonlyArray < keyof T [ P ] > } ;
48
53
49
- export const QueryDocumentKeys : { [ key : string ] : string [ ] } ;
54
+ export const QueryDocumentKeys : {
55
+ Name : [ ] ;
56
+
57
+ Document : [ 'definitions' ] ;
58
+ // Prettier forces trailing commas, but TS pre 3.2 doesn't allow them.
59
+ // prettier-ignore
60
+ OperationDefinition : [
61
+ 'name' ,
62
+ 'variableDefinitions' ,
63
+ 'directives' ,
64
+ 'selectionSet'
65
+ ] ;
66
+ VariableDefinition : [ 'variable' , 'type' , 'defaultValue' , 'directives' ] ;
67
+ Variable : [ 'name' ] ;
68
+ SelectionSet : [ 'selections' ] ;
69
+ Field : [ 'alias' , 'name' , 'arguments' , 'directives' , 'selectionSet' ] ;
70
+ Argument : [ 'name' , 'value' ] ;
71
+
72
+ FragmentSpread : [ 'name' , 'directives' ] ;
73
+ InlineFragment : [ 'typeCondition' , 'directives' , 'selectionSet' ] ;
74
+ // prettier-ignore
75
+ FragmentDefinition : [
76
+ 'name' ,
77
+ // Note: fragment variable definitions are experimental and may be changed
78
+ // or removed in the future.
79
+ 'variableDefinitions' ,
80
+ 'typeCondition' ,
81
+ 'directives' ,
82
+ 'selectionSet'
83
+ ] ;
84
+
85
+ IntValue : [ ] ;
86
+ FloatValue : [ ] ;
87
+ StringValue : [ ] ;
88
+ BooleanValue : [ ] ;
89
+ NullValue : [ ] ;
90
+ EnumValue : [ ] ;
91
+ ListValue : [ 'values' ] ;
92
+ ObjectValue : [ 'fields' ] ;
93
+ ObjectField : [ 'name' , 'value' ] ;
94
+
95
+ Directive : [ 'name' , 'arguments' ] ;
96
+
97
+ NamedType : [ 'name' ] ;
98
+ ListType : [ 'type' ] ;
99
+ NonNullType : [ 'type' ] ;
100
+
101
+ SchemaDefinition : [ 'directives' , 'operationTypes' ] ;
102
+ OperationTypeDefinition : [ 'type' ] ;
103
+
104
+ ScalarTypeDefinition : [ 'description' , 'name' , 'directives' ] ;
105
+ // prettier-ignore
106
+ ObjectTypeDefinition : [
107
+ 'description' ,
108
+ 'name' ,
109
+ 'interfaces' ,
110
+ 'directives' ,
111
+ 'fields'
112
+ ] ;
113
+ FieldDefinition : [ 'description' , 'name' , 'arguments' , 'type' , 'directives' ] ;
114
+ // prettier-ignore
115
+ InputValueDefinition : [
116
+ 'description' ,
117
+ 'name' ,
118
+ 'type' ,
119
+ 'defaultValue' ,
120
+ 'directives'
121
+ ] ;
122
+ InterfaceTypeDefinition : [ 'description' , 'name' , 'directives' , 'fields' ] ;
123
+ UnionTypeDefinition : [ 'description' , 'name' , 'directives' , 'types' ] ;
124
+ EnumTypeDefinition : [ 'description' , 'name' , 'directives' , 'values' ] ;
125
+ EnumValueDefinition : [ 'description' , 'name' , 'directives' ] ;
126
+ InputObjectTypeDefinition : [ 'description' , 'name' , 'directives' , 'fields' ] ;
127
+
128
+ DirectiveDefinition : [ 'description' , 'name' , 'arguments' , 'locations' ] ;
129
+
130
+ SchemaExtension : [ 'directives' , 'operationTypes' ] ;
131
+
132
+ ScalarTypeExtension : [ 'name' , 'directives' ] ;
133
+ ObjectTypeExtension : [ 'name' , 'interfaces' , 'directives' , 'fields' ] ;
134
+ InterfaceTypeExtension : [ 'name' , 'directives' , 'fields' ] ;
135
+ UnionTypeExtension : [ 'name' , 'directives' , 'types' ] ;
136
+ EnumTypeExtension : [ 'name' , 'directives' , 'values' ] ;
137
+ InputObjectTypeExtension : [ 'name' , 'directives' , 'fields' ] ;
138
+ } ;
50
139
51
- export const BREAK : any ;
140
+ export const BREAK : { } ;
52
141
53
142
/**
54
143
* visit() will walk through an AST using a depth first traversal, calling
@@ -149,7 +238,7 @@ export function visit(
149
238
* If a prior visitor edits a node, no following visitors will see that node.
150
239
*/
151
240
export function visitInParallel (
152
- visitors : Array < Visitor < ASTKindToNode > > ,
241
+ visitors : ReadonlyArray < Visitor < ASTKindToNode > > ,
153
242
) : Visitor < ASTKindToNode > ;
154
243
155
244
/**
0 commit comments