1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.visit = visit;
|
7 | exports.visitInParallel = visitInParallel;
|
8 | exports.getVisitFn = getVisitFn;
|
9 | exports.BREAK = exports.QueryDocumentKeys = void 0;
|
10 |
|
11 | var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
|
12 |
|
13 | var _ast = require("./ast.js");
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 | var QueryDocumentKeys = {
|
18 | Name: [],
|
19 | Document: ['definitions'],
|
20 | OperationDefinition: ['name', 'variableDefinitions', 'directives', 'selectionSet'],
|
21 | VariableDefinition: ['variable', 'type', 'defaultValue', 'directives'],
|
22 | Variable: ['name'],
|
23 | SelectionSet: ['selections'],
|
24 | Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
|
25 | Argument: ['name', 'value'],
|
26 | FragmentSpread: ['name', 'directives'],
|
27 | InlineFragment: ['typeCondition', 'directives', 'selectionSet'],
|
28 | FragmentDefinition: ['name',
|
29 |
|
30 | 'variableDefinitions', 'typeCondition', 'directives', 'selectionSet'],
|
31 | IntValue: [],
|
32 | FloatValue: [],
|
33 | StringValue: [],
|
34 | BooleanValue: [],
|
35 | NullValue: [],
|
36 | EnumValue: [],
|
37 | ListValue: ['values'],
|
38 | ObjectValue: ['fields'],
|
39 | ObjectField: ['name', 'value'],
|
40 | Directive: ['name', 'arguments'],
|
41 | NamedType: ['name'],
|
42 | ListType: ['type'],
|
43 | NonNullType: ['type'],
|
44 | SchemaDefinition: ['description', 'directives', 'operationTypes'],
|
45 | OperationTypeDefinition: ['type'],
|
46 | ScalarTypeDefinition: ['description', 'name', 'directives'],
|
47 | ObjectTypeDefinition: ['description', 'name', 'interfaces', 'directives', 'fields'],
|
48 | FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'],
|
49 | InputValueDefinition: ['description', 'name', 'type', 'defaultValue', 'directives'],
|
50 | InterfaceTypeDefinition: ['description', 'name', 'interfaces', 'directives', 'fields'],
|
51 | UnionTypeDefinition: ['description', 'name', 'directives', 'types'],
|
52 | EnumTypeDefinition: ['description', 'name', 'directives', 'values'],
|
53 | EnumValueDefinition: ['description', 'name', 'directives'],
|
54 | InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'],
|
55 | DirectiveDefinition: ['description', 'name', 'arguments', 'locations'],
|
56 | SchemaExtension: ['directives', 'operationTypes'],
|
57 | ScalarTypeExtension: ['name', 'directives'],
|
58 | ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
|
59 | InterfaceTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
|
60 | UnionTypeExtension: ['name', 'directives', 'types'],
|
61 | EnumTypeExtension: ['name', 'directives', 'values'],
|
62 | InputObjectTypeExtension: ['name', 'directives', 'fields']
|
63 | };
|
64 | exports.QueryDocumentKeys = QueryDocumentKeys;
|
65 | var BREAK = Object.freeze({});
|
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 |
|
153 | exports.BREAK = BREAK;
|
154 |
|
155 | function visit(root, visitor) {
|
156 | var visitorKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : QueryDocumentKeys;
|
157 |
|
158 |
|
159 | var stack = undefined;
|
160 | var inArray = Array.isArray(root);
|
161 | var keys = [root];
|
162 | var index = -1;
|
163 | var edits = [];
|
164 | var node = undefined;
|
165 | var key = undefined;
|
166 | var parent = undefined;
|
167 | var path = [];
|
168 | var ancestors = [];
|
169 | var newRoot = root;
|
170 |
|
171 |
|
172 | do {
|
173 | index++;
|
174 | var isLeaving = index === keys.length;
|
175 | var isEdited = isLeaving && edits.length !== 0;
|
176 |
|
177 | if (isLeaving) {
|
178 | key = ancestors.length === 0 ? undefined : path[path.length - 1];
|
179 | node = parent;
|
180 | parent = ancestors.pop();
|
181 |
|
182 | if (isEdited) {
|
183 | if (inArray) {
|
184 | node = node.slice();
|
185 | } else {
|
186 | var clone = {};
|
187 |
|
188 | for (var _i2 = 0, _Object$keys2 = Object.keys(node); _i2 < _Object$keys2.length; _i2++) {
|
189 | var k = _Object$keys2[_i2];
|
190 | clone[k] = node[k];
|
191 | }
|
192 |
|
193 | node = clone;
|
194 | }
|
195 |
|
196 | var editOffset = 0;
|
197 |
|
198 | for (var ii = 0; ii < edits.length; ii++) {
|
199 | var editKey = edits[ii][0];
|
200 | var editValue = edits[ii][1];
|
201 |
|
202 | if (inArray) {
|
203 | editKey -= editOffset;
|
204 | }
|
205 |
|
206 | if (inArray && editValue === null) {
|
207 | node.splice(editKey, 1);
|
208 | editOffset++;
|
209 | } else {
|
210 | node[editKey] = editValue;
|
211 | }
|
212 | }
|
213 | }
|
214 |
|
215 | index = stack.index;
|
216 | keys = stack.keys;
|
217 | edits = stack.edits;
|
218 | inArray = stack.inArray;
|
219 | stack = stack.prev;
|
220 | } else {
|
221 | key = parent ? inArray ? index : keys[index] : undefined;
|
222 | node = parent ? parent[key] : newRoot;
|
223 |
|
224 | if (node === null || node === undefined) {
|
225 | continue;
|
226 | }
|
227 |
|
228 | if (parent) {
|
229 | path.push(key);
|
230 | }
|
231 | }
|
232 |
|
233 | var result = void 0;
|
234 |
|
235 | if (!Array.isArray(node)) {
|
236 | if (!(0, _ast.isNode)(node)) {
|
237 | throw new Error("Invalid AST Node: ".concat((0, _inspect.default)(node), "."));
|
238 | }
|
239 |
|
240 | var visitFn = getVisitFn(visitor, node.kind, isLeaving);
|
241 |
|
242 | if (visitFn) {
|
243 | result = visitFn.call(visitor, node, key, parent, path, ancestors);
|
244 |
|
245 | if (result === BREAK) {
|
246 | break;
|
247 | }
|
248 |
|
249 | if (result === false) {
|
250 | if (!isLeaving) {
|
251 | path.pop();
|
252 | continue;
|
253 | }
|
254 | } else if (result !== undefined) {
|
255 | edits.push([key, result]);
|
256 |
|
257 | if (!isLeaving) {
|
258 | if ((0, _ast.isNode)(result)) {
|
259 | node = result;
|
260 | } else {
|
261 | path.pop();
|
262 | continue;
|
263 | }
|
264 | }
|
265 | }
|
266 | }
|
267 | }
|
268 |
|
269 | if (result === undefined && isEdited) {
|
270 | edits.push([key, node]);
|
271 | }
|
272 |
|
273 | if (isLeaving) {
|
274 | path.pop();
|
275 | } else {
|
276 | var _visitorKeys$node$kin;
|
277 |
|
278 | stack = {
|
279 | inArray: inArray,
|
280 | index: index,
|
281 | keys: keys,
|
282 | edits: edits,
|
283 | prev: stack
|
284 | };
|
285 | inArray = Array.isArray(node);
|
286 | keys = inArray ? node : (_visitorKeys$node$kin = visitorKeys[node.kind]) !== null && _visitorKeys$node$kin !== void 0 ? _visitorKeys$node$kin : [];
|
287 | index = -1;
|
288 | edits = [];
|
289 |
|
290 | if (parent) {
|
291 | ancestors.push(parent);
|
292 | }
|
293 |
|
294 | parent = node;
|
295 | }
|
296 | } while (stack !== undefined);
|
297 |
|
298 | if (edits.length !== 0) {
|
299 | newRoot = edits[edits.length - 1][1];
|
300 | }
|
301 |
|
302 | return newRoot;
|
303 | }
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | function visitInParallel(visitors) {
|
313 | var skipping = new Array(visitors.length);
|
314 | return {
|
315 | enter: function enter(node) {
|
316 | for (var i = 0; i < visitors.length; i++) {
|
317 | if (skipping[i] == null) {
|
318 | var fn = getVisitFn(visitors[i], node.kind,
|
319 |
|
320 | false);
|
321 |
|
322 | if (fn) {
|
323 | var result = fn.apply(visitors[i], arguments);
|
324 |
|
325 | if (result === false) {
|
326 | skipping[i] = node;
|
327 | } else if (result === BREAK) {
|
328 | skipping[i] = BREAK;
|
329 | } else if (result !== undefined) {
|
330 | return result;
|
331 | }
|
332 | }
|
333 | }
|
334 | }
|
335 | },
|
336 | leave: function leave(node) {
|
337 | for (var i = 0; i < visitors.length; i++) {
|
338 | if (skipping[i] == null) {
|
339 | var fn = getVisitFn(visitors[i], node.kind,
|
340 |
|
341 | true);
|
342 |
|
343 | if (fn) {
|
344 | var result = fn.apply(visitors[i], arguments);
|
345 |
|
346 | if (result === BREAK) {
|
347 | skipping[i] = BREAK;
|
348 | } else if (result !== undefined && result !== false) {
|
349 | return result;
|
350 | }
|
351 | }
|
352 | } else if (skipping[i] === node) {
|
353 | skipping[i] = null;
|
354 | }
|
355 | }
|
356 | }
|
357 | };
|
358 | }
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 | function getVisitFn(visitor, kind, isLeaving) {
|
366 | var kindVisitor = visitor[kind];
|
367 |
|
368 | if (kindVisitor) {
|
369 | if (!isLeaving && typeof kindVisitor === 'function') {
|
370 |
|
371 | return kindVisitor;
|
372 | }
|
373 |
|
374 | var kindSpecificVisitor = isLeaving ? kindVisitor.leave : kindVisitor.enter;
|
375 |
|
376 | if (typeof kindSpecificVisitor === 'function') {
|
377 |
|
378 | return kindSpecificVisitor;
|
379 | }
|
380 | } else {
|
381 | var specificVisitor = isLeaving ? visitor.leave : visitor.enter;
|
382 |
|
383 | if (specificVisitor) {
|
384 | if (typeof specificVisitor === 'function') {
|
385 |
|
386 | return specificVisitor;
|
387 | }
|
388 |
|
389 | var specificKindVisitor = specificVisitor[kind];
|
390 |
|
391 | if (typeof specificKindVisitor === 'function') {
|
392 |
|
393 | return specificKindVisitor;
|
394 | }
|
395 | }
|
396 | }
|
397 | }
|