You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
5.3 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.call = call;
  6. exports._call = _call;
  7. exports.isBlacklisted = isBlacklisted;
  8. exports.visit = visit;
  9. exports.skip = skip;
  10. exports.skipKey = skipKey;
  11. exports.stop = stop;
  12. exports.setScope = setScope;
  13. exports.setContext = setContext;
  14. exports.resync = resync;
  15. exports._resyncParent = _resyncParent;
  16. exports._resyncKey = _resyncKey;
  17. exports._resyncList = _resyncList;
  18. exports._resyncRemoved = _resyncRemoved;
  19. exports.popContext = popContext;
  20. exports.pushContext = pushContext;
  21. exports.setup = setup;
  22. exports.setKey = setKey;
  23. exports.requeue = requeue;
  24. exports._getQueueContexts = _getQueueContexts;
  25. var _index = _interopRequireDefault(require("../index"));
  26. var _index2 = require("./index");
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  28. function call(key) {
  29. const opts = this.opts;
  30. this.debug(key);
  31. if (this.node) {
  32. if (this._call(opts[key])) return true;
  33. }
  34. if (this.node) {
  35. return this._call(opts[this.node.type] && opts[this.node.type][key]);
  36. }
  37. return false;
  38. }
  39. function _call(fns) {
  40. if (!fns) return false;
  41. for (const fn of fns) {
  42. if (!fn) continue;
  43. const node = this.node;
  44. if (!node) return true;
  45. const ret = fn.call(this.state, this, this.state);
  46. if (ret && typeof ret === "object" && typeof ret.then === "function") {
  47. 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.`);
  48. }
  49. if (ret) {
  50. throw new Error(`Unexpected return value from visitor method ${fn}`);
  51. }
  52. if (this.node !== node) return true;
  53. if (this._traverseFlags > 0) return true;
  54. }
  55. return false;
  56. }
  57. function isBlacklisted() {
  58. const blacklist = this.opts.blacklist;
  59. return blacklist && blacklist.indexOf(this.node.type) > -1;
  60. }
  61. function visit() {
  62. if (!this.node) {
  63. return false;
  64. }
  65. if (this.isBlacklisted()) {
  66. return false;
  67. }
  68. if (this.opts.shouldSkip && this.opts.shouldSkip(this)) {
  69. return false;
  70. }
  71. if (this.shouldSkip || this.call("enter") || this.shouldSkip) {
  72. this.debug("Skip...");
  73. return this.shouldStop;
  74. }
  75. this.debug("Recursing into...");
  76. _index.default.node(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
  77. this.call("exit");
  78. return this.shouldStop;
  79. }
  80. function skip() {
  81. this.shouldSkip = true;
  82. }
  83. function skipKey(key) {
  84. if (this.skipKeys == null) {
  85. this.skipKeys = {};
  86. }
  87. this.skipKeys[key] = true;
  88. }
  89. function stop() {
  90. this._traverseFlags |= _index2.SHOULD_SKIP | _index2.SHOULD_STOP;
  91. }
  92. function setScope() {
  93. if (this.opts && this.opts.noScope) return;
  94. let path = this.parentPath;
  95. let target;
  96. while (path && !target) {
  97. if (path.opts && path.opts.noScope) return;
  98. target = path.scope;
  99. path = path.parentPath;
  100. }
  101. this.scope = this.getScope(target);
  102. if (this.scope) this.scope.init();
  103. }
  104. function setContext(context) {
  105. if (this.skipKeys != null) {
  106. this.skipKeys = {};
  107. }
  108. this._traverseFlags = 0;
  109. if (context) {
  110. this.context = context;
  111. this.state = context.state;
  112. this.opts = context.opts;
  113. }
  114. this.setScope();
  115. return this;
  116. }
  117. function resync() {
  118. if (this.removed) return;
  119. this._resyncParent();
  120. this._resyncList();
  121. this._resyncKey();
  122. }
  123. function _resyncParent() {
  124. if (this.parentPath) {
  125. this.parent = this.parentPath.node;
  126. }
  127. }
  128. function _resyncKey() {
  129. if (!this.container) return;
  130. if (this.node === this.container[this.key]) return;
  131. if (Array.isArray(this.container)) {
  132. for (let i = 0; i < this.container.length; i++) {
  133. if (this.container[i] === this.node) {
  134. return this.setKey(i);
  135. }
  136. }
  137. } else {
  138. for (const key of Object.keys(this.container)) {
  139. if (this.container[key] === this.node) {
  140. return this.setKey(key);
  141. }
  142. }
  143. }
  144. this.key = null;
  145. }
  146. function _resyncList() {
  147. if (!this.parent || !this.inList) return;
  148. const newContainer = this.parent[this.listKey];
  149. if (this.container === newContainer) return;
  150. this.container = newContainer || null;
  151. }
  152. function _resyncRemoved() {
  153. if (this.key == null || !this.container || this.container[this.key] !== this.node) {
  154. this._markRemoved();
  155. }
  156. }
  157. function popContext() {
  158. this.contexts.pop();
  159. if (this.contexts.length > 0) {
  160. this.setContext(this.contexts[this.contexts.length - 1]);
  161. } else {
  162. this.setContext(undefined);
  163. }
  164. }
  165. function pushContext(context) {
  166. this.contexts.push(context);
  167. this.setContext(context);
  168. }
  169. function setup(parentPath, container, listKey, key) {
  170. this.listKey = listKey;
  171. this.container = container;
  172. this.parentPath = parentPath || this.parentPath;
  173. this.setKey(key);
  174. }
  175. function setKey(key) {
  176. this.key = key;
  177. this.node = this.container[this.key];
  178. this.type = this.node && this.node.type;
  179. }
  180. function requeue(pathToQueue = this) {
  181. if (pathToQueue.removed) return;
  182. const contexts = this.contexts;
  183. for (const context of contexts) {
  184. context.maybeQueue(pathToQueue);
  185. }
  186. }
  187. function _getQueueContexts() {
  188. let path = this;
  189. let contexts = this.contexts;
  190. while (!contexts.length) {
  191. path = path.parentPath;
  192. if (!path) break;
  193. contexts = path.contexts;
  194. }
  195. return contexts;
  196. }