• 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    ; }(function c(node, st, override) {
77      var type = override || node.type;
78      baseVisitor[type](node, st, c);
79      if (!override) { callback(node, st, type); }
80    })(node, state, override);
81  }
82
83  // An fullAncestor walk is like an ancestor walk, but triggers
84  // the callback on each node
85  function fullAncestor(node, callback, baseVisitor, state) {
86    if (!baseVisitor) { baseVisitor = base; }
87    var ancestors = []
88    ;(function c(node, st, override) {
89      var type = override || node.type;
90      var isNew = node !== ancestors[ancestors.length - 1];
91      if (isNew) { ancestors.push(node); }
92      baseVisitor[type](node, st, c);
93      if (!override) { callback(node, st || ancestors, ancestors, type); }
94      if (isNew) { ancestors.pop(); }
95    })(node, state);
96  }
97
98  // Find a node with a given start, end, and type (all are optional,
99  // null can be used as wildcard). Returns a {node, state} object, or
100  // undefined when it doesn't find a matching node.
101  function findNodeAt(node, start, end, test, baseVisitor, state) {
102    if (!baseVisitor) { baseVisitor = base; }
103    test = makeTest(test);
104    try {
105      (function c(node, st, override) {
106        var type = override || node.type;
107        if ((start == null || node.start <= start) &&
108            (end == null || node.end >= end))
109          { baseVisitor[type](node, st, c); }
110        if ((start == null || node.start === start) &&
111            (end == null || node.end === end) &&
112            test(type, node))
113          { throw new Found(node, st) }
114      })(node, state);
115    } catch (e) {
116      if (e instanceof Found) { return e }
117      throw e
118    }
119  }
120
121  // Find the innermost node of a given type that contains the given
122  // position. Interface similar to findNodeAt.
123  function findNodeAround(node, pos, test, baseVisitor, state) {
124    test = makeTest(test);
125    if (!baseVisitor) { baseVisitor = base; }
126    try {
127      (function c(node, st, override) {
128        var type = override || node.type;
129        if (node.start > pos || node.end < pos) { return }
130        baseVisitor[type](node, st, c);
131        if (test(type, node)) { throw new Found(node, st) }
132      })(node, state);
133    } catch (e) {
134      if (e instanceof Found) { return e }
135      throw e
136    }
137  }
138
139  // Find the outermost matching node after a given position.
140  function findNodeAfter(node, pos, test, baseVisitor, state) {
141    test = makeTest(test);
142    if (!baseVisitor) { baseVisitor = base; }
143    try {
144      (function c(node, st, override) {
145        if (node.end < pos) { return }
146        var type = override || node.type;
147        if (node.start >= pos && test(type, node)) { throw new Found(node, st) }
148        baseVisitor[type](node, st, c);
149      })(node, state);
150    } catch (e) {
151      if (e instanceof Found) { return e }
152      throw e
153    }
154  }
155
156  // Find the outermost matching node before a given position.
157  function findNodeBefore(node, pos, test, baseVisitor, state) {
158    test = makeTest(test);
159    if (!baseVisitor) { baseVisitor = base; }
160    var max
161    ;(function c(node, st, override) {
162      if (node.start > pos) { return }
163      var type = override || node.type;
164      if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
165        { max = new Found(node, st); }
166      baseVisitor[type](node, st, c);
167    })(node, state);
168    return max
169  }
170
171  // Fallback to an Object.create polyfill for older environments.
172  var create = Object.create || function(proto) {
173    function Ctor() {}
174    Ctor.prototype = proto;
175    return new Ctor
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 = 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 = 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 =
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    c(node.source, st, "Expression");
409  };
410  base.ImportDeclaration = function (node, st, c) {
411    for (var i = 0, list = node.specifiers; i < list.length; i += 1)
412      {
413      var spec = list[i];
414
415      c(spec, st);
416    }
417    c(node.source, st, "Expression");
418  };
419  base.ImportExpression = function (node, st, c) {
420    c(node.source, st, "Expression");
421  };
422  base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore;
423
424  base.TaggedTemplateExpression = function (node, st, c) {
425    c(node.tag, st, "Expression");
426    c(node.quasi, st, "Expression");
427  };
428  base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
429  base.Class = function (node, st, c) {
430    if (node.id) { c(node.id, st, "Pattern"); }
431    if (node.superClass) { c(node.superClass, st, "Expression"); }
432    c(node.body, st);
433  };
434  base.ClassBody = function (node, st, c) {
435    for (var i = 0, list = node.body; i < list.length; i += 1)
436      {
437      var elt = list[i];
438
439      c(elt, st);
440    }
441  };
442  base.MethodDefinition = base.Property = function (node, st, c) {
443    if (node.computed) { c(node.key, st, "Expression"); }
444    c(node.value, st, "Expression");
445  };
446
447  exports.ancestor = ancestor;
448  exports.base = base;
449  exports.findNodeAfter = findNodeAfter;
450  exports.findNodeAround = findNodeAround;
451  exports.findNodeAt = findNodeAt;
452  exports.findNodeBefore = findNodeBefore;
453  exports.full = full;
454  exports.fullAncestor = fullAncestor;
455  exports.make = make;
456  exports.recursive = recursive;
457  exports.simple = simple;
458
459  Object.defineProperty(exports, '__esModule', { value: true });
460
461}));
462