walk.js 15 KB

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