source: frontend/node_modules/acorn-walk/dist/walk.mjs

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 14.2 KB
Line 
1// AST walker module for Mozilla Parser API compatible trees
2
3// A simple walk is one where you simply specify callbacks to be
4// called on specific nodes. The last two arguments are optional. A
5// simple use would be
6//
7// walk.simple(myTree, {
8// Expression: function(node) { ... }
9// });
10//
11// to do something with all expressions. All Parser API node types
12// can be used to identify node types, as well as Expression and
13// Statement, which denote categories of nodes.
14//
15// The base argument can be used to pass a custom (recursive)
16// walker, and state can be used to give this walked an initial
17// state.
18
19function simple(node, visitors, baseVisitor, state, override) {
20 if (!baseVisitor) { baseVisitor = base
21 ; }(function c(node, st, override) {
22 var type = override || node.type, found = visitors[type];
23 baseVisitor[type](node, st, c);
24 if (found) { found(node, st); }
25 })(node, state, override);
26}
27
28// An ancestor walk keeps an array of ancestor nodes (including the
29// current node) and passes them to the callback as third parameter
30// (and also as state parameter when no other state is present).
31function ancestor(node, visitors, baseVisitor, state, override) {
32 var ancestors = [];
33 if (!baseVisitor) { baseVisitor = base
34 ; }(function c(node, st, override) {
35 var type = override || node.type, found = visitors[type];
36 var isNew = node !== ancestors[ancestors.length - 1];
37 if (isNew) { ancestors.push(node); }
38 baseVisitor[type](node, st, c);
39 if (found) { found(node, st || ancestors, ancestors); }
40 if (isNew) { ancestors.pop(); }
41 })(node, state, override);
42}
43
44// A recursive walk is one where your functions override the default
45// walkers. They can modify and replace the state parameter that's
46// threaded through the walk, and can opt how and whether to walk
47// their child nodes (by calling their third argument on these
48// nodes).
49function recursive(node, state, funcs, baseVisitor, override) {
50 var visitor = funcs ? make(funcs, baseVisitor || undefined) : baseVisitor
51 ;(function c(node, st, override) {
52 visitor[override || node.type](node, st, c);
53 })(node, state, override);
54}
55
56function makeTest(test) {
57 if (typeof test === "string")
58 { return function (type) { return type === test; } }
59 else if (!test)
60 { return function () { return true; } }
61 else
62 { return test }
63}
64
65var Found = function Found(node, state) { this.node = node; this.state = state; };
66
67// A full walk triggers the callback on each node
68function full(node, callback, baseVisitor, state, override) {
69 if (!baseVisitor) { baseVisitor = base
70 ; }(function c(node, st, override) {
71 var type = override || node.type;
72 baseVisitor[type](node, st, c);
73 if (!override) { callback(node, st, type); }
74 })(node, state, override);
75}
76
77// An fullAncestor walk is like an ancestor walk, but triggers
78// the callback on each node
79function fullAncestor(node, callback, baseVisitor, state) {
80 if (!baseVisitor) { baseVisitor = base; }
81 var ancestors = []
82 ;(function c(node, st, override) {
83 var type = override || node.type;
84 var isNew = node !== ancestors[ancestors.length - 1];
85 if (isNew) { ancestors.push(node); }
86 baseVisitor[type](node, st, c);
87 if (!override) { callback(node, st || ancestors, ancestors, type); }
88 if (isNew) { ancestors.pop(); }
89 })(node, state);
90}
91
92// Find a node with a given start, end, and type (all are optional,
93// null can be used as wildcard). Returns a {node, state} object, or
94// undefined when it doesn't find a matching node.
95function findNodeAt(node, start, end, test, baseVisitor, state) {
96 if (!baseVisitor) { baseVisitor = base; }
97 test = makeTest(test);
98 try {
99 (function c(node, st, override) {
100 var type = override || node.type;
101 if ((start == null || node.start <= start) &&
102 (end == null || node.end >= end))
103 { baseVisitor[type](node, st, c); }
104 if ((start == null || node.start === start) &&
105 (end == null || node.end === end) &&
106 test(type, node))
107 { throw new Found(node, st) }
108 })(node, state);
109 } catch (e) {
110 if (e instanceof Found) { return e }
111 throw e
112 }
113}
114
115// Find the innermost node of a given type that contains the given
116// position. Interface similar to findNodeAt.
117function findNodeAround(node, pos, test, baseVisitor, state) {
118 test = makeTest(test);
119 if (!baseVisitor) { baseVisitor = base; }
120 try {
121 (function c(node, st, override) {
122 var type = override || node.type;
123 if (node.start > pos || node.end < pos) { return }
124 baseVisitor[type](node, st, c);
125 if (test(type, node)) { throw new Found(node, st) }
126 })(node, state);
127 } catch (e) {
128 if (e instanceof Found) { return e }
129 throw e
130 }
131}
132
133// Find the outermost matching node after a given position.
134function findNodeAfter(node, pos, test, baseVisitor, state) {
135 test = makeTest(test);
136 if (!baseVisitor) { baseVisitor = base; }
137 try {
138 (function c(node, st, override) {
139 if (node.end < pos) { return }
140 var type = override || node.type;
141 if (node.start >= pos && test(type, node)) { throw new Found(node, st) }
142 baseVisitor[type](node, st, c);
143 })(node, state);
144 } catch (e) {
145 if (e instanceof Found) { return e }
146 throw e
147 }
148}
149
150// Find the outermost matching node before a given position.
151function findNodeBefore(node, pos, test, baseVisitor, state) {
152 test = makeTest(test);
153 if (!baseVisitor) { baseVisitor = base; }
154 var max
155 ;(function c(node, st, override) {
156 if (node.start > pos) { return }
157 var type = override || node.type;
158 if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
159 { max = new Found(node, st); }
160 baseVisitor[type](node, st, c);
161 })(node, state);
162 return max
163}
164
165// Fallback to an Object.create polyfill for older environments.
166var create = Object.create || function(proto) {
167 function Ctor() {}
168 Ctor.prototype = proto;
169 return new Ctor
170};
171
172// Used to create a custom walker. Will fill in all missing node
173// type properties with the defaults.
174function make(funcs, baseVisitor) {
175 var visitor = create(baseVisitor || base);
176 for (var type in funcs) { visitor[type] = funcs[type]; }
177 return visitor
178}
179
180function skipThrough(node, st, c) { c(node, st); }
181function ignore(_node, _st, _c) {}
182
183// Node walkers.
184
185var base = {};
186
187base.Program = base.BlockStatement = function (node, st, c) {
188 for (var i = 0, list = node.body; i < list.length; i += 1)
189 {
190 var stmt = list[i];
191
192 c(stmt, st, "Statement");
193 }
194};
195base.Statement = skipThrough;
196base.EmptyStatement = ignore;
197base.ExpressionStatement = base.ParenthesizedExpression = base.ChainExpression =
198 function (node, st, c) { return c(node.expression, st, "Expression"); };
199base.IfStatement = function (node, st, c) {
200 c(node.test, st, "Expression");
201 c(node.consequent, st, "Statement");
202 if (node.alternate) { c(node.alternate, st, "Statement"); }
203};
204base.LabeledStatement = function (node, st, c) { return c(node.body, st, "Statement"); };
205base.BreakStatement = base.ContinueStatement = ignore;
206base.WithStatement = function (node, st, c) {
207 c(node.object, st, "Expression");
208 c(node.body, st, "Statement");
209};
210base.SwitchStatement = function (node, st, c) {
211 c(node.discriminant, st, "Expression");
212 for (var i$1 = 0, list$1 = node.cases; i$1 < list$1.length; i$1 += 1) {
213 var cs = list$1[i$1];
214
215 if (cs.test) { c(cs.test, st, "Expression"); }
216 for (var i = 0, list = cs.consequent; i < list.length; i += 1)
217 {
218 var cons = list[i];
219
220 c(cons, st, "Statement");
221 }
222 }
223};
224base.SwitchCase = function (node, st, c) {
225 if (node.test) { c(node.test, st, "Expression"); }
226 for (var i = 0, list = node.consequent; i < list.length; i += 1)
227 {
228 var cons = list[i];
229
230 c(cons, st, "Statement");
231 }
232};
233base.ReturnStatement = base.YieldExpression = base.AwaitExpression = function (node, st, c) {
234 if (node.argument) { c(node.argument, st, "Expression"); }
235};
236base.ThrowStatement = base.SpreadElement =
237 function (node, st, c) { return c(node.argument, st, "Expression"); };
238base.TryStatement = function (node, st, c) {
239 c(node.block, st, "Statement");
240 if (node.handler) { c(node.handler, st); }
241 if (node.finalizer) { c(node.finalizer, st, "Statement"); }
242};
243base.CatchClause = function (node, st, c) {
244 if (node.param) { c(node.param, st, "Pattern"); }
245 c(node.body, st, "Statement");
246};
247base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
248 c(node.test, st, "Expression");
249 c(node.body, st, "Statement");
250};
251base.ForStatement = function (node, st, c) {
252 if (node.init) { c(node.init, st, "ForInit"); }
253 if (node.test) { c(node.test, st, "Expression"); }
254 if (node.update) { c(node.update, st, "Expression"); }
255 c(node.body, st, "Statement");
256};
257base.ForInStatement = base.ForOfStatement = function (node, st, c) {
258 c(node.left, st, "ForInit");
259 c(node.right, st, "Expression");
260 c(node.body, st, "Statement");
261};
262base.ForInit = function (node, st, c) {
263 if (node.type === "VariableDeclaration") { c(node, st); }
264 else { c(node, st, "Expression"); }
265};
266base.DebuggerStatement = ignore;
267
268base.FunctionDeclaration = function (node, st, c) { return c(node, st, "Function"); };
269base.VariableDeclaration = function (node, st, c) {
270 for (var i = 0, list = node.declarations; i < list.length; i += 1)
271 {
272 var decl = list[i];
273
274 c(decl, st);
275 }
276};
277base.VariableDeclarator = function (node, st, c) {
278 c(node.id, st, "Pattern");
279 if (node.init) { c(node.init, st, "Expression"); }
280};
281
282base.Function = function (node, st, c) {
283 if (node.id) { c(node.id, st, "Pattern"); }
284 for (var i = 0, list = node.params; i < list.length; i += 1)
285 {
286 var param = list[i];
287
288 c(param, st, "Pattern");
289 }
290 c(node.body, st, node.expression ? "Expression" : "Statement");
291};
292
293base.Pattern = function (node, st, c) {
294 if (node.type === "Identifier")
295 { c(node, st, "VariablePattern"); }
296 else if (node.type === "MemberExpression")
297 { c(node, st, "MemberPattern"); }
298 else
299 { c(node, st); }
300};
301base.VariablePattern = ignore;
302base.MemberPattern = skipThrough;
303base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };
304base.ArrayPattern = function (node, st, c) {
305 for (var i = 0, list = node.elements; i < list.length; i += 1) {
306 var elt = list[i];
307
308 if (elt) { c(elt, st, "Pattern"); }
309 }
310};
311base.ObjectPattern = function (node, st, c) {
312 for (var i = 0, list = node.properties; i < list.length; i += 1) {
313 var prop = list[i];
314
315 if (prop.type === "Property") {
316 if (prop.computed) { c(prop.key, st, "Expression"); }
317 c(prop.value, st, "Pattern");
318 } else if (prop.type === "RestElement") {
319 c(prop.argument, st, "Pattern");
320 }
321 }
322};
323
324base.Expression = skipThrough;
325base.ThisExpression = base.Super = base.MetaProperty = ignore;
326base.ArrayExpression = function (node, st, c) {
327 for (var i = 0, list = node.elements; i < list.length; i += 1) {
328 var elt = list[i];
329
330 if (elt) { c(elt, st, "Expression"); }
331 }
332};
333base.ObjectExpression = function (node, st, c) {
334 for (var i = 0, list = node.properties; i < list.length; i += 1)
335 {
336 var prop = list[i];
337
338 c(prop, st);
339 }
340};
341base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
342base.SequenceExpression = function (node, st, c) {
343 for (var i = 0, list = node.expressions; i < list.length; i += 1)
344 {
345 var expr = list[i];
346
347 c(expr, st, "Expression");
348 }
349};
350base.TemplateLiteral = function (node, st, c) {
351 for (var i = 0, list = node.quasis; i < list.length; i += 1)
352 {
353 var quasi = list[i];
354
355 c(quasi, st);
356 }
357
358 for (var i$1 = 0, list$1 = node.expressions; i$1 < list$1.length; i$1 += 1)
359 {
360 var expr = list$1[i$1];
361
362 c(expr, st, "Expression");
363 }
364};
365base.TemplateElement = ignore;
366base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
367 c(node.argument, st, "Expression");
368};
369base.BinaryExpression = base.LogicalExpression = function (node, st, c) {
370 c(node.left, st, "Expression");
371 c(node.right, st, "Expression");
372};
373base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {
374 c(node.left, st, "Pattern");
375 c(node.right, st, "Expression");
376};
377base.ConditionalExpression = function (node, st, c) {
378 c(node.test, st, "Expression");
379 c(node.consequent, st, "Expression");
380 c(node.alternate, st, "Expression");
381};
382base.NewExpression = base.CallExpression = function (node, st, c) {
383 c(node.callee, st, "Expression");
384 if (node.arguments)
385 { for (var i = 0, list = node.arguments; i < list.length; i += 1)
386 {
387 var arg = list[i];
388
389 c(arg, st, "Expression");
390 } }
391};
392base.MemberExpression = function (node, st, c) {
393 c(node.object, st, "Expression");
394 if (node.computed) { c(node.property, st, "Expression"); }
395};
396base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
397 if (node.declaration)
398 { c(node.declaration, st, node.type === "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }
399 if (node.source) { c(node.source, st, "Expression"); }
400};
401base.ExportAllDeclaration = function (node, st, c) {
402 if (node.exported)
403 { c(node.exported, st); }
404 c(node.source, st, "Expression");
405};
406base.ImportDeclaration = function (node, st, c) {
407 for (var i = 0, list = node.specifiers; i < list.length; i += 1)
408 {
409 var spec = list[i];
410
411 c(spec, st);
412 }
413 c(node.source, st, "Expression");
414};
415base.ImportExpression = function (node, st, c) {
416 c(node.source, st, "Expression");
417};
418base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore;
419
420base.TaggedTemplateExpression = function (node, st, c) {
421 c(node.tag, st, "Expression");
422 c(node.quasi, st, "Expression");
423};
424base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
425base.Class = function (node, st, c) {
426 if (node.id) { c(node.id, st, "Pattern"); }
427 if (node.superClass) { c(node.superClass, st, "Expression"); }
428 c(node.body, st);
429};
430base.ClassBody = function (node, st, c) {
431 for (var i = 0, list = node.body; i < list.length; i += 1)
432 {
433 var elt = list[i];
434
435 c(elt, st);
436 }
437};
438base.MethodDefinition = base.Property = function (node, st, c) {
439 if (node.computed) { c(node.key, st, "Expression"); }
440 c(node.value, st, "Expression");
441};
442
443export { ancestor, base, findNodeAfter, findNodeAround, findNodeAt, findNodeBefore, full, fullAncestor, make, recursive, simple };
Note: See TracBrowser for help on using the repository browser.