index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. var visitors = require("./visitors.js");
  25. exports.visitors = visitors;
  26. var _t = require("@babel/types");
  27. var cache = require("./cache.js");
  28. var _traverseNode = require("./traverse-node.js");
  29. var _index = require("./path/index.js");
  30. var _index2 = require("./scope/index.js");
  31. var _hub = require("./hub.js");
  32. const {
  33. VISITOR_KEYS,
  34. removeProperties,
  35. traverseFast
  36. } = _t;
  37. function traverse(parent, opts = {}, scope, state, parentPath, visitSelf) {
  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 (!parentPath && visitSelf) {
  45. throw new Error("visitSelf can only be used when providing a NodePath.");
  46. }
  47. if (!VISITOR_KEYS[parent.type]) {
  48. return;
  49. }
  50. visitors.explode(opts);
  51. (0, _traverseNode.traverseNode)(parent, opts, scope, state, parentPath, null, visitSelf);
  52. }
  53. var _default = exports.default = traverse;
  54. traverse.visitors = visitors;
  55. traverse.verify = visitors.verify;
  56. traverse.explode = visitors.explode;
  57. traverse.cheap = function (node, enter) {
  58. traverseFast(node, enter);
  59. return;
  60. };
  61. traverse.node = function (node, opts, scope, state, path, skipKeys) {
  62. (0, _traverseNode.traverseNode)(node, opts, scope, state, path, skipKeys);
  63. };
  64. traverse.clearNode = function (node, opts) {
  65. removeProperties(node, opts);
  66. };
  67. traverse.removeProperties = function (tree, opts) {
  68. traverseFast(tree, traverse.clearNode, opts);
  69. return tree;
  70. };
  71. function hasDenylistedType(path, state) {
  72. if (path.node.type === state.type) {
  73. state.has = true;
  74. path.stop();
  75. }
  76. }
  77. traverse.hasType = function (tree, type, denylistTypes) {
  78. if (denylistTypes != null && denylistTypes.includes(tree.type)) return false;
  79. if (tree.type === type) return true;
  80. const state = {
  81. has: false,
  82. type: type
  83. };
  84. traverse(tree, {
  85. noScope: true,
  86. denylist: denylistTypes,
  87. enter: hasDenylistedType
  88. }, null, state);
  89. return state.has;
  90. };
  91. traverse.cache = cache;
  92. //# sourceMappingURL=index.js.map