context.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. exports.call = call;
  6. exports._call = _call;
  7. exports.isBlacklisted = isBlacklisted;
  8. exports.visit = visit;
  9. exports.skip = skip;
  10. exports.skipKey = skipKey;
  11. exports.stop = stop;
  12. exports.setScope = setScope;
  13. exports.setContext = setContext;
  14. exports.resync = resync;
  15. exports._resyncParent = _resyncParent;
  16. exports._resyncKey = _resyncKey;
  17. exports._resyncList = _resyncList;
  18. exports._resyncRemoved = _resyncRemoved;
  19. exports.popContext = popContext;
  20. exports.pushContext = pushContext;
  21. exports.setup = setup;
  22. exports.setKey = setKey;
  23. exports.requeue = requeue;
  24. exports._getQueueContexts = _getQueueContexts;
  25. var _index = require("../index");
  26. var _index2 = _interopRequireDefault(_index);
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  28. function call(key) {
  29. var opts = this.opts;
  30. this.debug(function () {
  31. return key;
  32. });
  33. if (this.node) {
  34. if (this._call(opts[key])) return true;
  35. }
  36. if (this.node) {
  37. return this._call(opts[this.node.type] && opts[this.node.type][key]);
  38. }
  39. return false;
  40. }
  41. function _call(fns) {
  42. if (!fns) return false;
  43. for (var _iterator = fns, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  44. var _ref;
  45. if (_isArray) {
  46. if (_i >= _iterator.length) break;
  47. _ref = _iterator[_i++];
  48. } else {
  49. _i = _iterator.next();
  50. if (_i.done) break;
  51. _ref = _i.value;
  52. }
  53. var fn = _ref;
  54. if (!fn) continue;
  55. var node = this.node;
  56. if (!node) return true;
  57. var ret = fn.call(this.state, this, this.state);
  58. if (ret) throw new Error("Unexpected return value from visitor method " + fn);
  59. if (this.node !== node) return true;
  60. if (this.shouldStop || this.shouldSkip || this.removed) return true;
  61. }
  62. return false;
  63. }
  64. function isBlacklisted() {
  65. var blacklist = this.opts.blacklist;
  66. return blacklist && blacklist.indexOf(this.node.type) > -1;
  67. }
  68. function visit() {
  69. if (!this.node) {
  70. return false;
  71. }
  72. if (this.isBlacklisted()) {
  73. return false;
  74. }
  75. if (this.opts.shouldSkip && this.opts.shouldSkip(this)) {
  76. return false;
  77. }
  78. if (this.call("enter") || this.shouldSkip) {
  79. this.debug(function () {
  80. return "Skip...";
  81. });
  82. return this.shouldStop;
  83. }
  84. this.debug(function () {
  85. return "Recursing into...";
  86. });
  87. _index2.default.node(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
  88. this.call("exit");
  89. return this.shouldStop;
  90. }
  91. function skip() {
  92. this.shouldSkip = true;
  93. }
  94. function skipKey(key) {
  95. this.skipKeys[key] = true;
  96. }
  97. function stop() {
  98. this.shouldStop = true;
  99. this.shouldSkip = true;
  100. }
  101. function setScope() {
  102. if (this.opts && this.opts.noScope) return;
  103. var target = this.context && this.context.scope;
  104. if (!target) {
  105. var path = this.parentPath;
  106. while (path && !target) {
  107. if (path.opts && path.opts.noScope) return;
  108. target = path.scope;
  109. path = path.parentPath;
  110. }
  111. }
  112. this.scope = this.getScope(target);
  113. if (this.scope) this.scope.init();
  114. }
  115. function setContext(context) {
  116. this.shouldSkip = false;
  117. this.shouldStop = false;
  118. this.removed = false;
  119. this.skipKeys = {};
  120. if (context) {
  121. this.context = context;
  122. this.state = context.state;
  123. this.opts = context.opts;
  124. }
  125. this.setScope();
  126. return this;
  127. }
  128. function resync() {
  129. if (this.removed) return;
  130. this._resyncParent();
  131. this._resyncList();
  132. this._resyncKey();
  133. }
  134. function _resyncParent() {
  135. if (this.parentPath) {
  136. this.parent = this.parentPath.node;
  137. }
  138. }
  139. function _resyncKey() {
  140. if (!this.container) return;
  141. if (this.node === this.container[this.key]) return;
  142. if (Array.isArray(this.container)) {
  143. for (var i = 0; i < this.container.length; i++) {
  144. if (this.container[i] === this.node) {
  145. return this.setKey(i);
  146. }
  147. }
  148. } else {
  149. for (var key in this.container) {
  150. if (this.container[key] === this.node) {
  151. return this.setKey(key);
  152. }
  153. }
  154. }
  155. this.key = null;
  156. }
  157. function _resyncList() {
  158. if (!this.parent || !this.inList) return;
  159. var newContainer = this.parent[this.listKey];
  160. if (this.container === newContainer) return;
  161. this.container = newContainer || null;
  162. }
  163. function _resyncRemoved() {
  164. if (this.key == null || !this.container || this.container[this.key] !== this.node) {
  165. this._markRemoved();
  166. }
  167. }
  168. function popContext() {
  169. this.contexts.pop();
  170. this.setContext(this.contexts[this.contexts.length - 1]);
  171. }
  172. function pushContext(context) {
  173. this.contexts.push(context);
  174. this.setContext(context);
  175. }
  176. function setup(parentPath, container, listKey, key) {
  177. this.inList = !!listKey;
  178. this.listKey = listKey;
  179. this.parentKey = listKey || key;
  180. this.container = container;
  181. this.parentPath = parentPath || this.parentPath;
  182. this.setKey(key);
  183. }
  184. function setKey(key) {
  185. this.key = key;
  186. this.node = this.container[this.key];
  187. this.type = this.node && this.node.type;
  188. }
  189. function requeue() {
  190. var pathToQueue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this;
  191. if (pathToQueue.removed) return;
  192. var contexts = this.contexts;
  193. for (var _iterator2 = contexts, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  194. var _ref2;
  195. if (_isArray2) {
  196. if (_i2 >= _iterator2.length) break;
  197. _ref2 = _iterator2[_i2++];
  198. } else {
  199. _i2 = _iterator2.next();
  200. if (_i2.done) break;
  201. _ref2 = _i2.value;
  202. }
  203. var context = _ref2;
  204. context.maybeQueue(pathToQueue);
  205. }
  206. }
  207. function _getQueueContexts() {
  208. var path = this;
  209. var contexts = this.contexts;
  210. while (!contexts.length) {
  211. path = path.parentPath;
  212. contexts = path.contexts;
  213. }
  214. return contexts;
  215. }