context.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  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 = _interopRequireDefault(require("../index"));
  26. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  27. function call(key) {
  28. var opts = this.opts;
  29. this.debug(key);
  30. if (this.node) {
  31. if (this._call(opts[key])) return true;
  32. }
  33. if (this.node) {
  34. return this._call(opts[this.node.type] && opts[this.node.type][key]);
  35. }
  36. return false;
  37. }
  38. function _call(fns) {
  39. if (!fns) return false;
  40. for (var _iterator = fns, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  41. var _ref;
  42. if (_isArray) {
  43. if (_i >= _iterator.length) break;
  44. _ref = _iterator[_i++];
  45. } else {
  46. _i = _iterator.next();
  47. if (_i.done) break;
  48. _ref = _i.value;
  49. }
  50. var fn = _ref;
  51. if (!fn) continue;
  52. var node = this.node;
  53. if (!node) return true;
  54. var ret = fn.call(this.state, this, this.state);
  55. if (ret && typeof ret === "object" && typeof ret.then === "function") {
  56. throw new Error("You appear to be using a plugin with an async traversal visitor, " + "which your current version of Babel does not support." + "If you're using a published plugin, you may need to upgrade " + "your @babel/core version.");
  57. }
  58. if (ret) {
  59. throw new Error("Unexpected return value from visitor method " + fn);
  60. }
  61. if (this.node !== node) return true;
  62. if (this.shouldStop || this.shouldSkip || this.removed) return true;
  63. }
  64. return false;
  65. }
  66. function isBlacklisted() {
  67. var blacklist = this.opts.blacklist;
  68. return blacklist && blacklist.indexOf(this.node.type) > -1;
  69. }
  70. function visit() {
  71. if (!this.node) {
  72. return false;
  73. }
  74. if (this.isBlacklisted()) {
  75. return false;
  76. }
  77. if (this.opts.shouldSkip && this.opts.shouldSkip(this)) {
  78. return false;
  79. }
  80. if (this.call("enter") || this.shouldSkip) {
  81. this.debug("Skip...");
  82. return this.shouldStop;
  83. }
  84. this.debug("Recursing into...");
  85. _index.default.node(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
  86. this.call("exit");
  87. return this.shouldStop;
  88. }
  89. function skip() {
  90. this.shouldSkip = true;
  91. }
  92. function skipKey(key) {
  93. this.skipKeys[key] = true;
  94. }
  95. function stop() {
  96. this.shouldStop = true;
  97. this.shouldSkip = true;
  98. }
  99. function setScope() {
  100. if (this.opts && this.opts.noScope) return;
  101. var path = this.parentPath;
  102. var target;
  103. while (path && !target) {
  104. if (path.opts && path.opts.noScope) return;
  105. target = path.scope;
  106. path = path.parentPath;
  107. }
  108. this.scope = this.getScope(target);
  109. if (this.scope) this.scope.init();
  110. }
  111. function setContext(context) {
  112. this.shouldSkip = false;
  113. this.shouldStop = false;
  114. this.removed = false;
  115. this.skipKeys = {};
  116. if (context) {
  117. this.context = context;
  118. this.state = context.state;
  119. this.opts = context.opts;
  120. }
  121. this.setScope();
  122. return this;
  123. }
  124. function resync() {
  125. if (this.removed) return;
  126. this._resyncParent();
  127. this._resyncList();
  128. this._resyncKey();
  129. }
  130. function _resyncParent() {
  131. if (this.parentPath) {
  132. this.parent = this.parentPath.node;
  133. }
  134. }
  135. function _resyncKey() {
  136. if (!this.container) return;
  137. if (this.node === this.container[this.key]) return;
  138. if (Array.isArray(this.container)) {
  139. for (var i = 0; i < this.container.length; i++) {
  140. if (this.container[i] === this.node) {
  141. return this.setKey(i);
  142. }
  143. }
  144. } else {
  145. for (var key in this.container) {
  146. if (this.container[key] === this.node) {
  147. return this.setKey(key);
  148. }
  149. }
  150. }
  151. this.key = null;
  152. }
  153. function _resyncList() {
  154. if (!this.parent || !this.inList) return;
  155. var newContainer = this.parent[this.listKey];
  156. if (this.container === newContainer) return;
  157. this.container = newContainer || null;
  158. }
  159. function _resyncRemoved() {
  160. if (this.key == null || !this.container || this.container[this.key] !== this.node) {
  161. this._markRemoved();
  162. }
  163. }
  164. function popContext() {
  165. this.contexts.pop();
  166. if (this.contexts.length > 0) {
  167. this.setContext(this.contexts[this.contexts.length - 1]);
  168. } else {
  169. this.setContext(undefined);
  170. }
  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(pathToQueue) {
  190. if (pathToQueue === void 0) {
  191. pathToQueue = this;
  192. }
  193. if (pathToQueue.removed) return;
  194. var contexts = this.contexts;
  195. for (var _iterator2 = contexts, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  196. var _ref2;
  197. if (_isArray2) {
  198. if (_i2 >= _iterator2.length) break;
  199. _ref2 = _iterator2[_i2++];
  200. } else {
  201. _i2 = _iterator2.next();
  202. if (_i2.done) break;
  203. _ref2 = _i2.value;
  204. }
  205. var context = _ref2;
  206. context.maybeQueue(pathToQueue);
  207. }
  208. }
  209. function _getQueueContexts() {
  210. var path = this;
  211. var contexts = this.contexts;
  212. while (!contexts.length) {
  213. path = path.parentPath;
  214. if (!path) break;
  215. contexts = path.contexts;
  216. }
  217. return contexts;
  218. }