context.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = require("./path");
  7. var _t = require("@babel/types");
  8. const {
  9. VISITOR_KEYS
  10. } = _t;
  11. class TraversalContext {
  12. constructor(scope, opts, state, parentPath) {
  13. this.queue = null;
  14. this.priorityQueue = null;
  15. this.parentPath = parentPath;
  16. this.scope = scope;
  17. this.state = state;
  18. this.opts = opts;
  19. }
  20. shouldVisit(node) {
  21. const opts = this.opts;
  22. if (opts.enter || opts.exit) return true;
  23. if (opts[node.type]) return true;
  24. const keys = VISITOR_KEYS[node.type];
  25. if (!(keys != null && keys.length)) return false;
  26. for (const key of keys) {
  27. if (
  28. node[key]) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. create(node, container, key, listKey) {
  35. return _path.default.get({
  36. parentPath: this.parentPath,
  37. parent: node,
  38. container,
  39. key: key,
  40. listKey
  41. });
  42. }
  43. maybeQueue(path, notPriority) {
  44. if (this.queue) {
  45. if (notPriority) {
  46. this.queue.push(path);
  47. } else {
  48. this.priorityQueue.push(path);
  49. }
  50. }
  51. }
  52. visitMultiple(container, parent, listKey) {
  53. if (container.length === 0) return false;
  54. const queue = [];
  55. for (let key = 0; key < container.length; key++) {
  56. const node = container[key];
  57. if (node && this.shouldVisit(node)) {
  58. queue.push(this.create(parent, container, key, listKey));
  59. }
  60. }
  61. return this.visitQueue(queue);
  62. }
  63. visitSingle(node, key) {
  64. if (this.shouldVisit(
  65. node[key])) {
  66. return this.visitQueue([this.create(node, node, key)]);
  67. } else {
  68. return false;
  69. }
  70. }
  71. visitQueue(queue) {
  72. this.queue = queue;
  73. this.priorityQueue = [];
  74. const visited = new WeakSet();
  75. let stop = false;
  76. for (const path of queue) {
  77. path.resync();
  78. if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
  79. path.pushContext(this);
  80. }
  81. if (path.key === null) continue;
  82. const {
  83. node
  84. } = path;
  85. if (visited.has(node)) continue;
  86. if (node) visited.add(node);
  87. if (path.visit()) {
  88. stop = true;
  89. break;
  90. }
  91. if (this.priorityQueue.length) {
  92. stop = this.visitQueue(this.priorityQueue);
  93. this.priorityQueue = [];
  94. this.queue = queue;
  95. if (stop) break;
  96. }
  97. }
  98. for (const path of queue) {
  99. path.popContext();
  100. }
  101. this.queue = null;
  102. return stop;
  103. }
  104. visit(node, key) {
  105. const nodes = node[key];
  106. if (!nodes) return false;
  107. if (Array.isArray(nodes)) {
  108. return this.visitMultiple(nodes, node, key);
  109. } else {
  110. return this.visitSingle(node, key);
  111. }
  112. }
  113. }
  114. exports.default = TraversalContext;
  115. //# sourceMappingURL=context.js.map