walk.js 13 KB

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