You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 lines
16 KiB

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