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