context.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. const {
  10. VISITOR_KEYS
  11. } = _t;
  12. class TraversalContext {
  13. constructor(scope, opts, state, parentPath) {
  14. this.queue = null;
  15. this.priorityQueue = null;
  16. this.parentPath = parentPath;
  17. this.scope = scope;
  18. this.state = state;
  19. this.opts = opts;
  20. }
  21. shouldVisit(node) {
  22. const opts = this.opts;
  23. if (opts.enter || opts.exit) return true;
  24. if (opts[node.type]) return true;
  25. const keys = VISITOR_KEYS[node.type];
  26. if (!(keys != null && keys.length)) return false;
  27. for (const key of keys) {
  28. if (node[key]) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. create(node, container, key, listKey) {
  35. return _index.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(node[key])) {
  65. return this.visitQueue([this.create(node, node, key)]);
  66. } else {
  67. return false;
  68. }
  69. }
  70. visitQueue(queue) {
  71. this.queue = queue;
  72. this.priorityQueue = [];
  73. const visited = new WeakSet();
  74. let stop = false;
  75. let visitIndex = 0;
  76. for (; visitIndex < queue.length;) {
  77. const path = queue[visitIndex];
  78. visitIndex++;
  79. _context.resync.call(path);
  80. ;
  81. if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
  82. _context.pushContext.call(path, this);
  83. }
  84. if (path.key === null) continue;
  85. const {
  86. node
  87. } = path;
  88. if (visited.has(node)) continue;
  89. if (node) visited.add(node);
  90. if (path.visit()) {
  91. stop = true;
  92. break;
  93. }
  94. if (this.priorityQueue.length) {
  95. stop = this.visitQueue(this.priorityQueue);
  96. this.priorityQueue = [];
  97. this.queue = queue;
  98. if (stop) break;
  99. }
  100. }
  101. for (let i = 0; i < visitIndex; i++) {
  102. ;
  103. _context.popContext.call(queue[i]);
  104. }
  105. this.queue = null;
  106. return stop;
  107. }
  108. visit(node, key) {
  109. const nodes = node[key];
  110. if (!nodes) return false;
  111. if (Array.isArray(nodes)) {
  112. return this.visitMultiple(nodes, node, key);
  113. } else {
  114. return this.visitSingle(node, key);
  115. }
  116. }
  117. }
  118. exports.default = TraversalContext;
  119. //# sourceMappingURL=context.js.map