context.js 4.0 KB

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