index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. return traverseFast(node, enter);
  57. };
  58. traverse.node = function (node, opts, scope, state, path, skipKeys) {
  59. (0, _traverseNode.traverseNode)(node, opts, scope, state, path, skipKeys);
  60. };
  61. traverse.clearNode = function (node, opts) {
  62. removeProperties(node, opts);
  63. cache.path.delete(node);
  64. };
  65. traverse.removeProperties = function (tree, opts) {
  66. traverseFast(tree, traverse.clearNode, opts);
  67. return tree;
  68. };
  69. function hasDenylistedType(path, state) {
  70. if (path.node.type === state.type) {
  71. state.has = true;
  72. path.stop();
  73. }
  74. }
  75. traverse.hasType = function (tree, type, denylistTypes) {
  76. if (denylistTypes != null && denylistTypes.includes(tree.type)) return false;
  77. if (tree.type === type) return true;
  78. const state = {
  79. has: false,
  80. type: type
  81. };
  82. traverse(tree, {
  83. noScope: true,
  84. denylist: denylistTypes,
  85. enter: hasDenylistedType
  86. }, null, state);
  87. return state.has;
  88. };
  89. traverse.cache = cache;