context.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports._call = _call;
  6. exports._forceSetScope = _forceSetScope;
  7. exports._getQueueContexts = _getQueueContexts;
  8. exports._resyncKey = _resyncKey;
  9. exports._resyncList = _resyncList;
  10. exports._resyncParent = _resyncParent;
  11. exports._resyncRemoved = _resyncRemoved;
  12. exports.call = call;
  13. exports.isDenylisted = isDenylisted;
  14. exports.popContext = popContext;
  15. exports.pushContext = pushContext;
  16. exports.requeue = requeue;
  17. exports.requeueComputedKeyAndDecorators = requeueComputedKeyAndDecorators;
  18. exports.resync = resync;
  19. exports.setContext = setContext;
  20. exports.setKey = setKey;
  21. exports.setScope = setScope;
  22. exports.setup = setup;
  23. exports.skip = skip;
  24. exports.skipKey = skipKey;
  25. exports.stop = stop;
  26. exports.visit = visit;
  27. var _traverseNode = require("../traverse-node.js");
  28. var _index = require("./index.js");
  29. var _removal = require("./removal.js");
  30. var t = require("@babel/types");
  31. function call(key) {
  32. const opts = this.opts;
  33. this.debug(key);
  34. if (this.node) {
  35. if (_call.call(this, opts[key])) return true;
  36. }
  37. if (this.node) {
  38. var _opts$this$node$type;
  39. return _call.call(this, (_opts$this$node$type = opts[this.node.type]) == null ? void 0 : _opts$this$node$type[key]);
  40. }
  41. return false;
  42. }
  43. function _call(fns) {
  44. if (!fns) return false;
  45. for (const fn of fns) {
  46. if (!fn) continue;
  47. const node = this.node;
  48. if (!node) return true;
  49. const ret = fn.call(this.state, this, this.state);
  50. if (ret && typeof ret === "object" && typeof ret.then === "function") {
  51. 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.`);
  52. }
  53. if (ret) {
  54. throw new Error(`Unexpected return value from visitor method ${fn}`);
  55. }
  56. if (this.node !== node) return true;
  57. if (this._traverseFlags > 0) return true;
  58. }
  59. return false;
  60. }
  61. function isDenylisted() {
  62. var _this$opts$denylist;
  63. const denylist = (_this$opts$denylist = this.opts.denylist) != null ? _this$opts$denylist : this.opts.blacklist;
  64. return denylist == null ? void 0 : denylist.includes(this.node.type);
  65. }
  66. exports.isBlacklisted = isDenylisted;
  67. function restoreContext(path, context) {
  68. if (path.context !== context) {
  69. path.context = context;
  70. path.state = context.state;
  71. path.opts = context.opts;
  72. }
  73. }
  74. function visit() {
  75. var _this$opts$shouldSkip, _this$opts;
  76. if (!this.node) {
  77. return false;
  78. }
  79. if (this.isDenylisted()) {
  80. return false;
  81. }
  82. if ((_this$opts$shouldSkip = (_this$opts = this.opts).shouldSkip) != null && _this$opts$shouldSkip.call(_this$opts, this)) {
  83. return false;
  84. }
  85. const currentContext = this.context;
  86. if (this.shouldSkip || call.call(this, "enter")) {
  87. this.debug("Skip...");
  88. return this.shouldStop;
  89. }
  90. restoreContext(this, currentContext);
  91. this.debug("Recursing into...");
  92. this.shouldStop = (0, _traverseNode.traverseNode)(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
  93. restoreContext(this, currentContext);
  94. call.call(this, "exit");
  95. return this.shouldStop;
  96. }
  97. function skip() {
  98. this.shouldSkip = true;
  99. }
  100. function skipKey(key) {
  101. if (this.skipKeys == null) {
  102. this.skipKeys = {};
  103. }
  104. this.skipKeys[key] = true;
  105. }
  106. function stop() {
  107. this._traverseFlags |= _index.SHOULD_SKIP | _index.SHOULD_STOP;
  108. }
  109. function _forceSetScope() {
  110. var _this$scope;
  111. let path = this.parentPath;
  112. if ((this.key === "key" || this.listKey === "decorators") && path.isMethod() || this.key === "discriminant" && path.isSwitchStatement()) {
  113. path = path.parentPath;
  114. }
  115. let target;
  116. while (path && !target) {
  117. target = path.scope;
  118. path = path.parentPath;
  119. }
  120. this.scope = this.getScope(target);
  121. (_this$scope = this.scope) == null || _this$scope.init();
  122. }
  123. function setScope() {
  124. var _this$opts2, _this$scope2;
  125. if ((_this$opts2 = this.opts) != null && _this$opts2.noScope) return;
  126. let path = this.parentPath;
  127. if ((this.key === "key" || this.listKey === "decorators") && path.isMethod() || this.key === "discriminant" && path.isSwitchStatement()) {
  128. path = path.parentPath;
  129. }
  130. let target;
  131. while (path && !target) {
  132. var _path$opts;
  133. if ((_path$opts = path.opts) != null && _path$opts.noScope) return;
  134. target = path.scope;
  135. path = path.parentPath;
  136. }
  137. this.scope = this.getScope(target);
  138. (_this$scope2 = this.scope) == null || _this$scope2.init();
  139. }
  140. function setContext(context) {
  141. if (this.skipKeys != null) {
  142. this.skipKeys = {};
  143. }
  144. this._traverseFlags = 0;
  145. if (context) {
  146. this.context = context;
  147. this.state = context.state;
  148. this.opts = context.opts;
  149. }
  150. setScope.call(this);
  151. return this;
  152. }
  153. function resync() {
  154. if (this.removed) return;
  155. _resyncParent.call(this);
  156. _resyncList.call(this);
  157. _resyncKey.call(this);
  158. }
  159. function _resyncParent() {
  160. if (this.parentPath) {
  161. this.parent = this.parentPath.node;
  162. }
  163. }
  164. function _resyncKey() {
  165. if (!this.container) return;
  166. if (this.node === this.container[this.key]) {
  167. return;
  168. }
  169. if (Array.isArray(this.container)) {
  170. for (let i = 0; i < this.container.length; i++) {
  171. if (this.container[i] === this.node) {
  172. setKey.call(this, i);
  173. return;
  174. }
  175. }
  176. } else {
  177. for (const key of Object.keys(this.container)) {
  178. if (this.container[key] === this.node) {
  179. setKey.call(this, key);
  180. return;
  181. }
  182. }
  183. }
  184. this.key = null;
  185. }
  186. function _resyncList() {
  187. if (!this.parent || !this.inList) return;
  188. const newContainer = this.parent[this.listKey];
  189. if (this.container === newContainer) return;
  190. this.container = newContainer || null;
  191. }
  192. function _resyncRemoved() {
  193. if (this.key == null || !this.container || this.container[this.key] !== this.node) {
  194. _removal._markRemoved.call(this);
  195. }
  196. }
  197. function popContext() {
  198. this.contexts.pop();
  199. if (this.contexts.length > 0) {
  200. this.setContext(this.contexts[this.contexts.length - 1]);
  201. } else {
  202. this.setContext(undefined);
  203. }
  204. }
  205. function pushContext(context) {
  206. this.contexts.push(context);
  207. this.setContext(context);
  208. }
  209. function setup(parentPath, container, listKey, key) {
  210. this.listKey = listKey;
  211. this.container = container;
  212. this.parentPath = parentPath || this.parentPath;
  213. setKey.call(this, key);
  214. }
  215. function setKey(key) {
  216. var _this$node;
  217. this.key = key;
  218. this.node = this.container[this.key];
  219. this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
  220. }
  221. function requeue(pathToQueue = this) {
  222. if (pathToQueue.removed) return;
  223. const contexts = this.contexts;
  224. for (const context of contexts) {
  225. context.maybeQueue(pathToQueue);
  226. }
  227. }
  228. function requeueComputedKeyAndDecorators() {
  229. const {
  230. context,
  231. node
  232. } = this;
  233. if (!t.isPrivate(node) && node.computed) {
  234. context.maybeQueue(this.get("key"));
  235. }
  236. if (node.decorators) {
  237. for (const decorator of this.get("decorators")) {
  238. context.maybeQueue(decorator);
  239. }
  240. }
  241. }
  242. function _getQueueContexts() {
  243. let path = this;
  244. let contexts = this.contexts;
  245. while (!contexts.length) {
  246. path = path.parentPath;
  247. if (!path) break;
  248. contexts = path.contexts;
  249. }
  250. return contexts;
  251. }
  252. //# sourceMappingURL=context.js.map