1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.visit = visit;
|
7 | exports.visitInParallel = visitInParallel;
|
8 | exports.visitWithTypeInfo = visitWithTypeInfo;
|
9 | exports.getVisitFn = getVisitFn;
|
10 | exports.BREAK = exports.QueryDocumentKeys = void 0;
|
11 |
|
12 | var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15 |
|
16 | var QueryDocumentKeys = {
|
17 | Name: [],
|
18 | Document: ['definitions'],
|
19 | OperationDefinition: ['name', 'variableDefinitions', 'directives', 'selectionSet'],
|
20 | VariableDefinition: ['variable', 'type', 'defaultValue', 'directives'],
|
21 | Variable: ['name'],
|
22 | SelectionSet: ['selections'],
|
23 | Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
|
24 | Argument: ['name', 'value'],
|
25 | FragmentSpread: ['name', 'directives'],
|
26 | InlineFragment: ['typeCondition', 'directives', 'selectionSet'],
|
27 | FragmentDefinition: ['name',
|
28 |
|
29 | 'variableDefinitions', 'typeCondition', 'directives', 'selectionSet'],
|
30 | IntValue: [],
|
31 | FloatValue: [],
|
32 | StringValue: [],
|
33 | BooleanValue: [],
|
34 | NullValue: [],
|
35 | EnumValue: [],
|
36 | ListValue: ['values'],
|
37 | ObjectValue: ['fields'],
|
38 | ObjectField: ['name', 'value'],
|
39 | Directive: ['name', 'arguments'],
|
40 | NamedType: ['name'],
|
41 | ListType: ['type'],
|
42 | NonNullType: ['type'],
|
43 | SchemaDefinition: ['directives', 'operationTypes'],
|
44 | OperationTypeDefinition: ['type'],
|
45 | ScalarTypeDefinition: ['description', 'name', 'directives'],
|
46 | ObjectTypeDefinition: ['description', 'name', 'interfaces', 'directives', 'fields'],
|
47 | FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'],
|
48 | InputValueDefinition: ['description', 'name', 'type', 'defaultValue', 'directives'],
|
49 | InterfaceTypeDefinition: ['description', 'name', 'directives', 'fields'],
|
50 | UnionTypeDefinition: ['description', 'name', 'directives', 'types'],
|
51 | EnumTypeDefinition: ['description', 'name', 'directives', 'values'],
|
52 | EnumValueDefinition: ['description', 'name', 'directives'],
|
53 | InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'],
|
54 | DirectiveDefinition: ['description', 'name', 'arguments', 'locations'],
|
55 | SchemaExtension: ['directives', 'operationTypes'],
|
56 | ScalarTypeExtension: ['name', 'directives'],
|
57 | ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
|
58 | InterfaceTypeExtension: ['name', 'directives', 'fields'],
|
59 | UnionTypeExtension: ['name', 'directives', 'types'],
|
60 | EnumTypeExtension: ['name', 'directives', 'values'],
|
61 | InputObjectTypeExtension: ['name', 'directives', 'fields']
|
62 | };
|
63 | exports.QueryDocumentKeys = QueryDocumentKeys;
|
64 | var BREAK = Object.freeze({});
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | exports.BREAK = BREAK;
|
153 |
|
154 | function visit(root, visitor) {
|
155 | var visitorKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : QueryDocumentKeys;
|
156 |
|
157 |
|
158 | var stack = undefined;
|
159 | var inArray = Array.isArray(root);
|
160 | var keys = [root];
|
161 | var index = -1;
|
162 | var edits = [];
|
163 | var node = undefined;
|
164 | var key = undefined;
|
165 | var parent = undefined;
|
166 | var path = [];
|
167 | var ancestors = [];
|
168 | var newRoot = root;
|
169 |
|
170 |
|
171 | do {
|
172 | index++;
|
173 | var isLeaving = index === keys.length;
|
174 | var isEdited = isLeaving && edits.length !== 0;
|
175 |
|
176 | if (isLeaving) {
|
177 | key = ancestors.length === 0 ? undefined : path[path.length - 1];
|
178 | node = parent;
|
179 | parent = ancestors.pop();
|
180 |
|
181 | if (isEdited) {
|
182 | if (inArray) {
|
183 | node = node.slice();
|
184 | } else {
|
185 | var clone = {};
|
186 |
|
187 | for (var _i2 = 0, _Object$keys2 = Object.keys(node); _i2 < _Object$keys2.length; _i2++) {
|
188 | var k = _Object$keys2[_i2];
|
189 | clone[k] = node[k];
|
190 | }
|
191 |
|
192 | node = clone;
|
193 | }
|
194 |
|
195 | var editOffset = 0;
|
196 |
|
197 | for (var ii = 0; ii < edits.length; ii++) {
|
198 | var editKey = edits[ii][0];
|
199 | var editValue = edits[ii][1];
|
200 |
|
201 | if (inArray) {
|
202 | editKey -= editOffset;
|
203 | }
|
204 |
|
205 | if (inArray && editValue === null) {
|
206 | node.splice(editKey, 1);
|
207 | editOffset++;
|
208 | } else {
|
209 | node[editKey] = editValue;
|
210 | }
|
211 | }
|
212 | }
|
213 |
|
214 | index = stack.index;
|
215 | keys = stack.keys;
|
216 | edits = stack.edits;
|
217 | inArray = stack.inArray;
|
218 | stack = stack.prev;
|
219 | } else {
|
220 | key = parent ? inArray ? index : keys[index] : undefined;
|
221 | node = parent ? parent[key] : newRoot;
|
222 |
|
223 | if (node === null || node === undefined) {
|
224 | continue;
|
225 | }
|
226 |
|
227 | if (parent) {
|
228 | path.push(key);
|
229 | }
|
230 | }
|
231 |
|
232 | var result = void 0;
|
233 |
|
234 | if (!Array.isArray(node)) {
|
235 | if (!isNode(node)) {
|
236 | throw new Error('Invalid AST Node: ' + (0, _inspect.default)(node));
|
237 | }
|
238 |
|
239 | var visitFn = getVisitFn(visitor, node.kind, isLeaving);
|
240 |
|
241 | if (visitFn) {
|
242 | result = visitFn.call(visitor, node, key, parent, path, ancestors);
|
243 |
|
244 | if (result === BREAK) {
|
245 | break;
|
246 | }
|
247 |
|
248 | if (result === false) {
|
249 | if (!isLeaving) {
|
250 | path.pop();
|
251 | continue;
|
252 | }
|
253 | } else if (result !== undefined) {
|
254 | edits.push([key, result]);
|
255 |
|
256 | if (!isLeaving) {
|
257 | if (isNode(result)) {
|
258 | node = result;
|
259 | } else {
|
260 | path.pop();
|
261 | continue;
|
262 | }
|
263 | }
|
264 | }
|
265 | }
|
266 | }
|
267 |
|
268 | if (result === undefined && isEdited) {
|
269 | edits.push([key, node]);
|
270 | }
|
271 |
|
272 | if (isLeaving) {
|
273 | path.pop();
|
274 | } else {
|
275 | stack = {
|
276 | inArray: inArray,
|
277 | index: index,
|
278 | keys: keys,
|
279 | edits: edits,
|
280 | prev: stack
|
281 | };
|
282 | inArray = Array.isArray(node);
|
283 | keys = inArray ? node : visitorKeys[node.kind] || [];
|
284 | index = -1;
|
285 | edits = [];
|
286 |
|
287 | if (parent) {
|
288 | ancestors.push(parent);
|
289 | }
|
290 |
|
291 | parent = node;
|
292 | }
|
293 | } while (stack !== undefined);
|
294 |
|
295 | if (edits.length !== 0) {
|
296 | newRoot = edits[edits.length - 1][1];
|
297 | }
|
298 |
|
299 | return newRoot;
|
300 | }
|
301 |
|
302 | function isNode(maybeNode) {
|
303 | return Boolean(maybeNode && typeof maybeNode.kind === 'string');
|
304 | }
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | function visitInParallel(visitors) {
|
314 | var skipping = new Array(visitors.length);
|
315 | return {
|
316 | enter: function enter(node) {
|
317 | for (var i = 0; i < visitors.length; i++) {
|
318 | if (!skipping[i]) {
|
319 | var fn = getVisitFn(visitors[i], node.kind,
|
320 |
|
321 | false);
|
322 |
|
323 | if (fn) {
|
324 | var result = fn.apply(visitors[i], arguments);
|
325 |
|
326 | if (result === false) {
|
327 | skipping[i] = node;
|
328 | } else if (result === BREAK) {
|
329 | skipping[i] = BREAK;
|
330 | } else if (result !== undefined) {
|
331 | return result;
|
332 | }
|
333 | }
|
334 | }
|
335 | }
|
336 | },
|
337 | leave: function leave(node) {
|
338 | for (var i = 0; i < visitors.length; i++) {
|
339 | if (!skipping[i]) {
|
340 | var fn = getVisitFn(visitors[i], node.kind,
|
341 |
|
342 | true);
|
343 |
|
344 | if (fn) {
|
345 | var result = fn.apply(visitors[i], arguments);
|
346 |
|
347 | if (result === BREAK) {
|
348 | skipping[i] = BREAK;
|
349 | } else if (result !== undefined && result !== false) {
|
350 | return result;
|
351 | }
|
352 | }
|
353 | } else if (skipping[i] === node) {
|
354 | skipping[i] = null;
|
355 | }
|
356 | }
|
357 | }
|
358 | };
|
359 | }
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 | function visitWithTypeInfo(typeInfo, visitor) {
|
367 | return {
|
368 | enter: function enter(node) {
|
369 | typeInfo.enter(node);
|
370 | var fn = getVisitFn(visitor, node.kind,
|
371 |
|
372 | false);
|
373 |
|
374 | if (fn) {
|
375 | var result = fn.apply(visitor, arguments);
|
376 |
|
377 | if (result !== undefined) {
|
378 | typeInfo.leave(node);
|
379 |
|
380 | if (isNode(result)) {
|
381 | typeInfo.enter(result);
|
382 | }
|
383 | }
|
384 |
|
385 | return result;
|
386 | }
|
387 | },
|
388 | leave: function leave(node) {
|
389 | var fn = getVisitFn(visitor, node.kind,
|
390 |
|
391 | true);
|
392 | var result;
|
393 |
|
394 | if (fn) {
|
395 | result = fn.apply(visitor, arguments);
|
396 | }
|
397 |
|
398 | typeInfo.leave(node);
|
399 | return result;
|
400 | }
|
401 | };
|
402 | }
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 | function getVisitFn(visitor, kind, isLeaving) {
|
410 | var kindVisitor = visitor[kind];
|
411 |
|
412 | if (kindVisitor) {
|
413 | if (!isLeaving && typeof kindVisitor === 'function') {
|
414 |
|
415 | return kindVisitor;
|
416 | }
|
417 |
|
418 | var kindSpecificVisitor = isLeaving ? kindVisitor.leave : kindVisitor.enter;
|
419 |
|
420 | if (typeof kindSpecificVisitor === 'function') {
|
421 |
|
422 | return kindSpecificVisitor;
|
423 | }
|
424 | } else {
|
425 | var specificVisitor = isLeaving ? visitor.leave : visitor.enter;
|
426 |
|
427 | if (specificVisitor) {
|
428 | if (typeof specificVisitor === 'function') {
|
429 |
|
430 | return specificVisitor;
|
431 | }
|
432 |
|
433 | var specificKindVisitor = specificVisitor[kind];
|
434 |
|
435 | if (typeof specificKindVisitor === 'function') {
|
436 |
|
437 | return specificKindVisitor;
|
438 | }
|
439 | }
|
440 | }
|
441 | }
|