visitors.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.environmentVisitor = environmentVisitor;
  6. exports.explode = explode$1;
  7. exports.isExplodedVisitor = isExplodedVisitor;
  8. exports.merge = merge;
  9. exports.verify = verify$1;
  10. var virtualTypes = require("./path/lib/virtual-types.js");
  11. var virtualTypesValidators = require("./path/lib/virtual-types-validator.js");
  12. var _t = require("@babel/types");
  13. var _context = require("./path/context.js");
  14. const {
  15. DEPRECATED_KEYS,
  16. DEPRECATED_ALIASES,
  17. FLIPPED_ALIAS_KEYS,
  18. TYPES,
  19. __internal__deprecationWarning: deprecationWarning
  20. } = _t;
  21. function isVirtualType(type) {
  22. return type in virtualTypes;
  23. }
  24. function isExplodedVisitor(visitor) {
  25. return visitor == null ? void 0 : visitor._exploded;
  26. }
  27. function explode$1(visitor) {
  28. if (isExplodedVisitor(visitor)) return visitor;
  29. visitor._exploded = true;
  30. for (const nodeType of Object.keys(visitor)) {
  31. if (shouldIgnoreKey(nodeType)) continue;
  32. const parts = nodeType.split("|");
  33. if (parts.length === 1) continue;
  34. const fns = visitor[nodeType];
  35. delete visitor[nodeType];
  36. for (const part of parts) {
  37. visitor[part] = fns;
  38. }
  39. }
  40. verify$1(visitor);
  41. delete visitor.__esModule;
  42. ensureEntranceObjects(visitor);
  43. ensureCallbackArrays(visitor);
  44. for (const nodeType of Object.keys(visitor)) {
  45. if (shouldIgnoreKey(nodeType)) continue;
  46. if (!isVirtualType(nodeType)) continue;
  47. const fns = visitor[nodeType];
  48. for (const type of Object.keys(fns)) {
  49. fns[type] = wrapCheck(nodeType, fns[type]);
  50. }
  51. delete visitor[nodeType];
  52. const types = virtualTypes[nodeType];
  53. if (types !== null) {
  54. for (const type of types) {
  55. if (visitor[type]) {
  56. mergePair(visitor[type], fns);
  57. } else {
  58. visitor[type] = fns;
  59. }
  60. }
  61. } else {
  62. mergePair(visitor, fns);
  63. }
  64. }
  65. for (const nodeType of Object.keys(visitor)) {
  66. if (shouldIgnoreKey(nodeType)) continue;
  67. let aliases = FLIPPED_ALIAS_KEYS[nodeType];
  68. if (nodeType in DEPRECATED_KEYS) {
  69. const deprecatedKey = DEPRECATED_KEYS[nodeType];
  70. deprecationWarning(nodeType, deprecatedKey, "Visitor ");
  71. aliases = [deprecatedKey];
  72. } else if (nodeType in DEPRECATED_ALIASES) {
  73. const deprecatedAlias = DEPRECATED_ALIASES[nodeType];
  74. deprecationWarning(nodeType, deprecatedAlias, "Visitor ");
  75. aliases = FLIPPED_ALIAS_KEYS[deprecatedAlias];
  76. }
  77. if (!aliases) continue;
  78. const fns = visitor[nodeType];
  79. delete visitor[nodeType];
  80. for (const alias of aliases) {
  81. const existing = visitor[alias];
  82. if (existing) {
  83. mergePair(existing, fns);
  84. } else {
  85. visitor[alias] = Object.assign({}, fns);
  86. }
  87. }
  88. }
  89. for (const nodeType of Object.keys(visitor)) {
  90. if (shouldIgnoreKey(nodeType)) continue;
  91. ensureCallbackArrays(visitor[nodeType]);
  92. }
  93. return visitor;
  94. }
  95. function verify$1(visitor) {
  96. if (visitor._verified) return;
  97. if (typeof visitor === "function") {
  98. throw new Error("You passed `traverse()` a function when it expected a visitor object, " + "are you sure you didn't mean `{ enter: Function }`?");
  99. }
  100. for (const nodeType of Object.keys(visitor)) {
  101. if (nodeType === "enter" || nodeType === "exit") {
  102. validateVisitorMethods(nodeType, visitor[nodeType]);
  103. }
  104. if (shouldIgnoreKey(nodeType)) continue;
  105. if (!TYPES.includes(nodeType)) {
  106. throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type in @babel/traverse ${"7.25.9"}`);
  107. }
  108. const visitors = visitor[nodeType];
  109. if (typeof visitors === "object") {
  110. for (const visitorKey of Object.keys(visitors)) {
  111. if (visitorKey === "enter" || visitorKey === "exit") {
  112. validateVisitorMethods(`${nodeType}.${visitorKey}`, visitors[visitorKey]);
  113. } else {
  114. throw new Error("You passed `traverse()` a visitor object with the property " + `${nodeType} that has the invalid property ${visitorKey}`);
  115. }
  116. }
  117. }
  118. }
  119. visitor._verified = true;
  120. }
  121. function validateVisitorMethods(path, val) {
  122. const fns = [].concat(val);
  123. for (const fn of fns) {
  124. if (typeof fn !== "function") {
  125. throw new TypeError(`Non-function found defined in ${path} with type ${typeof fn}`);
  126. }
  127. }
  128. }
  129. function merge(visitors, states = [], wrapper) {
  130. const mergedVisitor = {
  131. _verified: true,
  132. _exploded: true
  133. };
  134. {
  135. Object.defineProperty(mergedVisitor, "_exploded", {
  136. enumerable: false
  137. });
  138. Object.defineProperty(mergedVisitor, "_verified", {
  139. enumerable: false
  140. });
  141. }
  142. for (let i = 0; i < visitors.length; i++) {
  143. const visitor = explode$1(visitors[i]);
  144. const state = states[i];
  145. let topVisitor = visitor;
  146. if (state || wrapper) {
  147. topVisitor = wrapWithStateOrWrapper(topVisitor, state, wrapper);
  148. }
  149. mergePair(mergedVisitor, topVisitor);
  150. for (const key of Object.keys(visitor)) {
  151. if (shouldIgnoreKey(key)) continue;
  152. let typeVisitor = visitor[key];
  153. if (state || wrapper) {
  154. typeVisitor = wrapWithStateOrWrapper(typeVisitor, state, wrapper);
  155. }
  156. const nodeVisitor = mergedVisitor[key] || (mergedVisitor[key] = {});
  157. mergePair(nodeVisitor, typeVisitor);
  158. }
  159. }
  160. return mergedVisitor;
  161. }
  162. function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
  163. const newVisitor = {};
  164. for (const phase of ["enter", "exit"]) {
  165. let fns = oldVisitor[phase];
  166. if (!Array.isArray(fns)) continue;
  167. fns = fns.map(function (fn) {
  168. let newFn = fn;
  169. if (state) {
  170. newFn = function (path) {
  171. fn.call(state, path, state);
  172. };
  173. }
  174. if (wrapper) {
  175. newFn = wrapper(state == null ? void 0 : state.key, phase, newFn);
  176. }
  177. if (newFn !== fn) {
  178. newFn.toString = () => fn.toString();
  179. }
  180. return newFn;
  181. });
  182. newVisitor[phase] = fns;
  183. }
  184. return newVisitor;
  185. }
  186. function ensureEntranceObjects(obj) {
  187. for (const key of Object.keys(obj)) {
  188. if (shouldIgnoreKey(key)) continue;
  189. const fns = obj[key];
  190. if (typeof fns === "function") {
  191. obj[key] = {
  192. enter: fns
  193. };
  194. }
  195. }
  196. }
  197. function ensureCallbackArrays(obj) {
  198. if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter];
  199. if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
  200. }
  201. function wrapCheck(nodeType, fn) {
  202. const fnKey = `is${nodeType}`;
  203. const validator = virtualTypesValidators[fnKey];
  204. const newFn = function (path) {
  205. if (validator.call(path)) {
  206. return fn.apply(this, arguments);
  207. }
  208. };
  209. newFn.toString = () => fn.toString();
  210. return newFn;
  211. }
  212. function shouldIgnoreKey(key) {
  213. if (key[0] === "_") return true;
  214. if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
  215. if (key === "denylist" || key === "noScope" || key === "skipKeys") {
  216. return true;
  217. }
  218. {
  219. if (key === "blacklist") {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. function mergePair(dest, src) {
  226. for (const phase of ["enter", "exit"]) {
  227. if (!src[phase]) continue;
  228. dest[phase] = [].concat(dest[phase] || [], src[phase]);
  229. }
  230. }
  231. const _environmentVisitor = {
  232. FunctionParent(path) {
  233. if (path.isArrowFunctionExpression()) return;
  234. path.skip();
  235. if (path.isMethod()) {
  236. if (!path.requeueComputedKeyAndDecorators) {
  237. _context.requeueComputedKeyAndDecorators.call(path);
  238. } else {
  239. path.requeueComputedKeyAndDecorators();
  240. }
  241. }
  242. },
  243. Property(path) {
  244. if (path.isObjectProperty()) return;
  245. path.skip();
  246. if (!path.requeueComputedKeyAndDecorators) {
  247. _context.requeueComputedKeyAndDecorators.call(path);
  248. } else {
  249. path.requeueComputedKeyAndDecorators();
  250. }
  251. }
  252. };
  253. function environmentVisitor(visitor) {
  254. return merge([_environmentVisitor, visitor]);
  255. }
  256. //# sourceMappingURL=visitors.js.map