context.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = _interopRequireDefault(require("./path"));
  7. function t() {
  8. const data = _interopRequireWildcard(require("@babel/types"));
  9. t = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. const testing = process.env.NODE_ENV === "test";
  17. class TraversalContext {
  18. constructor(scope, opts, state, parentPath) {
  19. this.queue = null;
  20. this.parentPath = parentPath;
  21. this.scope = scope;
  22. this.state = state;
  23. this.opts = opts;
  24. }
  25. shouldVisit(node) {
  26. const opts = this.opts;
  27. if (opts.enter || opts.exit) return true;
  28. if (opts[node.type]) return true;
  29. const keys = t().VISITOR_KEYS[node.type];
  30. if (!keys || !keys.length) return false;
  31. for (const key of keys) {
  32. if (node[key]) return true;
  33. }
  34. return false;
  35. }
  36. create(node, obj, key, listKey) {
  37. return _path.default.get({
  38. parentPath: this.parentPath,
  39. parent: node,
  40. container: obj,
  41. key: key,
  42. listKey
  43. });
  44. }
  45. maybeQueue(path, notPriority) {
  46. if (this.trap) {
  47. throw new Error("Infinite cycle detected");
  48. }
  49. if (this.queue) {
  50. if (notPriority) {
  51. this.queue.push(path);
  52. } else {
  53. this.priorityQueue.push(path);
  54. }
  55. }
  56. }
  57. visitMultiple(container, parent, listKey) {
  58. if (container.length === 0) return false;
  59. const queue = [];
  60. for (let key = 0; key < container.length; key++) {
  61. const node = container[key];
  62. if (node && this.shouldVisit(node)) {
  63. queue.push(this.create(parent, container, key, listKey));
  64. }
  65. }
  66. return this.visitQueue(queue);
  67. }
  68. visitSingle(node, key) {
  69. if (this.shouldVisit(node[key])) {
  70. return this.visitQueue([this.create(node, node, key)]);
  71. } else {
  72. return false;
  73. }
  74. }
  75. visitQueue(queue) {
  76. this.queue = queue;
  77. this.priorityQueue = [];
  78. const visited = [];
  79. let stop = false;
  80. for (const path of queue) {
  81. path.resync();
  82. if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
  83. path.pushContext(this);
  84. }
  85. if (path.key === null) continue;
  86. if (testing && queue.length >= 10000) {
  87. this.trap = true;
  88. }
  89. if (visited.indexOf(path.node) >= 0) continue;
  90. visited.push(path.node);
  91. if (path.visit()) {
  92. stop = true;
  93. break;
  94. }
  95. if (this.priorityQueue.length) {
  96. stop = this.visitQueue(this.priorityQueue);
  97. this.priorityQueue = [];
  98. this.queue = queue;
  99. if (stop) break;
  100. }
  101. }
  102. for (const path of queue) {
  103. path.popContext();
  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;