context.js 3.2 KB

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