context.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.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 && denylist.indexOf(this.node.type) > -1;
  64. }
  65. function restoreContext(path, context) {
  66. if (path.context !== context) {
  67. path.context = context;
  68. path.state = context.state;
  69. path.opts = context.opts;
  70. }
  71. }
  72. function visit() {
  73. var _this$opts$shouldSkip, _this$opts;
  74. if (!this.node) {
  75. return false;
  76. }
  77. if (this.isDenylisted()) {
  78. return false;
  79. }
  80. if ((_this$opts$shouldSkip = (_this$opts = this.opts).shouldSkip) != null && _this$opts$shouldSkip.call(_this$opts, this)) {
  81. return false;
  82. }
  83. const currentContext = this.context;
  84. if (this.shouldSkip || this.call("enter")) {
  85. this.debug("Skip...");
  86. return this.shouldStop;
  87. }
  88. restoreContext(this, currentContext);
  89. this.debug("Recursing into...");
  90. this.shouldStop = (0, _traverseNode.traverseNode)(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
  91. restoreContext(this, currentContext);
  92. this.call("exit");
  93. return this.shouldStop;
  94. }
  95. function skip() {
  96. this.shouldSkip = true;
  97. }
  98. function skipKey(key) {
  99. if (this.skipKeys == null) {
  100. this.skipKeys = {};
  101. }
  102. this.skipKeys[key] = true;
  103. }
  104. function stop() {
  105. this._traverseFlags |= _index.SHOULD_SKIP | _index.SHOULD_STOP;
  106. }
  107. function setScope() {
  108. var _this$opts2, _this$scope;
  109. if ((_this$opts2 = this.opts) != null && _this$opts2.noScope) return;
  110. let path = this.parentPath;
  111. if ((this.key === "key" || this.listKey === "decorators") && path.isMethod() || this.key === "discriminant" && path.isSwitchStatement()) {
  112. path = path.parentPath;
  113. }
  114. let target;
  115. while (path && !target) {
  116. var _path$opts;
  117. if ((_path$opts = path.opts) != null && _path$opts.noScope) return;
  118. target = path.scope;
  119. path = path.parentPath;
  120. }
  121. this.scope = this.getScope(target);
  122. (_this$scope = this.scope) == null || _this$scope.init();
  123. }
  124. function setContext(context) {
  125. if (this.skipKeys != null) {
  126. this.skipKeys = {};
  127. }
  128. this._traverseFlags = 0;
  129. if (context) {
  130. this.context = context;
  131. this.state = context.state;
  132. this.opts = context.opts;
  133. }
  134. this.setScope();
  135. return this;
  136. }
  137. function resync() {
  138. if (this.removed) return;
  139. _resyncParent.call(this);
  140. _resyncList.call(this);
  141. _resyncKey.call(this);
  142. }
  143. function _resyncParent() {
  144. if (this.parentPath) {
  145. this.parent = this.parentPath.node;
  146. }
  147. }
  148. function _resyncKey() {
  149. if (!this.container) return;
  150. if (this.node === this.container[this.key]) {
  151. return;
  152. }
  153. if (Array.isArray(this.container)) {
  154. for (let i = 0; i < this.container.length; i++) {
  155. if (this.container[i] === this.node) {
  156. this.setKey(i);
  157. return;
  158. }
  159. }
  160. } else {
  161. for (const key of Object.keys(this.container)) {
  162. if (this.container[key] === this.node) {
  163. this.setKey(key);
  164. return;
  165. }
  166. }
  167. }
  168. this.key = null;
  169. }
  170. function _resyncList() {
  171. if (!this.parent || !this.inList) return;
  172. const newContainer = this.parent[this.listKey];
  173. if (this.container === newContainer) return;
  174. this.container = newContainer || null;
  175. }
  176. function _resyncRemoved() {
  177. if (this.key == null || !this.container || this.container[this.key] !== this.node) {
  178. _removal._markRemoved.call(this);
  179. }
  180. }
  181. function popContext() {
  182. this.contexts.pop();
  183. if (this.contexts.length > 0) {
  184. this.setContext(this.contexts[this.contexts.length - 1]);
  185. } else {
  186. this.setContext(undefined);
  187. }
  188. }
  189. function pushContext(context) {
  190. this.contexts.push(context);
  191. this.setContext(context);
  192. }
  193. function setup(parentPath, container, listKey, key) {
  194. this.listKey = listKey;
  195. this.container = container;
  196. this.parentPath = parentPath || this.parentPath;
  197. this.setKey(key);
  198. }
  199. function setKey(key) {
  200. var _this$node;
  201. this.key = key;
  202. this.node = this.container[this.key];
  203. this.type = (_this$node = this.node) == null ? void 0 : _this$node.type;
  204. }
  205. function requeue(pathToQueue = this) {
  206. if (pathToQueue.removed) return;
  207. ;
  208. const contexts = this.contexts;
  209. for (const context of contexts) {
  210. context.maybeQueue(pathToQueue);
  211. }
  212. }
  213. function requeueComputedKeyAndDecorators() {
  214. const {
  215. context,
  216. node
  217. } = this;
  218. if (!t.isPrivate(node) && node.computed) {
  219. context.maybeQueue(this.get("key"));
  220. }
  221. if (node.decorators) {
  222. for (const decorator of this.get("decorators")) {
  223. context.maybeQueue(decorator);
  224. }
  225. }
  226. }
  227. function _getQueueContexts() {
  228. let path = this;
  229. let contexts = this.contexts;
  230. while (!contexts.length) {
  231. path = path.parentPath;
  232. if (!path) break;
  233. contexts = path.contexts;
  234. }
  235. return contexts;
  236. }
  237. //# sourceMappingURL=context.js.map