walk.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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,
  17. // Statement, and ScopeBody, 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 === void 0 ) 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. if ( baseVisitor === void 0 ) baseVisitor = base;
  35. var ancestors = [];(function c(node, st, override) {
  36. var type = override || node.type, found = visitors[type];
  37. var isNew = node != ancestors[ancestors.length - 1];
  38. if (isNew) { ancestors.push(node); }
  39. baseVisitor[type](node, st, c);
  40. if (found) { found(node, st || ancestors, ancestors); }
  41. if (isNew) { ancestors.pop(); }
  42. })(node, state);
  43. }
  44. // A recursive walk is one where your functions override the default
  45. // walkers. They can modify and replace the state parameter that's
  46. // threaded through the walk, and can opt how and whether to walk
  47. // their child nodes (by calling their third argument on these
  48. // nodes).
  49. function recursive(node, state, funcs, baseVisitor, override) {
  50. var visitor = funcs ? make(funcs, baseVisitor || undefined) : baseVisitor;(function c(node, st, override) {
  51. visitor[override || node.type](node, st, c);
  52. })(node, state, override);
  53. }
  54. function makeTest(test) {
  55. if (typeof test == "string")
  56. { return function (type) { return type == test; } }
  57. else if (!test)
  58. { return function () { return true; } }
  59. else
  60. { return test }
  61. }
  62. var Found = function Found(node, state) { this.node = node; this.state = state; };
  63. // A full walk triggers the callback on each node
  64. function full(node, callback, baseVisitor, state, override) {
  65. if ( baseVisitor === void 0 ) baseVisitor = base;
  66. (function c(node, st, override) {
  67. var type = override || node.type;
  68. baseVisitor[type](node, st, c);
  69. if (!override) { callback(node, st, type); }
  70. })(node, state, override);
  71. }
  72. // An fullAncestor walk is like an ancestor walk, but triggers
  73. // the callback on each node
  74. function fullAncestor(node, callback, baseVisitor, state) {
  75. if ( baseVisitor === void 0 ) baseVisitor = base;
  76. var ancestors = [];(function c(node, st, override) {
  77. var type = override || node.type;
  78. var isNew = node != ancestors[ancestors.length - 1];
  79. if (isNew) { ancestors.push(node); }
  80. baseVisitor[type](node, st, c);
  81. if (!override) { callback(node, st || ancestors, ancestors, type); }
  82. if (isNew) { ancestors.pop(); }
  83. })(node, state);
  84. }
  85. // Find a node with a given start, end, and type (all are optional,
  86. // null can be used as wildcard). Returns a {node, state} object, or
  87. // undefined when it doesn't find a matching node.
  88. function findNodeAt(node, start, end, test, baseVisitor, state) {
  89. if ( baseVisitor === void 0 ) baseVisitor = base;
  90. test = makeTest(test);
  91. try {
  92. (function c(node, st, override) {
  93. var type = override || node.type;
  94. if ((start == null || node.start <= start) &&
  95. (end == null || node.end >= end))
  96. { baseVisitor[type](node, st, c); }
  97. if ((start == null || node.start == start) &&
  98. (end == null || node.end == end) &&
  99. test(type, node))
  100. { throw new Found(node, st) }
  101. })(node, state);
  102. } catch (e) {
  103. if (e instanceof Found) { return e }
  104. throw e
  105. }
  106. }
  107. // Find the innermost node of a given type that contains the given
  108. // position. Interface similar to findNodeAt.
  109. function findNodeAround(node, pos, test, baseVisitor, state) {
  110. if ( baseVisitor === void 0 ) baseVisitor = base;
  111. test = makeTest(test);
  112. try {
  113. (function c(node, st, override) {
  114. var type = override || node.type;
  115. if (node.start > pos || node.end < pos) { return }
  116. baseVisitor[type](node, st, c);
  117. if (test(type, node)) { throw new Found(node, st) }
  118. })(node, state);
  119. } catch (e) {
  120. if (e instanceof Found) { return e }
  121. throw e
  122. }
  123. }
  124. // Find the outermost matching node after a given position.
  125. function findNodeAfter(node, pos, test, baseVisitor, state) {
  126. if ( baseVisitor === void 0 ) baseVisitor = base;
  127. test = makeTest(test);
  128. try {
  129. (function c(node, st, override) {
  130. if (node.end < pos) { return }
  131. var type = override || node.type;
  132. if (node.start >= pos && test(type, node)) { throw new Found(node, st) }
  133. baseVisitor[type](node, st, c);
  134. })(node, state);
  135. } catch (e) {
  136. if (e instanceof Found) { return e }
  137. throw e
  138. }
  139. }
  140. // Find the outermost matching node before a given position.
  141. function findNodeBefore(node, pos, test, baseVisitor, state) {
  142. if ( baseVisitor === void 0 ) baseVisitor = base;
  143. test = makeTest(test);
  144. var max;(function c(node, st, override) {
  145. if (node.start > pos) { return }
  146. var type = override || node.type;
  147. if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
  148. { max = new Found(node, st); }
  149. baseVisitor[type](node, st, c);
  150. })(node, state);
  151. return max
  152. }
  153. // Fallback to an Object.create polyfill for older environments.
  154. var create = Object.create || function(proto) {
  155. function Ctor() {}
  156. Ctor.prototype = proto;
  157. return new Ctor
  158. };
  159. // Used to create a custom walker. Will fill in all missing node
  160. // type properties with the defaults.
  161. function make(funcs, baseVisitor) {
  162. if ( baseVisitor === void 0 ) baseVisitor = base;
  163. var visitor = create(baseVisitor);
  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. c(node.param, st, "Pattern");
  225. c(node.body, st, "ScopeBody");
  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 ? "ScopeExpression" : "ScopeBody");
  267. };
  268. // FIXME drop these node types in next major version
  269. // (They are awkward, and in ES6 every block can be a scope.)
  270. base.ScopeBody = function (node, st, c) { return c(node, st, "Statement"); };
  271. base.ScopeExpression = function (node, st, c) { return c(node, st, "Expression"); };
  272. base.Pattern = function (node, st, c) {
  273. if (node.type == "Identifier")
  274. { c(node, st, "VariablePattern"); }
  275. else if (node.type == "MemberExpression")
  276. { c(node, st, "MemberPattern"); }
  277. else
  278. { c(node, st); }
  279. };
  280. base.VariablePattern = ignore;
  281. base.MemberPattern = skipThrough;
  282. base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };
  283. base.ArrayPattern = function (node, st, c) {
  284. for (var i = 0, list = node.elements; i < list.length; i += 1) {
  285. var elt = list[i];
  286. if (elt) { c(elt, st, "Pattern"); }
  287. }
  288. };
  289. base.ObjectPattern = function (node, st, c) {
  290. for (var i = 0, list = node.properties; i < list.length; i += 1) {
  291. var prop = list[i];
  292. if (prop.type === "Property") {
  293. if (prop.computed) { c(prop.key, st, "Expression"); }
  294. c(prop.value, st, "Pattern");
  295. } else if (prop.type === "RestElement") {
  296. c(prop.argument, st, "Pattern");
  297. }
  298. }
  299. };
  300. base.Expression = skipThrough;
  301. base.ThisExpression = base.Super = base.MetaProperty = ignore;
  302. base.ArrayExpression = function (node, st, c) {
  303. for (var i = 0, list = node.elements; i < list.length; i += 1) {
  304. var elt = list[i];
  305. if (elt) { c(elt, st, "Expression"); }
  306. }
  307. };
  308. base.ObjectExpression = function (node, st, c) {
  309. for (var i = 0, list = node.properties; i < list.length; i += 1)
  310. {
  311. var prop = list[i];
  312. c(prop, st);
  313. }
  314. };
  315. base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
  316. base.SequenceExpression = base.TemplateLiteral = function (node, st, c) {
  317. for (var i = 0, list = node.expressions; i < list.length; i += 1)
  318. {
  319. var expr = list[i];
  320. c(expr, st, "Expression");
  321. }
  322. };
  323. base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
  324. c(node.argument, st, "Expression");
  325. };
  326. base.BinaryExpression = base.LogicalExpression = function (node, st, c) {
  327. c(node.left, st, "Expression");
  328. c(node.right, st, "Expression");
  329. };
  330. base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {
  331. c(node.left, st, "Pattern");
  332. c(node.right, st, "Expression");
  333. };
  334. base.ConditionalExpression = function (node, st, c) {
  335. c(node.test, st, "Expression");
  336. c(node.consequent, st, "Expression");
  337. c(node.alternate, st, "Expression");
  338. };
  339. base.NewExpression = base.CallExpression = function (node, st, c) {
  340. c(node.callee, st, "Expression");
  341. if (node.arguments)
  342. { for (var i = 0, list = node.arguments; i < list.length; i += 1)
  343. {
  344. var arg = list[i];
  345. c(arg, st, "Expression");
  346. } }
  347. };
  348. base.MemberExpression = function (node, st, c) {
  349. c(node.object, st, "Expression");
  350. if (node.computed) { c(node.property, st, "Expression"); }
  351. };
  352. base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
  353. if (node.declaration)
  354. { c(node.declaration, st, node.type == "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }
  355. if (node.source) { c(node.source, st, "Expression"); }
  356. };
  357. base.ExportAllDeclaration = function (node, st, c) {
  358. c(node.source, st, "Expression");
  359. };
  360. base.ImportDeclaration = function (node, st, c) {
  361. for (var i = 0, list = node.specifiers; i < list.length; i += 1)
  362. {
  363. var spec = list[i];
  364. c(spec, st);
  365. }
  366. c(node.source, st, "Expression");
  367. };
  368. base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore;
  369. base.TaggedTemplateExpression = function (node, st, c) {
  370. c(node.tag, st, "Expression");
  371. c(node.quasi, st, "Expression");
  372. };
  373. base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
  374. base.Class = function (node, st, c) {
  375. if (node.id) { c(node.id, st, "Pattern"); }
  376. if (node.superClass) { c(node.superClass, st, "Expression"); }
  377. c(node.body, st);
  378. };
  379. base.ClassBody = function (node, st, c) {
  380. for (var i = 0, list = node.body; i < list.length; i += 1)
  381. {
  382. var elt = list[i];
  383. c(elt, st);
  384. }
  385. };
  386. base.MethodDefinition = base.Property = function (node, st, c) {
  387. if (node.computed) { c(node.key, st, "Expression"); }
  388. c(node.value, st, "Expression");
  389. };
  390. exports.simple = simple;
  391. exports.ancestor = ancestor;
  392. exports.recursive = recursive;
  393. exports.full = full;
  394. exports.fullAncestor = fullAncestor;
  395. exports.findNodeAt = findNodeAt;
  396. exports.findNodeAround = findNodeAround;
  397. exports.findNodeAfter = findNodeAfter;
  398. exports.findNodeBefore = findNodeBefore;
  399. exports.make = make;
  400. exports.base = base;
  401. Object.defineProperty(exports, '__esModule', { value: true });
  402. })));