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