index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 _path.default;
  15. }
  16. });
  17. Object.defineProperty(exports, "Scope", {
  18. enumerable: true,
  19. get: function () {
  20. return _scope.default;
  21. }
  22. });
  23. exports.visitors = exports.default = void 0;
  24. var visitors = require("./visitors");
  25. exports.visitors = visitors;
  26. var _t = require("@babel/types");
  27. var cache = require("./cache");
  28. var _traverseNode = require("./traverse-node");
  29. var _path = require("./path");
  30. var _scope = require("./scope");
  31. var _hub = require("./hub");
  32. const {
  33. VISITOR_KEYS,
  34. removeProperties,
  35. traverseFast
  36. } = _t;
  37. function traverse(parent, opts = {}, scope, state, parentPath) {
  38. if (!parent) return;
  39. if (!opts.noScope && !scope) {
  40. if (parent.type !== "Program" && parent.type !== "File") {
  41. 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.");
  42. }
  43. }
  44. if (!VISITOR_KEYS[parent.type]) {
  45. return;
  46. }
  47. visitors.explode(opts);
  48. (0, _traverseNode.traverseNode)(parent, opts, scope, state, parentPath);
  49. }
  50. var _default = traverse;
  51. exports.default = _default;
  52. traverse.visitors = visitors;
  53. traverse.verify = visitors.verify;
  54. traverse.explode = visitors.explode;
  55. traverse.cheap = function (node, enter) {
  56. traverseFast(node, enter);
  57. return;
  58. };
  59. traverse.node = function (node, opts, scope, state, path, skipKeys) {
  60. (0, _traverseNode.traverseNode)(node, opts, scope, state, path, skipKeys);
  61. };
  62. traverse.clearNode = function (node, opts) {
  63. removeProperties(node, opts);
  64. cache.path.delete(node);
  65. };
  66. traverse.removeProperties = function (tree, opts) {
  67. traverseFast(tree, traverse.clearNode, opts);
  68. return tree;
  69. };
  70. function hasDenylistedType(path, state) {
  71. if (path.node.type === state.type) {
  72. state.has = true;
  73. path.stop();
  74. }
  75. }
  76. traverse.hasType = function (tree, type, denylistTypes) {
  77. if (denylistTypes != null && denylistTypes.includes(tree.type)) return false;
  78. if (tree.type === type) return true;
  79. const state = {
  80. has: false,
  81. type: type
  82. };
  83. traverse(tree, {
  84. noScope: true,
  85. denylist: denylistTypes,
  86. enter: hasDenylistedType
  87. }, null, state);
  88. return state.has;
  89. };
  90. traverse.cache = cache;
  91. //# sourceMappingURL=index.js.map