visitors.js 7.7 KB

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