context.js 6.7 KB

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