index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "NodePath", {
  6. enumerable: true,
  7. get: function () {
  8. return _path.default;
  9. }
  10. });
  11. Object.defineProperty(exports, "Scope", {
  12. enumerable: true,
  13. get: function () {
  14. return _scope.default;
  15. }
  16. });
  17. Object.defineProperty(exports, "Hub", {
  18. enumerable: true,
  19. get: function () {
  20. return _hub.default;
  21. }
  22. });
  23. exports.visitors = exports.default = void 0;
  24. var _context = require("./context");
  25. var visitors = require("./visitors");
  26. exports.visitors = visitors;
  27. var _t = require("@babel/types");
  28. var cache = require("./cache");
  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. traverse.node(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, parentPath, skipKeys) {
  59. const keys = VISITOR_KEYS[node.type];
  60. if (!keys) return;
  61. const context = new _context.default(scope, opts, state, parentPath);
  62. for (const key of keys) {
  63. if (skipKeys && skipKeys[key]) continue;
  64. if (context.visit(node, key)) return;
  65. }
  66. };
  67. traverse.clearNode = function (node, opts) {
  68. removeProperties(node, opts);
  69. cache.path.delete(node);
  70. };
  71. traverse.removeProperties = function (tree, opts) {
  72. traverseFast(tree, traverse.clearNode, opts);
  73. return tree;
  74. };
  75. function hasDenylistedType(path, state) {
  76. if (path.node.type === state.type) {
  77. state.has = true;
  78. path.stop();
  79. }
  80. }
  81. traverse.hasType = function (tree, type, denylistTypes) {
  82. if (denylistTypes != null && denylistTypes.includes(tree.type)) return false;
  83. if (tree.type === type) return true;
  84. const state = {
  85. has: false,
  86. type: type
  87. };
  88. traverse(tree, {
  89. noScope: true,
  90. denylist: denylistTypes,
  91. enter: hasDenylistedType
  92. }, null, state);
  93. return state.has;
  94. };
  95. traverse.cache = cache;