walk.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}(g.acorn || (g.acorn = {})).walk = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. "use strict";
  3. var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
  4. // AST walker module for Mozilla Parser API compatible trees
  5. // A simple walk is one where you simply specify callbacks to be
  6. // called on specific nodes. The last two arguments are optional. A
  7. // simple use would be
  8. //
  9. // walk.simple(myTree, {
  10. // Expression: function(node) { ... }
  11. // });
  12. //
  13. // to do something with all expressions. All Parser API node types
  14. // can be used to identify node types, as well as Expression,
  15. // Statement, and ScopeBody, which denote categories of nodes.
  16. //
  17. // The base argument can be used to pass a custom (recursive)
  18. // walker, and state can be used to give this walked an initial
  19. // state.
  20. exports.simple = simple;
  21. // An ancestor walk builds up an array of ancestor nodes (including
  22. // the current node) and passes them to the callback as the state parameter.
  23. exports.ancestor = ancestor;
  24. // A recursive walk is one where your functions override the default
  25. // walkers. They can modify and replace the state parameter that's
  26. // threaded through the walk, and can opt how and whether to walk
  27. // their child nodes (by calling their third argument on these
  28. // nodes).
  29. exports.recursive = recursive;
  30. // Find a node with a given start, end, and type (all are optional,
  31. // null can be used as wildcard). Returns a {node, state} object, or
  32. // undefined when it doesn't find a matching node.
  33. exports.findNodeAt = findNodeAt;
  34. // Find the innermost node of a given type that contains the given
  35. // position. Interface similar to findNodeAt.
  36. exports.findNodeAround = findNodeAround;
  37. // Find the outermost matching node after a given position.
  38. exports.findNodeAfter = findNodeAfter;
  39. // Find the outermost matching node before a given position.
  40. exports.findNodeBefore = findNodeBefore;
  41. // Used to create a custom walker. Will fill in all missing node
  42. // type properties with the defaults.
  43. exports.make = make;
  44. exports.__esModule = true;
  45. function simple(node, visitors, base, state) {
  46. if (!base) base = exports.base;(function c(node, st, override) {
  47. var type = override || node.type,
  48. found = visitors[type];
  49. base[type](node, st, c);
  50. if (found) found(node, st);
  51. })(node, state);
  52. }
  53. function ancestor(node, visitors, base, state) {
  54. if (!base) base = exports.base;
  55. if (!state) state = [];(function c(node, st, override) {
  56. var type = override || node.type,
  57. found = visitors[type];
  58. if (node != st[st.length - 1]) {
  59. st = st.slice();
  60. st.push(node);
  61. }
  62. base[type](node, st, c);
  63. if (found) found(node, st);
  64. })(node, state);
  65. }
  66. function recursive(node, state, funcs, base) {
  67. var visitor = funcs ? exports.make(funcs, base) : base;(function c(node, st, override) {
  68. visitor[override || node.type](node, st, c);
  69. })(node, state);
  70. }
  71. function makeTest(test) {
  72. if (typeof test == "string") {
  73. return function (type) {
  74. return type == test;
  75. };
  76. } else if (!test) {
  77. return function () {
  78. return true;
  79. };
  80. } else {
  81. return test;
  82. }
  83. }
  84. var Found = function Found(node, state) {
  85. _classCallCheck(this, Found);
  86. this.node = node;this.state = state;
  87. };
  88. function findNodeAt(node, start, end, test, base, state) {
  89. test = makeTest(test);
  90. if (!base) base = exports.base;
  91. try {
  92. ;(function c(node, st, override) {
  93. var type = override || node.type;
  94. if ((start == null || node.start <= start) && (end == null || node.end >= end)) base[type](node, st, c);
  95. if (test(type, node) && (start == null || node.start == start) && (end == null || node.end == end)) throw new Found(node, st);
  96. })(node, state);
  97. } catch (e) {
  98. if (e instanceof Found) {
  99. return e;
  100. }throw e;
  101. }
  102. }
  103. function findNodeAround(node, pos, test, base, state) {
  104. test = makeTest(test);
  105. if (!base) base = exports.base;
  106. try {
  107. ;(function c(node, st, override) {
  108. var type = override || node.type;
  109. if (node.start > pos || node.end < pos) {
  110. return;
  111. }base[type](node, st, c);
  112. if (test(type, node)) throw new Found(node, st);
  113. })(node, state);
  114. } catch (e) {
  115. if (e instanceof Found) {
  116. return e;
  117. }throw e;
  118. }
  119. }
  120. function findNodeAfter(node, pos, test, base, state) {
  121. test = makeTest(test);
  122. if (!base) base = exports.base;
  123. try {
  124. ;(function c(node, st, override) {
  125. if (node.end < pos) {
  126. return;
  127. }var type = override || node.type;
  128. if (node.start >= pos && test(type, node)) throw new Found(node, st);
  129. base[type](node, st, c);
  130. })(node, state);
  131. } catch (e) {
  132. if (e instanceof Found) {
  133. return e;
  134. }throw e;
  135. }
  136. }
  137. function findNodeBefore(node, pos, test, base, state) {
  138. test = makeTest(test);
  139. if (!base) base = exports.base;
  140. var max = undefined;(function c(node, st, override) {
  141. if (node.start > pos) {
  142. return;
  143. }var type = override || node.type;
  144. if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node)) max = new Found(node, st);
  145. base[type](node, st, c);
  146. })(node, state);
  147. return max;
  148. }
  149. function make(funcs, base) {
  150. if (!base) base = exports.base;
  151. var visitor = {};
  152. for (var type in base) visitor[type] = base[type];
  153. for (var type in funcs) visitor[type] = funcs[type];
  154. return visitor;
  155. }
  156. function skipThrough(node, st, c) {
  157. c(node, st);
  158. }
  159. function ignore(_node, _st, _c) {}
  160. // Node walkers.
  161. var base = {};
  162. exports.base = base;
  163. base.Program = base.BlockStatement = function (node, st, c) {
  164. for (var i = 0; i < node.body.length; ++i) {
  165. c(node.body[i], st, "Statement");
  166. }
  167. };
  168. base.Statement = skipThrough;
  169. base.EmptyStatement = ignore;
  170. base.ExpressionStatement = base.ParenthesizedExpression = function (node, st, c) {
  171. return c(node.expression, st, "Expression");
  172. };
  173. base.IfStatement = function (node, st, c) {
  174. c(node.test, st, "Expression");
  175. c(node.consequent, st, "Statement");
  176. if (node.alternate) c(node.alternate, st, "Statement");
  177. };
  178. base.LabeledStatement = function (node, st, c) {
  179. return c(node.body, st, "Statement");
  180. };
  181. base.BreakStatement = base.ContinueStatement = ignore;
  182. base.WithStatement = function (node, st, c) {
  183. c(node.object, st, "Expression");
  184. c(node.body, st, "Statement");
  185. };
  186. base.SwitchStatement = function (node, st, c) {
  187. c(node.discriminant, st, "Expression");
  188. for (var i = 0; i < node.cases.length; ++i) {
  189. var cs = node.cases[i];
  190. if (cs.test) c(cs.test, st, "Expression");
  191. for (var j = 0; j < cs.consequent.length; ++j) {
  192. c(cs.consequent[j], st, "Statement");
  193. }
  194. }
  195. };
  196. base.ReturnStatement = base.YieldExpression = function (node, st, c) {
  197. if (node.argument) c(node.argument, st, "Expression");
  198. };
  199. base.ThrowStatement = base.SpreadElement = base.RestElement = function (node, st, c) {
  200. return c(node.argument, st, "Expression");
  201. };
  202. base.TryStatement = function (node, st, c) {
  203. c(node.block, st, "Statement");
  204. if (node.handler) c(node.handler.body, st, "ScopeBody");
  205. if (node.finalizer) c(node.finalizer, st, "Statement");
  206. };
  207. base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
  208. c(node.test, st, "Expression");
  209. c(node.body, st, "Statement");
  210. };
  211. base.ForStatement = function (node, st, c) {
  212. if (node.init) c(node.init, st, "ForInit");
  213. if (node.test) c(node.test, st, "Expression");
  214. if (node.update) c(node.update, st, "Expression");
  215. c(node.body, st, "Statement");
  216. };
  217. base.ForInStatement = base.ForOfStatement = function (node, st, c) {
  218. c(node.left, st, "ForInit");
  219. c(node.right, st, "Expression");
  220. c(node.body, st, "Statement");
  221. };
  222. base.ForInit = function (node, st, c) {
  223. if (node.type == "VariableDeclaration") c(node, st);else c(node, st, "Expression");
  224. };
  225. base.DebuggerStatement = ignore;
  226. base.FunctionDeclaration = function (node, st, c) {
  227. return c(node, st, "Function");
  228. };
  229. base.VariableDeclaration = function (node, st, c) {
  230. for (var i = 0; i < node.declarations.length; ++i) {
  231. var decl = node.declarations[i];
  232. if (decl.init) c(decl.init, st, "Expression");
  233. }
  234. };
  235. base.Function = function (node, st, c) {
  236. return c(node.body, st, "ScopeBody");
  237. };
  238. base.ScopeBody = function (node, st, c) {
  239. return c(node, st, "Statement");
  240. };
  241. base.Expression = skipThrough;
  242. base.ThisExpression = base.Super = base.MetaProperty = ignore;
  243. base.ArrayExpression = base.ArrayPattern = function (node, st, c) {
  244. for (var i = 0; i < node.elements.length; ++i) {
  245. var elt = node.elements[i];
  246. if (elt) c(elt, st, "Expression");
  247. }
  248. };
  249. base.ObjectExpression = base.ObjectPattern = function (node, st, c) {
  250. for (var i = 0; i < node.properties.length; ++i) {
  251. c(node.properties[i], st);
  252. }
  253. };
  254. base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
  255. base.SequenceExpression = base.TemplateLiteral = function (node, st, c) {
  256. for (var i = 0; i < node.expressions.length; ++i) {
  257. c(node.expressions[i], st, "Expression");
  258. }
  259. };
  260. base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
  261. c(node.argument, st, "Expression");
  262. };
  263. base.BinaryExpression = base.AssignmentExpression = base.AssignmentPattern = base.LogicalExpression = function (node, st, c) {
  264. c(node.left, st, "Expression");
  265. c(node.right, st, "Expression");
  266. };
  267. base.ConditionalExpression = function (node, st, c) {
  268. c(node.test, st, "Expression");
  269. c(node.consequent, st, "Expression");
  270. c(node.alternate, st, "Expression");
  271. };
  272. base.NewExpression = base.CallExpression = function (node, st, c) {
  273. c(node.callee, st, "Expression");
  274. if (node.arguments) for (var i = 0; i < node.arguments.length; ++i) {
  275. c(node.arguments[i], st, "Expression");
  276. }
  277. };
  278. base.MemberExpression = function (node, st, c) {
  279. c(node.object, st, "Expression");
  280. if (node.computed) c(node.property, st, "Expression");
  281. };
  282. base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
  283. return c(node.declaration, st);
  284. };
  285. base.ImportDeclaration = function (node, st, c) {
  286. for (var i = 0; i < node.specifiers.length; i++) {
  287. c(node.specifiers[i], st);
  288. }
  289. };
  290. base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore;
  291. base.TaggedTemplateExpression = function (node, st, c) {
  292. c(node.tag, st, "Expression");
  293. c(node.quasi, st);
  294. };
  295. base.ClassDeclaration = base.ClassExpression = function (node, st, c) {
  296. if (node.superClass) c(node.superClass, st, "Expression");
  297. for (var i = 0; i < node.body.body.length; i++) {
  298. c(node.body.body[i], st);
  299. }
  300. };
  301. base.MethodDefinition = base.Property = function (node, st, c) {
  302. if (node.computed) c(node.key, st, "Expression");
  303. c(node.value, st, "Expression");
  304. };
  305. base.ComprehensionExpression = function (node, st, c) {
  306. for (var i = 0; i < node.blocks.length; i++) {
  307. c(node.blocks[i].right, st, "Expression");
  308. }c(node.body, st, "Expression");
  309. };
  310. },{}]},{},[1])(1)
  311. });