walk.js 11 KB

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