walk.mjs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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) {
  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);
  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;(function c(node, st, override) {
  47. visitor[override || node.type](node, st, c);
  48. })(node, state, override);
  49. }
  50. function makeTest(test) {
  51. if (typeof test === "string")
  52. { return function (type) { return type === test; } }
  53. else if (!test)
  54. { return function () { return true; } }
  55. else
  56. { return test }
  57. }
  58. var Found = function Found(node, state) { this.node = node; this.state = state; };
  59. // A full walk triggers the callback on each node
  60. function full(node, callback, baseVisitor, state, override) {
  61. if (!baseVisitor) { baseVisitor = base
  62. ; }(function c(node, st, override) {
  63. var type = override || node.type;
  64. baseVisitor[type](node, st, c);
  65. if (!override) { callback(node, st, type); }
  66. })(node, state, override);
  67. }
  68. // An fullAncestor walk is like an ancestor walk, but triggers
  69. // the callback on each node
  70. function fullAncestor(node, callback, baseVisitor, state) {
  71. if (!baseVisitor) { baseVisitor = base; }
  72. var ancestors = [];(function c(node, st, override) {
  73. var type = override || node.type;
  74. var isNew = node !== ancestors[ancestors.length - 1];
  75. if (isNew) { ancestors.push(node); }
  76. baseVisitor[type](node, st, c);
  77. if (!override) { callback(node, st || ancestors, ancestors, type); }
  78. if (isNew) { ancestors.pop(); }
  79. })(node, state);
  80. }
  81. // Find a node with a given start, end, and type (all are optional,
  82. // null can be used as wildcard). Returns a {node, state} object, or
  83. // undefined when it doesn't find a matching node.
  84. function findNodeAt(node, start, end, test, baseVisitor, state) {
  85. if (!baseVisitor) { baseVisitor = base; }
  86. test = makeTest(test);
  87. try {
  88. (function c(node, st, override) {
  89. var type = override || node.type;
  90. if ((start == null || node.start <= start) &&
  91. (end == null || node.end >= end))
  92. { baseVisitor[type](node, st, c); }
  93. if ((start == null || node.start === start) &&
  94. (end == null || node.end === end) &&
  95. test(type, node))
  96. { throw new Found(node, st) }
  97. })(node, state);
  98. } catch (e) {
  99. if (e instanceof Found) { return e }
  100. throw e
  101. }
  102. }
  103. // Find the innermost node of a given type that contains the given
  104. // position. Interface similar to findNodeAt.
  105. function findNodeAround(node, pos, test, baseVisitor, state) {
  106. test = makeTest(test);
  107. if (!baseVisitor) { baseVisitor = base; }
  108. try {
  109. (function c(node, st, override) {
  110. var type = override || node.type;
  111. if (node.start > pos || node.end < pos) { return }
  112. baseVisitor[type](node, st, c);
  113. if (test(type, node)) { throw new Found(node, st) }
  114. })(node, state);
  115. } catch (e) {
  116. if (e instanceof Found) { return e }
  117. throw e
  118. }
  119. }
  120. // Find the outermost matching node after a given position.
  121. function findNodeAfter(node, pos, test, baseVisitor, state) {
  122. test = makeTest(test);
  123. if (!baseVisitor) { baseVisitor = base; }
  124. try {
  125. (function c(node, st, override) {
  126. if (node.end < pos) { return }
  127. var type = override || node.type;
  128. if (node.start >= pos && test(type, node)) { throw new Found(node, st) }
  129. baseVisitor[type](node, st, c);
  130. })(node, state);
  131. } catch (e) {
  132. if (e instanceof Found) { return e }
  133. throw e
  134. }
  135. }
  136. // Find the outermost matching node before a given position.
  137. function findNodeBefore(node, pos, test, baseVisitor, state) {
  138. test = makeTest(test);
  139. if (!baseVisitor) { baseVisitor = base; }
  140. var max;(function c(node, st, override) {
  141. if (node.start > pos) { return }
  142. var type = override || node.type;
  143. if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
  144. { max = new Found(node, st); }
  145. baseVisitor[type](node, st, c);
  146. })(node, state);
  147. return max
  148. }
  149. // Fallback to an Object.create polyfill for older environments.
  150. var create = Object.create || function(proto) {
  151. function Ctor() {}
  152. Ctor.prototype = proto;
  153. return new Ctor
  154. };
  155. // Used to create a custom walker. Will fill in all missing node
  156. // type properties with the defaults.
  157. function make(funcs, baseVisitor) {
  158. var visitor = create(baseVisitor || 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.SwitchCase = function (node, st, c) {
  201. if (node.test) { c(node.test, st, "Expression"); }
  202. for (var i = 0, list = node.consequent; i < list.length; i += 1)
  203. {
  204. var cons = list[i];
  205. c(cons, st, "Statement");
  206. }
  207. };
  208. base.ReturnStatement = base.YieldExpression = base.AwaitExpression = function (node, st, c) {
  209. if (node.argument) { c(node.argument, st, "Expression"); }
  210. };
  211. base.ThrowStatement = base.SpreadElement =
  212. function (node, st, c) { return c(node.argument, st, "Expression"); };
  213. base.TryStatement = function (node, st, c) {
  214. c(node.block, st, "Statement");
  215. if (node.handler) { c(node.handler, st); }
  216. if (node.finalizer) { c(node.finalizer, st, "Statement"); }
  217. };
  218. base.CatchClause = function (node, st, c) {
  219. if (node.param) { c(node.param, st, "Pattern"); }
  220. c(node.body, st, "Statement");
  221. };
  222. base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
  223. c(node.test, st, "Expression");
  224. c(node.body, st, "Statement");
  225. };
  226. base.ForStatement = function (node, st, c) {
  227. if (node.init) { c(node.init, st, "ForInit"); }
  228. if (node.test) { c(node.test, st, "Expression"); }
  229. if (node.update) { c(node.update, st, "Expression"); }
  230. c(node.body, st, "Statement");
  231. };
  232. base.ForInStatement = base.ForOfStatement = function (node, st, c) {
  233. c(node.left, st, "ForInit");
  234. c(node.right, st, "Expression");
  235. c(node.body, st, "Statement");
  236. };
  237. base.ForInit = function (node, st, c) {
  238. if (node.type === "VariableDeclaration") { c(node, st); }
  239. else { c(node, st, "Expression"); }
  240. };
  241. base.DebuggerStatement = ignore;
  242. base.FunctionDeclaration = function (node, st, c) { return c(node, st, "Function"); };
  243. base.VariableDeclaration = function (node, st, c) {
  244. for (var i = 0, list = node.declarations; i < list.length; i += 1)
  245. {
  246. var decl = list[i];
  247. c(decl, st);
  248. }
  249. };
  250. base.VariableDeclarator = function (node, st, c) {
  251. c(node.id, st, "Pattern");
  252. if (node.init) { c(node.init, st, "Expression"); }
  253. };
  254. base.Function = function (node, st, c) {
  255. if (node.id) { c(node.id, st, "Pattern"); }
  256. for (var i = 0, list = node.params; i < list.length; i += 1)
  257. {
  258. var param = list[i];
  259. c(param, st, "Pattern");
  260. }
  261. c(node.body, st, node.expression ? "Expression" : "Statement");
  262. };
  263. base.Pattern = function (node, st, c) {
  264. if (node.type === "Identifier")
  265. { c(node, st, "VariablePattern"); }
  266. else if (node.type === "MemberExpression")
  267. { c(node, st, "MemberPattern"); }
  268. else
  269. { c(node, st); }
  270. };
  271. base.VariablePattern = ignore;
  272. base.MemberPattern = skipThrough;
  273. base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };
  274. base.ArrayPattern = function (node, st, c) {
  275. for (var i = 0, list = node.elements; i < list.length; i += 1) {
  276. var elt = list[i];
  277. if (elt) { c(elt, st, "Pattern"); }
  278. }
  279. };
  280. base.ObjectPattern = function (node, st, c) {
  281. for (var i = 0, list = node.properties; i < list.length; i += 1) {
  282. var prop = list[i];
  283. if (prop.type === "Property") {
  284. if (prop.computed) { c(prop.key, st, "Expression"); }
  285. c(prop.value, st, "Pattern");
  286. } else if (prop.type === "RestElement") {
  287. c(prop.argument, st, "Pattern");
  288. }
  289. }
  290. };
  291. base.Expression = skipThrough;
  292. base.ThisExpression = base.Super = base.MetaProperty = ignore;
  293. base.ArrayExpression = function (node, st, c) {
  294. for (var i = 0, list = node.elements; i < list.length; i += 1) {
  295. var elt = list[i];
  296. if (elt) { c(elt, st, "Expression"); }
  297. }
  298. };
  299. base.ObjectExpression = function (node, st, c) {
  300. for (var i = 0, list = node.properties; i < list.length; i += 1)
  301. {
  302. var prop = list[i];
  303. c(prop, st);
  304. }
  305. };
  306. base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
  307. base.SequenceExpression = function (node, st, c) {
  308. for (var i = 0, list = node.expressions; i < list.length; i += 1)
  309. {
  310. var expr = list[i];
  311. c(expr, st, "Expression");
  312. }
  313. };
  314. base.TemplateLiteral = function (node, st, c) {
  315. for (var i = 0, list = node.quasis; i < list.length; i += 1)
  316. {
  317. var quasi = list[i];
  318. c(quasi, st);
  319. }
  320. for (var i$1 = 0, list$1 = node.expressions; i$1 < list$1.length; i$1 += 1)
  321. {
  322. var expr = list$1[i$1];
  323. c(expr, st, "Expression");
  324. }
  325. };
  326. base.TemplateElement = ignore;
  327. base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
  328. c(node.argument, st, "Expression");
  329. };
  330. base.BinaryExpression = base.LogicalExpression = function (node, st, c) {
  331. c(node.left, st, "Expression");
  332. c(node.right, st, "Expression");
  333. };
  334. base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {
  335. c(node.left, st, "Pattern");
  336. c(node.right, st, "Expression");
  337. };
  338. base.ConditionalExpression = function (node, st, c) {
  339. c(node.test, st, "Expression");
  340. c(node.consequent, st, "Expression");
  341. c(node.alternate, st, "Expression");
  342. };
  343. base.NewExpression = base.CallExpression = function (node, st, c) {
  344. c(node.callee, st, "Expression");
  345. if (node.arguments)
  346. { for (var i = 0, list = node.arguments; i < list.length; i += 1)
  347. {
  348. var arg = list[i];
  349. c(arg, st, "Expression");
  350. } }
  351. };
  352. base.MemberExpression = function (node, st, c) {
  353. c(node.object, st, "Expression");
  354. if (node.computed) { c(node.property, st, "Expression"); }
  355. };
  356. base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
  357. if (node.declaration)
  358. { c(node.declaration, st, node.type === "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }
  359. if (node.source) { c(node.source, st, "Expression"); }
  360. };
  361. base.ExportAllDeclaration = function (node, st, c) {
  362. c(node.source, st, "Expression");
  363. };
  364. base.ImportDeclaration = function (node, st, c) {
  365. for (var i = 0, list = node.specifiers; i < list.length; i += 1)
  366. {
  367. var spec = list[i];
  368. c(spec, st);
  369. }
  370. c(node.source, st, "Expression");
  371. };
  372. base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore;
  373. base.TaggedTemplateExpression = function (node, st, c) {
  374. c(node.tag, st, "Expression");
  375. c(node.quasi, st, "Expression");
  376. };
  377. base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
  378. base.Class = function (node, st, c) {
  379. if (node.id) { c(node.id, st, "Pattern"); }
  380. if (node.superClass) { c(node.superClass, st, "Expression"); }
  381. c(node.body, st);
  382. };
  383. base.ClassBody = function (node, st, c) {
  384. for (var i = 0, list = node.body; i < list.length; i += 1)
  385. {
  386. var elt = list[i];
  387. c(elt, st);
  388. }
  389. };
  390. base.MethodDefinition = base.Property = function (node, st, c) {
  391. if (node.computed) { c(node.key, st, "Expression"); }
  392. c(node.value, st, "Expression");
  393. };
  394. export { simple, ancestor, recursive, full, fullAncestor, findNodeAt, findNodeAround, findNodeAfter, findNodeBefore, make, base };
  395. //# sourceMappingURL=walk.mjs.map