index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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,
  38. opts = {}, scope, state, parentPath) {
  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 (!VISITOR_KEYS[parent.type]) {
  46. return;
  47. }
  48. visitors.explode(opts);
  49. (0, _traverseNode.traverseNode)(parent, opts, scope, state, parentPath);
  50. }
  51. var _default = traverse;
  52. exports.default = _default;
  53. traverse.visitors = visitors;
  54. traverse.verify = visitors.verify;
  55. traverse.explode = visitors.explode;
  56. traverse.cheap = function (node, enter) {
  57. return traverseFast(node, enter);
  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