context.js 2.7 KB

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