traverseForScope.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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._forceSetScope.call(path);
  32. const visitor = exploded[node.type];
  33. if (visitor != null && visitor.enter) {
  34. for (const visit of visitor.enter) {
  35. visit.call(state, path, state);
  36. }
  37. }
  38. if (path.shouldSkip) {
  39. return;
  40. }
  41. const keys = VISITOR_KEYS[node.type];
  42. if (!(keys != null && keys.length)) {
  43. return;
  44. }
  45. for (const key of keys) {
  46. const prop = node[key];
  47. if (!prop) continue;
  48. if (Array.isArray(prop)) {
  49. for (let i = 0; i < prop.length; i++) {
  50. const value = prop[i];
  51. _traverse(path, node, value, prop, i, key);
  52. }
  53. } else {
  54. _traverse(path, node, prop, node, key, null);
  55. }
  56. }
  57. if (visitor != null && visitor.exit) {
  58. for (const visit of visitor.exit) {
  59. visit.call(state, path, state);
  60. }
  61. }
  62. }
  63. }
  64. //# sourceMappingURL=traverseForScope.js.map