walk.es.js 13 KB

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