walk.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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(require,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. Object.defineProperty(exports, "__esModule", {
  45. value: true
  46. });
  47. function simple(node, visitors, base, state) {
  48. if (!base) base = exports.base;(function c(node, st, override) {
  49. var type = override || node.type,
  50. found = visitors[type];
  51. base[type](node, st, c);
  52. if (found) found(node, st);
  53. })(node, state);
  54. }
  55. function ancestor(node, visitors, base, state) {
  56. if (!base) base = exports.base;
  57. if (!state) state = [];(function c(node, st, override) {
  58. var type = override || node.type,
  59. found = visitors[type];
  60. if (node != st[st.length - 1]) {
  61. st = st.slice();
  62. st.push(node);
  63. }
  64. base[type](node, st, c);
  65. if (found) found(node, st);
  66. })(node, state);
  67. }
  68. function recursive(node, state, funcs, base) {
  69. var visitor = funcs ? exports.make(funcs, base) : base;(function c(node, st, override) {
  70. visitor[override || node.type](node, st, c);
  71. })(node, state);
  72. }
  73. function makeTest(test) {
  74. if (typeof test == "string") {
  75. return function (type) {
  76. return type == test;
  77. };
  78. } else if (!test) {
  79. return function () {
  80. return true;
  81. };
  82. } else {
  83. return test;
  84. }
  85. }
  86. var Found = function Found(node, state) {
  87. _classCallCheck(this, Found);
  88. this.node = node;this.state = state;
  89. };
  90. function findNodeAt(node, start, end, test, base, state) {
  91. test = makeTest(test);
  92. if (!base) base = exports.base;
  93. try {
  94. ;(function c(node, st, override) {
  95. var type = override || node.type;
  96. if ((start == null || node.start <= start) && (end == null || node.end >= end)) base[type](node, st, c);
  97. if (test(type, node) && (start == null || node.start == start) && (end == null || node.end == end)) throw new Found(node, st);
  98. })(node, state);
  99. } catch (e) {
  100. if (e instanceof Found) {
  101. return e;
  102. }throw e;
  103. }
  104. }
  105. function findNodeAround(node, pos, test, base, state) {
  106. test = makeTest(test);
  107. if (!base) base = exports.base;
  108. try {
  109. ;(function c(node, st, override) {
  110. var type = override || node.type;
  111. if (node.start > pos || node.end < pos) {
  112. return;
  113. }base[type](node, st, c);
  114. if (test(type, node)) throw new Found(node, st);
  115. })(node, state);
  116. } catch (e) {
  117. if (e instanceof Found) {
  118. return e;
  119. }throw e;
  120. }
  121. }
  122. function findNodeAfter(node, pos, test, base, state) {
  123. test = makeTest(test);
  124. if (!base) base = exports.base;
  125. try {
  126. ;(function c(node, st, override) {
  127. if (node.end < pos) {
  128. return;
  129. }var type = override || node.type;
  130. if (node.start >= pos && test(type, node)) throw new Found(node, st);
  131. base[type](node, st, c);
  132. })(node, state);
  133. } catch (e) {
  134. if (e instanceof Found) {
  135. return e;
  136. }throw e;
  137. }
  138. }
  139. function findNodeBefore(node, pos, test, base, state) {
  140. test = makeTest(test);
  141. if (!base) base = exports.base;
  142. var max = undefined;(function c(node, st, override) {
  143. if (node.start > pos) {
  144. return;
  145. }var type = override || node.type;
  146. if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node)) max = new Found(node, st);
  147. base[type](node, st, c);
  148. })(node, state);
  149. return max;
  150. }
  151. function make(funcs, base) {
  152. if (!base) base = exports.base;
  153. var visitor = {};
  154. for (var type in base) visitor[type] = base[type];
  155. for (var type in funcs) visitor[type] = funcs[type];
  156. return visitor;
  157. }
  158. function skipThrough(node, st, c) {
  159. c(node, st);
  160. }
  161. function ignore(_node, _st, _c) {}
  162. // Node walkers.
  163. var base = {};
  164. exports.base = base;
  165. base.Program = base.BlockStatement = function (node, st, c) {
  166. for (var i = 0; i < node.body.length; ++i) {
  167. c(node.body[i], st, "Statement");
  168. }
  169. };
  170. base.Statement = skipThrough;
  171. base.EmptyStatement = ignore;
  172. base.ExpressionStatement = base.ParenthesizedExpression = function (node, st, c) {
  173. return c(node.expression, st, "Expression");
  174. };
  175. base.IfStatement = function (node, st, c) {
  176. c(node.test, st, "Expression");
  177. c(node.consequent, st, "Statement");
  178. if (node.alternate) c(node.alternate, st, "Statement");
  179. };
  180. base.LabeledStatement = function (node, st, c) {
  181. return c(node.body, st, "Statement");
  182. };
  183. base.BreakStatement = base.ContinueStatement = ignore;
  184. base.WithStatement = function (node, st, c) {
  185. c(node.object, st, "Expression");
  186. c(node.body, st, "Statement");
  187. };
  188. base.SwitchStatement = function (node, st, c) {
  189. c(node.discriminant, st, "Expression");
  190. for (var i = 0; i < node.cases.length; ++i) {
  191. var cs = node.cases[i];
  192. if (cs.test) c(cs.test, st, "Expression");
  193. for (var j = 0; j < cs.consequent.length; ++j) {
  194. c(cs.consequent[j], st, "Statement");
  195. }
  196. }
  197. };
  198. base.ReturnStatement = base.YieldExpression = function (node, st, c) {
  199. if (node.argument) c(node.argument, st, "Expression");
  200. };
  201. base.ThrowStatement = base.SpreadElement = base.RestElement = function (node, st, c) {
  202. return c(node.argument, st, "Expression");
  203. };
  204. base.TryStatement = function (node, st, c) {
  205. c(node.block, st, "Statement");
  206. if (node.handler) c(node.handler.body, st, "ScopeBody");
  207. if (node.finalizer) c(node.finalizer, st, "Statement");
  208. };
  209. base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
  210. c(node.test, st, "Expression");
  211. c(node.body, st, "Statement");
  212. };
  213. base.ForStatement = function (node, st, c) {
  214. if (node.init) c(node.init, st, "ForInit");
  215. if (node.test) c(node.test, st, "Expression");
  216. if (node.update) c(node.update, st, "Expression");
  217. c(node.body, st, "Statement");
  218. };
  219. base.ForInStatement = base.ForOfStatement = function (node, st, c) {
  220. c(node.left, st, "ForInit");
  221. c(node.right, st, "Expression");
  222. c(node.body, st, "Statement");
  223. };
  224. base.ForInit = function (node, st, c) {
  225. if (node.type == "VariableDeclaration") c(node, st);else c(node, st, "Expression");
  226. };
  227. base.DebuggerStatement = ignore;
  228. base.FunctionDeclaration = function (node, st, c) {
  229. return c(node, st, "Function");
  230. };
  231. base.VariableDeclaration = function (node, st, c) {
  232. for (var i = 0; i < node.declarations.length; ++i) {
  233. var decl = node.declarations[i];
  234. if (decl.init) c(decl.init, st, "Expression");
  235. }
  236. };
  237. base.Function = function (node, st, c) {
  238. return c(node.body, st, "ScopeBody");
  239. };
  240. base.ScopeBody = function (node, st, c) {
  241. return c(node, st, "Statement");
  242. };
  243. base.Expression = skipThrough;
  244. base.ThisExpression = base.Super = base.MetaProperty = ignore;
  245. base.ArrayExpression = base.ArrayPattern = function (node, st, c) {
  246. for (var i = 0; i < node.elements.length; ++i) {
  247. var elt = node.elements[i];
  248. if (elt) c(elt, st, "Expression");
  249. }
  250. };
  251. base.ObjectExpression = base.ObjectPattern = function (node, st, c) {
  252. for (var i = 0; i < node.properties.length; ++i) {
  253. c(node.properties[i], st);
  254. }
  255. };
  256. base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
  257. base.SequenceExpression = base.TemplateLiteral = function (node, st, c) {
  258. for (var i = 0; i < node.expressions.length; ++i) {
  259. c(node.expressions[i], st, "Expression");
  260. }
  261. };
  262. base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
  263. c(node.argument, st, "Expression");
  264. };
  265. base.BinaryExpression = base.AssignmentExpression = base.AssignmentPattern = base.LogicalExpression = function (node, st, c) {
  266. c(node.left, st, "Expression");
  267. c(node.right, st, "Expression");
  268. };
  269. base.ConditionalExpression = function (node, st, c) {
  270. c(node.test, st, "Expression");
  271. c(node.consequent, st, "Expression");
  272. c(node.alternate, st, "Expression");
  273. };
  274. base.NewExpression = base.CallExpression = function (node, st, c) {
  275. c(node.callee, st, "Expression");
  276. if (node.arguments) for (var i = 0; i < node.arguments.length; ++i) {
  277. c(node.arguments[i], st, "Expression");
  278. }
  279. };
  280. base.MemberExpression = function (node, st, c) {
  281. c(node.object, st, "Expression");
  282. if (node.computed) c(node.property, st, "Expression");
  283. };
  284. base.ExportDeclaration = function (node, st, c) {
  285. return c(node.declaration, st);
  286. };
  287. base.ImportDeclaration = function (node, st, c) {
  288. for (var i = 0; i < node.specifiers.length; i++) {
  289. c(node.specifiers[i], st);
  290. }
  291. };
  292. base.ImportSpecifier = base.ImportBatchSpecifier = base.Identifier = base.Literal = ignore;
  293. base.TaggedTemplateExpression = function (node, st, c) {
  294. c(node.tag, st, "Expression");
  295. c(node.quasi, st);
  296. };
  297. base.ClassDeclaration = base.ClassExpression = function (node, st, c) {
  298. if (node.superClass) c(node.superClass, st, "Expression");
  299. for (var i = 0; i < node.body.body.length; i++) {
  300. c(node.body.body[i], st);
  301. }
  302. };
  303. base.MethodDefinition = base.Property = function (node, st, c) {
  304. if (node.computed) c(node.key, st, "Expression");
  305. c(node.value, st, "Expression");
  306. };
  307. base.ComprehensionExpression = function (node, st, c) {
  308. for (var i = 0; i < node.blocks.length; i++) {
  309. c(node.blocks[i].right, st, "Expression");
  310. }c(node.body, st, "Expression");
  311. };
  312. },{}]},{},[1])(1)
  313. });