index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "Hub", {
  6. enumerable: true,
  7. get: function () {
  8. return _hub.default;
  9. }
  10. });
  11. Object.defineProperty(exports, "NodePath", {
  12. enumerable: true,
  13. get: function () {
  14. return _index.default;
  15. }
  16. });
  17. Object.defineProperty(exports, "Scope", {
  18. enumerable: true,
  19. get: function () {
  20. return _index2.default;
  21. }
  22. });
  23. exports.visitors = exports.default = void 0;
  24. require("./path/context.js");
  25. var visitors = require("./visitors.js");
  26. exports.visitors = visitors;
  27. var _t = require("@babel/types");
  28. var cache = require("./cache.js");
  29. var _traverseNode = require("./traverse-node.js");
  30. var _index = require("./path/index.js");
  31. var _index2 = require("./scope/index.js");
  32. var _hub = require("./hub.js");
  33. const {
  34. VISITOR_KEYS,
  35. removeProperties,
  36. traverseFast
  37. } = _t;
  38. function traverse(parent, opts = {}, scope, state, parentPath, visitSelf) {
  39. if (!parent) return;
  40. if (!opts.noScope && !scope) {
  41. if (parent.type !== "Program" && parent.type !== "File") {
  42. throw new Error("You must pass a scope and parentPath unless traversing a Program/File. " + `Instead of that you tried to traverse a ${parent.type} node without ` + "passing scope and parentPath.");
  43. }
  44. }
  45. if (!parentPath && visitSelf) {
  46. throw new Error("visitSelf can only be used when providing a NodePath.");
  47. }
  48. if (!VISITOR_KEYS[parent.type]) {
  49. return;
  50. }
  51. visitors.explode(opts);
  52. (0, _traverseNode.traverseNode)(parent, opts, scope, state, parentPath, null, visitSelf);
  53. }
  54. var _default = exports.default = traverse;
  55. traverse.visitors = visitors;
  56. traverse.verify = visitors.verify;
  57. traverse.explode = visitors.explode;
  58. traverse.cheap = function (node, enter) {
  59. traverseFast(node, enter);
  60. return;
  61. };
  62. traverse.node = function (node, opts, scope, state, path, skipKeys) {
  63. (0, _traverseNode.traverseNode)(node, opts, scope, state, path, skipKeys);
  64. };
  65. traverse.clearNode = function (node, opts) {
  66. removeProperties(node, opts);
  67. };
  68. traverse.removeProperties = function (tree, opts) {
  69. traverseFast(tree, traverse.clearNode, opts);
  70. return tree;
  71. };
  72. traverse.hasType = function (tree, type, denylistTypes) {
  73. if (denylistTypes != null && denylistTypes.includes(tree.type)) return false;
  74. if (tree.type === type) return true;
  75. return traverseFast(tree, function (node) {
  76. if (denylistTypes != null && denylistTypes.includes(node.type)) {
  77. return traverseFast.skip;
  78. }
  79. if (node.type === type) {
  80. return traverseFast.stop;
  81. }
  82. });
  83. };
  84. traverse.cache = cache;
  85. //# sourceMappingURL=index.js.map