You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.9 KiB

4 years ago
  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.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 = t.VISITOR_KEYS[node.type];
  25. if (!keys || !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.trap) {
  42. throw new Error("Infinite cycle detected");
  43. }
  44. if (this.queue) {
  45. if (notPriority) {
  46. this.queue.push(path);
  47. } else {
  48. this.priorityQueue.push(path);
  49. }
  50. }
  51. }
  52. visitMultiple(container, parent, listKey) {
  53. if (container.length === 0) return false;
  54. const queue = [];
  55. for (let key = 0; key < container.length; key++) {
  56. const node = container[key];
  57. if (node && this.shouldVisit(node)) {
  58. queue.push(this.create(parent, container, key, listKey));
  59. }
  60. }
  61. return this.visitQueue(queue);
  62. }
  63. visitSingle(node, key) {
  64. if (this.shouldVisit(node[key])) {
  65. return this.visitQueue([this.create(node, node, key)]);
  66. } else {
  67. return false;
  68. }
  69. }
  70. visitQueue(queue) {
  71. this.queue = queue;
  72. this.priorityQueue = [];
  73. const visited = [];
  74. let stop = false;
  75. for (const path of queue) {
  76. path.resync();
  77. if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
  78. path.pushContext(this);
  79. }
  80. if (path.key === null) continue;
  81. if (testing && queue.length >= 10000) {
  82. this.trap = true;
  83. }
  84. if (visited.indexOf(path.node) >= 0) continue;
  85. visited.push(path.node);
  86. if (path.visit()) {
  87. stop = true;
  88. break;
  89. }
  90. if (this.priorityQueue.length) {
  91. stop = this.visitQueue(this.priorityQueue);
  92. this.priorityQueue = [];
  93. this.queue = queue;
  94. if (stop) break;
  95. }
  96. }
  97. for (const path of queue) {
  98. path.popContext();
  99. }
  100. this.queue = null;
  101. return stop;
  102. }
  103. visit(node, key) {
  104. const nodes = node[key];
  105. if (!nodes) return false;
  106. if (Array.isArray(nodes)) {
  107. return this.visitMultiple(nodes, node, key);
  108. } else {
  109. return this.visitSingle(node, key);
  110. }
  111. }
  112. }
  113. exports.default = TraversalContext;