context.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 (node[key]) {
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. create(node, container, key, listKey) {
  34. return _path.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. for (const path of queue) {
  75. path.resync();
  76. if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
  77. path.pushContext(this);
  78. }
  79. if (path.key === null) continue;
  80. const {
  81. node
  82. } = path;
  83. if (visited.has(node)) continue;
  84. if (node) visited.add(node);
  85. if (path.visit()) {
  86. stop = true;
  87. break;
  88. }
  89. if (this.priorityQueue.length) {
  90. stop = this.visitQueue(this.priorityQueue);
  91. this.priorityQueue = [];
  92. this.queue = queue;
  93. if (stop) break;
  94. }
  95. }
  96. for (const path of queue) {
  97. path.popContext();
  98. }
  99. this.queue = null;
  100. return stop;
  101. }
  102. visit(node, key) {
  103. const nodes = node[key];
  104. if (!nodes) return false;
  105. if (Array.isArray(nodes)) {
  106. return this.visitMultiple(nodes, node, key);
  107. } else {
  108. return this.visitSingle(node, key);
  109. }
  110. }
  111. }
  112. exports.default = TraversalContext;
  113. //# sourceMappingURL=context.js.map