context.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.call = call;
  6. exports._call = _call;
  7. exports.isBlacklisted = exports.isDenylisted = isDenylisted;
  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. var _index2 = require("./index");
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  28. function call(key) {
  29. const opts = this.opts;
  30. this.debug(key);
  31. if (this.node) {
  32. if (this._call(opts[key])) return true;
  33. }
  34. if (this.node) {
  35. return this._call(opts[this.node.type] && opts[this.node.type][key]);
  36. }
  37. return false;
  38. }
  39. function _call(fns) {
  40. if (!fns) return false;
  41. for (const fn of fns) {
  42. if (!fn) continue;
  43. const node = this.node;
  44. if (!node) return true;
  45. const ret = fn.call(this.state, this, this.state);
  46. if (ret && typeof ret === "object" && typeof ret.then === "function") {
  47. 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.`);
  48. }
  49. if (ret) {
  50. throw new Error(`Unexpected return value from visitor method ${fn}`);
  51. }
  52. if (this.node !== node) return true;
  53. if (this._traverseFlags > 0) return true;
  54. }
  55. return false;
  56. }
  57. function isDenylisted() {
  58. var _this$opts$denylist;
  59. const denylist = (_this$opts$denylist = this.opts.denylist) != null ? _this$opts$denylist : this.opts.blacklist;
  60. return denylist && denylist.indexOf(this.node.type) > -1;
  61. }
  62. function visit() {
  63. if (!this.node) {
  64. return false;
  65. }
  66. if (this.isDenylisted()) {
  67. return false;
  68. }
  69. if (this.opts.shouldSkip && this.opts.shouldSkip(this)) {
  70. return false;
  71. }
  72. if (this.shouldSkip || this.call("enter") || this.shouldSkip) {
  73. this.debug("Skip...");
  74. return this.shouldStop;
  75. }
  76. this.debug("Recursing into...");
  77. _index.default.node(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
  78. this.call("exit");
  79. return this.shouldStop;
  80. }
  81. function skip() {
  82. this.shouldSkip = true;
  83. }
  84. function skipKey(key) {
  85. if (this.skipKeys == null) {
  86. this.skipKeys = {};
  87. }
  88. this.skipKeys[key] = true;
  89. }
  90. function stop() {
  91. this._traverseFlags |= _index2.SHOULD_SKIP | _index2.SHOULD_STOP;
  92. }
  93. function setScope() {
  94. if (this.opts && this.opts.noScope) return;
  95. let path = this.parentPath;
  96. if (this.key === "key" && path.isMethod()) path = path.parentPath;
  97. let target;
  98. while (path && !target) {
  99. if (path.opts && path.opts.noScope) return;
  100. target = path.scope;
  101. path = path.parentPath;
  102. }
  103. this.scope = this.getScope(target);
  104. if (this.scope) this.scope.init();
  105. }
  106. function setContext(context) {
  107. if (this.skipKeys != null) {
  108. this.skipKeys = {};
  109. }
  110. this._traverseFlags = 0;
  111. if (context) {
  112. this.context = context;
  113. this.state = context.state;
  114. this.opts = context.opts;
  115. }
  116. this.setScope();
  117. return this;
  118. }
  119. function resync() {
  120. if (this.removed) return;
  121. this._resyncParent();
  122. this._resyncList();
  123. this._resyncKey();
  124. }
  125. function _resyncParent() {
  126. if (this.parentPath) {
  127. this.parent = this.parentPath.node;
  128. }
  129. }
  130. function _resyncKey() {
  131. if (!this.container) return;
  132. if (this.node === this.container[this.key]) return;
  133. if (Array.isArray(this.container)) {
  134. for (let i = 0; i < this.container.length; i++) {
  135. if (this.container[i] === this.node) {
  136. return this.setKey(i);
  137. }
  138. }
  139. } else {
  140. for (const key of Object.keys(this.container)) {
  141. if (this.container[key] === this.node) {
  142. return this.setKey(key);
  143. }
  144. }
  145. }
  146. this.key = null;
  147. }
  148. function _resyncList() {
  149. if (!this.parent || !this.inList) return;
  150. const newContainer = this.parent[this.listKey];
  151. if (this.container === newContainer) return;
  152. this.container = newContainer || null;
  153. }
  154. function _resyncRemoved() {
  155. if (this.key == null || !this.container || this.container[this.key] !== this.node) {
  156. this._markRemoved();
  157. }
  158. }
  159. function popContext() {
  160. this.contexts.pop();
  161. if (this.contexts.length > 0) {
  162. this.setContext(this.contexts[this.contexts.length - 1]);
  163. } else {
  164. this.setContext(undefined);
  165. }
  166. }
  167. function pushContext(context) {
  168. this.contexts.push(context);
  169. this.setContext(context);
  170. }
  171. function setup(parentPath, container, listKey, key) {
  172. this.listKey = listKey;
  173. this.container = container;
  174. this.parentPath = parentPath || this.parentPath;
  175. this.setKey(key);
  176. }
  177. function setKey(key) {
  178. var _this$node;
  179. this.key = key;
  180. this.node = this.container[this.key];
  181. this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
  182. }
  183. function requeue(pathToQueue = this) {
  184. if (pathToQueue.removed) return;
  185. const contexts = this.contexts;
  186. for (const context of contexts) {
  187. context.maybeQueue(pathToQueue);
  188. }
  189. }
  190. function _getQueueContexts() {
  191. let path = this;
  192. let contexts = this.contexts;
  193. while (!contexts.length) {
  194. path = path.parentPath;
  195. if (!path) break;
  196. contexts = path.contexts;
  197. }
  198. return contexts;
  199. }