walk.mjs 14 KB

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