context.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  6. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  7. var _path2 = require("./path");
  8. var _path3 = _interopRequireDefault(_path2);
  9. var _babelTypes = require("babel-types");
  10. var t = _interopRequireWildcard(_babelTypes);
  11. 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. var testing = process.env.NODE_ENV === "test";
  14. var TraversalContext = function () {
  15. function TraversalContext(scope, opts, state, parentPath) {
  16. (0, _classCallCheck3.default)(this, TraversalContext);
  17. this.queue = null;
  18. this.parentPath = parentPath;
  19. this.scope = scope;
  20. this.state = state;
  21. this.opts = opts;
  22. }
  23. TraversalContext.prototype.shouldVisit = function shouldVisit(node) {
  24. var opts = this.opts;
  25. if (opts.enter || opts.exit) return true;
  26. if (opts[node.type]) return true;
  27. var keys = t.VISITOR_KEYS[node.type];
  28. if (!keys || !keys.length) return false;
  29. for (var _iterator = keys, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  30. var _ref;
  31. if (_isArray) {
  32. if (_i >= _iterator.length) break;
  33. _ref = _iterator[_i++];
  34. } else {
  35. _i = _iterator.next();
  36. if (_i.done) break;
  37. _ref = _i.value;
  38. }
  39. var key = _ref;
  40. if (node[key]) return true;
  41. }
  42. return false;
  43. };
  44. TraversalContext.prototype.create = function create(node, obj, key, listKey) {
  45. return _path3.default.get({
  46. parentPath: this.parentPath,
  47. parent: node,
  48. container: obj,
  49. key: key,
  50. listKey: listKey
  51. });
  52. };
  53. TraversalContext.prototype.maybeQueue = function maybeQueue(path, notPriority) {
  54. if (this.trap) {
  55. throw new Error("Infinite cycle detected");
  56. }
  57. if (this.queue) {
  58. if (notPriority) {
  59. this.queue.push(path);
  60. } else {
  61. this.priorityQueue.push(path);
  62. }
  63. }
  64. };
  65. TraversalContext.prototype.visitMultiple = function visitMultiple(container, parent, listKey) {
  66. if (container.length === 0) return false;
  67. var queue = [];
  68. for (var key = 0; key < container.length; key++) {
  69. var node = container[key];
  70. if (node && this.shouldVisit(node)) {
  71. queue.push(this.create(parent, container, key, listKey));
  72. }
  73. }
  74. return this.visitQueue(queue);
  75. };
  76. TraversalContext.prototype.visitSingle = function visitSingle(node, key) {
  77. if (this.shouldVisit(node[key])) {
  78. return this.visitQueue([this.create(node, node, key)]);
  79. } else {
  80. return false;
  81. }
  82. };
  83. TraversalContext.prototype.visitQueue = function visitQueue(queue) {
  84. this.queue = queue;
  85. this.priorityQueue = [];
  86. var visited = [];
  87. var stop = false;
  88. for (var _iterator2 = queue, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  89. var _ref2;
  90. if (_isArray2) {
  91. if (_i2 >= _iterator2.length) break;
  92. _ref2 = _iterator2[_i2++];
  93. } else {
  94. _i2 = _iterator2.next();
  95. if (_i2.done) break;
  96. _ref2 = _i2.value;
  97. }
  98. var path = _ref2;
  99. path.resync();
  100. if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
  101. path.pushContext(this);
  102. }
  103. if (path.key === null) continue;
  104. if (testing && queue.length >= 10000) {
  105. this.trap = true;
  106. }
  107. if (visited.indexOf(path.node) >= 0) continue;
  108. visited.push(path.node);
  109. if (path.visit()) {
  110. stop = true;
  111. break;
  112. }
  113. if (this.priorityQueue.length) {
  114. stop = this.visitQueue(this.priorityQueue);
  115. this.priorityQueue = [];
  116. this.queue = queue;
  117. if (stop) break;
  118. }
  119. }
  120. for (var _iterator3 = queue, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  121. var _ref3;
  122. if (_isArray3) {
  123. if (_i3 >= _iterator3.length) break;
  124. _ref3 = _iterator3[_i3++];
  125. } else {
  126. _i3 = _iterator3.next();
  127. if (_i3.done) break;
  128. _ref3 = _i3.value;
  129. }
  130. var _path = _ref3;
  131. _path.popContext();
  132. }
  133. this.queue = null;
  134. return stop;
  135. };
  136. TraversalContext.prototype.visit = function visit(node, key) {
  137. var nodes = node[key];
  138. if (!nodes) return false;
  139. if (Array.isArray(nodes)) {
  140. return this.visitMultiple(nodes, node, key);
  141. } else {
  142. return this.visitSingle(node, key);
  143. }
  144. };
  145. return TraversalContext;
  146. }();
  147. exports.default = TraversalContext;
  148. module.exports = exports["default"];