context.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _index = require("./path/index.js");
  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 (node[key]) {
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. create(node, container, key, listKey) {
  34. return _index.default.get({
  35. parentPath: this.parentPath,
  36. parent: node,
  37. container,
  38. key: key,
  39. listKey
  40. });
  41. }
  42. maybeQueue(path, notPriority) {
  43. if (this.queue) {
  44. if (notPriority) {
  45. this.queue.push(path);
  46. } else {
  47. this.priorityQueue.push(path);
  48. }
  49. }
  50. }
  51. visitMultiple(container, parent, listKey) {
  52. if (container.length === 0) return false;
  53. const queue = [];
  54. for (let key = 0; key < container.length; key++) {
  55. const node = container[key];
  56. if (node && this.shouldVisit(node)) {
  57. queue.push(this.create(parent, container, key, listKey));
  58. }
  59. }
  60. return this.visitQueue(queue);
  61. }
  62. visitSingle(node, key) {
  63. if (this.shouldVisit(node[key])) {
  64. return this.visitQueue([this.create(node, node, key)]);
  65. } else {
  66. return false;
  67. }
  68. }
  69. visitQueue(queue) {
  70. this.queue = queue;
  71. this.priorityQueue = [];
  72. const visited = new WeakSet();
  73. let stop = false;
  74. let visitIndex = 0;
  75. for (; visitIndex < queue.length;) {
  76. const path = queue[visitIndex];
  77. visitIndex++;
  78. path.resync();
  79. if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
  80. path.pushContext(this);
  81. }
  82. if (path.key === null) continue;
  83. const {
  84. node
  85. } = path;
  86. if (visited.has(node)) continue;
  87. if (node) visited.add(node);
  88. if (path.visit()) {
  89. stop = true;
  90. break;
  91. }
  92. if (this.priorityQueue.length) {
  93. stop = this.visitQueue(this.priorityQueue);
  94. this.priorityQueue = [];
  95. this.queue = queue;
  96. if (stop) break;
  97. }
  98. }
  99. for (let i = 0; i < visitIndex; i++) {
  100. queue[i].popContext();
  101. }
  102. this.queue = null;
  103. return stop;
  104. }
  105. visit(node, key) {
  106. const nodes = node[key];
  107. if (!nodes) return false;
  108. if (Array.isArray(nodes)) {
  109. return this.visitMultiple(nodes, node, key);
  110. } else {
  111. return this.visitSingle(node, key);
  112. }
  113. }
  114. }
  115. exports.default = TraversalContext;
  116. //# sourceMappingURL=context.js.map