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. 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 = traverse;
  54. exports.default = _default;
  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