index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. exports.recursive = exports.ancestor = exports.simple = void 0;
  26. const t = __importStar(require("@babel/types"));
  27. const explode_1 = __importDefault(require("./explode"));
  28. const VISITOR_KEYS = t.VISITOR_KEYS;
  29. if (!(VISITOR_KEYS &&
  30. // tslint:disable-next-line: strict-type-predicates
  31. typeof VISITOR_KEYS === 'object' &&
  32. Object.keys(VISITOR_KEYS).every((key) => Array.isArray(VISITOR_KEYS[key]) &&
  33. // tslint:disable-next-line: strict-type-predicates
  34. VISITOR_KEYS[key].every((v) => typeof v === 'string')))) {
  35. throw new Error('@babel/types VISITOR_KEYS does not match the expected type.');
  36. }
  37. function simple(visitors) {
  38. const vis = explode_1.default(visitors);
  39. return (node, state) => {
  40. (function recurse(node) {
  41. if (!node)
  42. return;
  43. const visitor = vis[node.type];
  44. if (visitor === null || visitor === void 0 ? void 0 : visitor.enter) {
  45. for (const v of visitor.enter) {
  46. v(node, state);
  47. }
  48. }
  49. for (const key of VISITOR_KEYS[node.type] || []) {
  50. const subNode = node[key];
  51. if (Array.isArray(subNode)) {
  52. for (const subSubNode of subNode) {
  53. recurse(subSubNode);
  54. }
  55. }
  56. else {
  57. recurse(subNode);
  58. }
  59. }
  60. if (visitor === null || visitor === void 0 ? void 0 : visitor.exit) {
  61. for (const v of visitor.exit) {
  62. v(node, state);
  63. }
  64. }
  65. })(node);
  66. };
  67. }
  68. exports.simple = simple;
  69. function ancestor(visitors) {
  70. const vis = explode_1.default(visitors);
  71. return (node, state) => {
  72. const ancestors = [];
  73. (function recurse(node) {
  74. if (!node)
  75. return;
  76. const visitor = vis[node.type];
  77. const isNew = node !== ancestors[ancestors.length - 1];
  78. if (isNew)
  79. ancestors.push(node);
  80. if (visitor === null || visitor === void 0 ? void 0 : visitor.enter) {
  81. for (const v of visitor.enter) {
  82. v(node, state, ancestors);
  83. }
  84. }
  85. for (const key of VISITOR_KEYS[node.type] || []) {
  86. const subNode = node[key];
  87. if (Array.isArray(subNode)) {
  88. for (const subSubNode of subNode) {
  89. recurse(subSubNode);
  90. }
  91. }
  92. else {
  93. recurse(subNode);
  94. }
  95. }
  96. if (visitor === null || visitor === void 0 ? void 0 : visitor.exit) {
  97. for (const v of visitor.exit) {
  98. v(node, state, ancestors);
  99. }
  100. }
  101. if (isNew)
  102. ancestors.pop();
  103. })(node);
  104. };
  105. }
  106. exports.ancestor = ancestor;
  107. function recursive(visitors) {
  108. const vis = explode_1.default(visitors);
  109. return (node, state) => {
  110. (function recurse(node) {
  111. if (!node)
  112. return;
  113. const visitor = vis[node.type];
  114. if (visitor === null || visitor === void 0 ? void 0 : visitor.enter) {
  115. for (const v of visitor.enter) {
  116. v(node, state, recurse);
  117. }
  118. }
  119. else {
  120. for (const key of VISITOR_KEYS[node.type] || []) {
  121. const subNode = node[key];
  122. if (Array.isArray(subNode)) {
  123. for (const subSubNode of subNode) {
  124. recurse(subSubNode);
  125. }
  126. }
  127. else {
  128. recurse(subNode);
  129. }
  130. }
  131. }
  132. })(node);
  133. };
  134. }
  135. exports.recursive = recursive;
  136. //# sourceMappingURL=index.js.map