context.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 (
  106. (this.key === "key" || this.listKey === "decorators") && path.isMethod() ||
  107. this.key === "discriminant" && path.isSwitchStatement()) {
  108. path = path.parentPath;
  109. }
  110. let target;
  111. while (path && !target) {
  112. if (path.opts && path.opts.noScope) return;
  113. target = path.scope;
  114. path = path.parentPath;
  115. }
  116. this.scope = this.getScope(target);
  117. if (this.scope) this.scope.init();
  118. }
  119. function setContext(context) {
  120. if (this.skipKeys != null) {
  121. this.skipKeys = {};
  122. }
  123. this._traverseFlags = 0;
  124. if (context) {
  125. this.context = context;
  126. this.state = context.state;
  127. this.opts = context.opts;
  128. }
  129. this.setScope();
  130. return this;
  131. }
  132. function resync() {
  133. if (this.removed) return;
  134. this._resyncParent();
  135. this._resyncList();
  136. this._resyncKey();
  137. }
  138. function _resyncParent() {
  139. if (this.parentPath) {
  140. this.parent = this.parentPath.node;
  141. }
  142. }
  143. function _resyncKey() {
  144. if (!this.container) return;
  145. if (this.node ===
  146. this.container[this.key]) {
  147. return;
  148. }
  149. if (Array.isArray(this.container)) {
  150. for (let i = 0; i < this.container.length; i++) {
  151. if (this.container[i] === this.node) {
  152. return this.setKey(i);
  153. }
  154. }
  155. } else {
  156. for (const key of Object.keys(this.container)) {
  157. if (this.container[key] === this.node) {
  158. return this.setKey(key);
  159. }
  160. }
  161. }
  162. this.key = null;
  163. }
  164. function _resyncList() {
  165. if (!this.parent || !this.inList) return;
  166. const newContainer =
  167. this.parent[this.listKey];
  168. if (this.container === newContainer) return;
  169. this.container = newContainer || null;
  170. }
  171. function _resyncRemoved() {
  172. if (this.key == null || !this.container ||
  173. this.container[this.key] !== this.node) {
  174. this._markRemoved();
  175. }
  176. }
  177. function popContext() {
  178. this.contexts.pop();
  179. if (this.contexts.length > 0) {
  180. this.setContext(this.contexts[this.contexts.length - 1]);
  181. } else {
  182. this.setContext(undefined);
  183. }
  184. }
  185. function pushContext(context) {
  186. this.contexts.push(context);
  187. this.setContext(context);
  188. }
  189. function setup(parentPath, container, listKey, key) {
  190. this.listKey = listKey;
  191. this.container = container;
  192. this.parentPath = parentPath || this.parentPath;
  193. this.setKey(key);
  194. }
  195. function setKey(key) {
  196. var _this$node;
  197. this.key = key;
  198. this.node =
  199. this.container[this.key];
  200. this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
  201. }
  202. function requeue(pathToQueue = this) {
  203. if (pathToQueue.removed) return;
  204. ;
  205. const contexts = this.contexts;
  206. for (const context of contexts) {
  207. context.maybeQueue(pathToQueue);
  208. }
  209. }
  210. function _getQueueContexts() {
  211. let path = this;
  212. let contexts = this.contexts;
  213. while (!contexts.length) {
  214. path = path.parentPath;
  215. if (!path) break;
  216. contexts = path.contexts;
  217. }
  218. return contexts;
  219. }
  220. //# sourceMappingURL=context.js.map