walk.js 16 KB

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