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