traverse-node.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.traverseNode = traverseNode;
  6. var _context = require("./context.js");
  7. var _index = require("./path/index.js");
  8. var _t = require("@babel/types");
  9. var _context2 = require("./path/context.js");
  10. const {
  11. VISITOR_KEYS
  12. } = _t;
  13. function _visitPaths(ctx, paths) {
  14. ctx.queue = paths;
  15. ctx.priorityQueue = [];
  16. const visited = new Set();
  17. let stop = false;
  18. let visitIndex = 0;
  19. for (; visitIndex < paths.length;) {
  20. const path = paths[visitIndex];
  21. visitIndex++;
  22. _context2.resync.call(path);
  23. if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== ctx) {
  24. _context2.pushContext.call(path, ctx);
  25. }
  26. if (path.key === null) continue;
  27. const {
  28. node
  29. } = path;
  30. if (visited.has(node)) continue;
  31. if (node) visited.add(node);
  32. if (_visit(ctx, path)) {
  33. stop = true;
  34. break;
  35. }
  36. if (ctx.priorityQueue.length) {
  37. stop = _visitPaths(ctx, ctx.priorityQueue);
  38. ctx.priorityQueue = [];
  39. ctx.queue = paths;
  40. if (stop) break;
  41. }
  42. }
  43. for (let i = 0; i < visitIndex; i++) {
  44. _context2.popContext.call(paths[i]);
  45. }
  46. ctx.queue = null;
  47. return stop;
  48. }
  49. function _visit(ctx, path) {
  50. var _opts$denylist;
  51. const node = path.node;
  52. if (!node) {
  53. return false;
  54. }
  55. const opts = ctx.opts;
  56. const denylist = (_opts$denylist = opts.denylist) != null ? _opts$denylist : opts.blacklist;
  57. if (denylist != null && denylist.includes(node.type)) {
  58. return false;
  59. }
  60. if (opts.shouldSkip != null && opts.shouldSkip(path)) {
  61. return false;
  62. }
  63. if (path.shouldSkip) return path.shouldStop;
  64. if (_context2._call.call(path, opts.enter)) return path.shouldStop;
  65. if (path.node) {
  66. var _opts$node$type;
  67. if (_context2._call.call(path, (_opts$node$type = opts[node.type]) == null ? void 0 : _opts$node$type.enter)) return path.shouldStop;
  68. }
  69. path.shouldStop = _traverse(path.node, opts, path.scope, ctx.state, path, path.skipKeys);
  70. if (path.node) {
  71. if (_context2._call.call(path, opts.exit)) return true;
  72. }
  73. if (path.node) {
  74. var _opts$node$type2;
  75. _context2._call.call(path, (_opts$node$type2 = opts[node.type]) == null ? void 0 : _opts$node$type2.exit);
  76. }
  77. return path.shouldStop;
  78. }
  79. function _traverse(node, opts, scope, state, path, skipKeys, visitSelf) {
  80. const keys = VISITOR_KEYS[node.type];
  81. if (!(keys != null && keys.length)) return false;
  82. const ctx = new _context.default(scope, opts, state, path);
  83. if (visitSelf) {
  84. if (skipKeys != null && skipKeys[path.parentKey]) return false;
  85. return _visitPaths(ctx, [path]);
  86. }
  87. for (const key of keys) {
  88. if (skipKeys != null && skipKeys[key]) continue;
  89. const prop = node[key];
  90. if (!prop) continue;
  91. if (Array.isArray(prop)) {
  92. if (!prop.length) continue;
  93. const paths = [];
  94. for (let i = 0; i < prop.length; i++) {
  95. const childPath = _index.default.get({
  96. parentPath: path,
  97. parent: node,
  98. container: prop,
  99. key: i,
  100. listKey: key
  101. });
  102. paths.push(childPath);
  103. }
  104. if (_visitPaths(ctx, paths)) return true;
  105. } else {
  106. if (_visitPaths(ctx, [_index.default.get({
  107. parentPath: path,
  108. parent: node,
  109. container: node,
  110. key,
  111. listKey: null
  112. })])) {
  113. return true;
  114. }
  115. }
  116. }
  117. return false;
  118. }
  119. function traverseNode(node, opts, scope, state, path, skipKeys, visitSelf) {
  120. ;
  121. const keys = VISITOR_KEYS[node.type];
  122. if (!keys) return false;
  123. const context = new _context.default(scope, opts, state, path);
  124. if (visitSelf) {
  125. if (skipKeys != null && skipKeys[path.parentKey]) return false;
  126. return context.visitQueue([path]);
  127. }
  128. for (const key of keys) {
  129. if (skipKeys != null && skipKeys[key]) continue;
  130. if (context.visit(node, key)) {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. //# sourceMappingURL=traverse-node.js.map