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.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 = t.VISITOR_KEYS[node.type];
  26. if (!(keys != null && keys.length)) return false;
  27. for (const key of keys) {
  28. if (node[key]) return true;
  29. }
  30. return false;
  31. }
  32. create(node, obj, key, listKey) {
  33. return _path.default.get({
  34. parentPath: this.parentPath,
  35. parent: node,
  36. container: obj,
  37. key: key,
  38. listKey
  39. });
  40. }
  41. maybeQueue(path, notPriority) {
  42. if (this.trap) {
  43. throw new Error("Infinite cycle detected");
  44. }
  45. if (this.queue) {
  46. if (notPriority) {
  47. this.queue.push(path);
  48. } else {
  49. this.priorityQueue.push(path);
  50. }
  51. }
  52. }
  53. visitMultiple(container, parent, listKey) {
  54. if (container.length === 0) return false;
  55. const queue = [];
  56. for (let key = 0; key < container.length; key++) {
  57. const node = container[key];
  58. if (node && this.shouldVisit(node)) {
  59. queue.push(this.create(parent, container, key, listKey));
  60. }
  61. }
  62. return this.visitQueue(queue);
  63. }
  64. visitSingle(node, key) {
  65. if (this.shouldVisit(node[key])) {
  66. return this.visitQueue([this.create(node, node, key)]);
  67. } else {
  68. return false;
  69. }
  70. }
  71. visitQueue(queue) {
  72. this.queue = queue;
  73. this.priorityQueue = [];
  74. const visited = new WeakSet();
  75. let stop = false;
  76. for (const path of queue) {
  77. path.resync();
  78. if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
  79. path.pushContext(this);
  80. }
  81. if (path.key === null) continue;
  82. if (testing && queue.length >= 10000) {
  83. this.trap = true;
  84. }
  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 (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;