traverseForScope.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = traverseForScope;
  6. var _t = require("@babel/types");
  7. var _index = require("../index.js");
  8. var _visitors = require("../visitors.js");
  9. var _context = require("../path/context.js");
  10. const {
  11. VISITOR_KEYS
  12. } = _t;
  13. function traverseForScope(path, visitors, state) {
  14. const exploded = (0, _visitors.explode)(visitors);
  15. if (exploded.enter || exploded.exit) {
  16. throw new Error("Should not be used with enter/exit visitors.");
  17. }
  18. _traverse(path.parentPath, path.parent, path.node, path.container, path.key, path.listKey, path.hub, path);
  19. function _traverse(parentPath, parent, node, container, key, listKey, hub, inPath) {
  20. if (!node) {
  21. return;
  22. }
  23. const path = inPath || _index.NodePath.get({
  24. hub,
  25. parentPath,
  26. parent,
  27. container,
  28. listKey,
  29. key
  30. });
  31. _context.setScope.call(path);
  32. const visitor = exploded[node.type];
  33. if (visitor) {
  34. if (visitor.enter) {
  35. for (const visit of visitor.enter) {
  36. visit.call(state, path, state);
  37. }
  38. }
  39. if (visitor.exit) {
  40. for (const visit of visitor.exit) {
  41. visit.call(state, path, state);
  42. }
  43. }
  44. }
  45. if (path.shouldSkip) {
  46. return;
  47. }
  48. const keys = VISITOR_KEYS[node.type];
  49. if (!(keys != null && keys.length)) {
  50. return;
  51. }
  52. for (const key of keys) {
  53. const prop = node[key];
  54. if (!prop) continue;
  55. if (Array.isArray(prop)) {
  56. for (let i = 0; i < prop.length; i++) {
  57. const value = prop[i];
  58. _traverse(path, node, value, prop, i, key);
  59. }
  60. } else {
  61. _traverse(path, node, prop, node, key, null);
  62. }
  63. }
  64. }
  65. }
  66. //# sourceMappingURL=traverseForScope.js.map