context.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports._call = _call;
  6. exports._getQueueContexts = _getQueueContexts;
  7. exports._resyncKey = _resyncKey;
  8. exports._resyncList = _resyncList;
  9. exports._resyncParent = _resyncParent;
  10. exports._resyncRemoved = _resyncRemoved;
  11. exports.call = call;
  12. exports.isBlacklisted = exports.isDenylisted = isDenylisted;
  13. exports.popContext = popContext;
  14. exports.pushContext = pushContext;
  15. exports.requeue = requeue;
  16. exports.resync = resync;
  17. exports.setContext = setContext;
  18. exports.setKey = setKey;
  19. exports.setScope = setScope;
  20. exports.setup = setup;
  21. exports.skip = skip;
  22. exports.skipKey = skipKey;
  23. exports.stop = stop;
  24. exports.visit = visit;
  25. var _traverseNode = require("../traverse-node");
  26. var _index = require("./index");
  27. function call(key) {
  28. const 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 (const fn of fns) {
  41. if (!fn) continue;
  42. const node = this.node;
  43. if (!node) return true;
  44. const ret = fn.call(this.state, this, this.state);
  45. if (ret && typeof ret === "object" && typeof ret.then === "function") {
  46. 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.`);
  47. }
  48. if (ret) {
  49. throw new Error(`Unexpected return value from visitor method ${fn}`);
  50. }
  51. if (this.node !== node) return true;
  52. if (this._traverseFlags > 0) return true;
  53. }
  54. return false;
  55. }
  56. function isDenylisted() {
  57. var _this$opts$denylist;
  58. const denylist = (_this$opts$denylist = this.opts.denylist) != null ? _this$opts$denylist : this.opts.blacklist;
  59. return denylist && denylist.indexOf(this.node.type) > -1;
  60. }
  61. function restoreContext(path, context) {
  62. if (path.context !== context) {
  63. path.context = context;
  64. path.state = context.state;
  65. path.opts = context.opts;
  66. }
  67. }
  68. function visit() {
  69. if (!this.node) {
  70. return false;
  71. }
  72. if (this.isDenylisted()) {
  73. return false;
  74. }
  75. if (this.opts.shouldSkip && this.opts.shouldSkip(this)) {
  76. return false;
  77. }
  78. const currentContext = this.context;
  79. if (this.shouldSkip || this.call("enter")) {
  80. this.debug("Skip...");
  81. return this.shouldStop;
  82. }
  83. restoreContext(this, currentContext);
  84. this.debug("Recursing into...");
  85. this.shouldStop = (0, _traverseNode.traverseNode)(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
  86. restoreContext(this, currentContext);
  87. this.call("exit");
  88. return this.shouldStop;
  89. }
  90. function skip() {
  91. this.shouldSkip = true;
  92. }
  93. function skipKey(key) {
  94. if (this.skipKeys == null) {
  95. this.skipKeys = {};
  96. }
  97. this.skipKeys[key] = true;
  98. }
  99. function stop() {
  100. this._traverseFlags |= _index.SHOULD_SKIP | _index.SHOULD_STOP;
  101. }
  102. function setScope() {
  103. if (this.opts && this.opts.noScope) return;
  104. let path = this.parentPath;
  105. if (this.key === "key" && path.isMethod()) path = path.parentPath;
  106. let target;
  107. while (path && !target) {
  108. if (path.opts && path.opts.noScope) return;
  109. target = path.scope;
  110. path = path.parentPath;
  111. }
  112. this.scope = this.getScope(target);
  113. if (this.scope) this.scope.init();
  114. }
  115. function setContext(context) {
  116. if (this.skipKeys != null) {
  117. this.skipKeys = {};
  118. }
  119. this._traverseFlags = 0;
  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 (let 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 (const key of Object.keys(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. const 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. if (this.contexts.length > 0) {
  171. this.setContext(this.contexts[this.contexts.length - 1]);
  172. } else {
  173. this.setContext(undefined);
  174. }
  175. }
  176. function pushContext(context) {
  177. this.contexts.push(context);
  178. this.setContext(context);
  179. }
  180. function setup(parentPath, container, listKey, key) {
  181. this.listKey = listKey;
  182. this.container = container;
  183. this.parentPath = parentPath || this.parentPath;
  184. this.setKey(key);
  185. }
  186. function setKey(key) {
  187. var _this$node;
  188. this.key = key;
  189. this.node = this.container[this.key];
  190. this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
  191. }
  192. function requeue(pathToQueue = this) {
  193. if (pathToQueue.removed) return;
  194. ;
  195. const contexts = this.contexts;
  196. for (const context of contexts) {
  197. context.maybeQueue(pathToQueue);
  198. }
  199. }
  200. function _getQueueContexts() {
  201. let path = this;
  202. let contexts = this.contexts;
  203. while (!contexts.length) {
  204. path = path.parentPath;
  205. if (!path) break;
  206. contexts = path.contexts;
  207. }
  208. return contexts;
  209. }