index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = traverse;
  6. Object.defineProperty(exports, "NodePath", {
  7. enumerable: true,
  8. get: function () {
  9. return _path.default;
  10. }
  11. });
  12. Object.defineProperty(exports, "Scope", {
  13. enumerable: true,
  14. get: function () {
  15. return _scope.default;
  16. }
  17. });
  18. Object.defineProperty(exports, "Hub", {
  19. enumerable: true,
  20. get: function () {
  21. return _hub.default;
  22. }
  23. });
  24. exports.visitors = void 0;
  25. var _context = _interopRequireDefault(require("./context"));
  26. var visitors = _interopRequireWildcard(require("./visitors"));
  27. exports.visitors = visitors;
  28. function _includes() {
  29. const data = _interopRequireDefault(require("lodash/includes"));
  30. _includes = function () {
  31. return data;
  32. };
  33. return data;
  34. }
  35. function t() {
  36. const data = _interopRequireWildcard(require("@babel/types"));
  37. t = function () {
  38. return data;
  39. };
  40. return data;
  41. }
  42. var cache = _interopRequireWildcard(require("./cache"));
  43. var _path = _interopRequireDefault(require("./path"));
  44. var _scope = _interopRequireDefault(require("./scope"));
  45. var _hub = _interopRequireDefault(require("./hub"));
  46. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  47. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  48. function traverse(parent, opts, scope, state, parentPath) {
  49. if (!parent) return;
  50. if (!opts) opts = {};
  51. if (!opts.noScope && !scope) {
  52. if (parent.type !== "Program" && parent.type !== "File") {
  53. 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.");
  54. }
  55. }
  56. visitors.explode(opts);
  57. traverse.node(parent, opts, scope, state, parentPath);
  58. }
  59. traverse.visitors = visitors;
  60. traverse.verify = visitors.verify;
  61. traverse.explode = visitors.explode;
  62. traverse.cheap = function (node, enter) {
  63. return t().traverseFast(node, enter);
  64. };
  65. traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
  66. const keys = t().VISITOR_KEYS[node.type];
  67. if (!keys) return;
  68. const context = new _context.default(scope, opts, state, parentPath);
  69. for (const key of keys) {
  70. if (skipKeys && skipKeys[key]) continue;
  71. if (context.visit(node, key)) return;
  72. }
  73. };
  74. traverse.clearNode = function (node, opts) {
  75. t().removeProperties(node, opts);
  76. cache.path.delete(node);
  77. };
  78. traverse.removeProperties = function (tree, opts) {
  79. t().traverseFast(tree, traverse.clearNode, opts);
  80. return tree;
  81. };
  82. function hasBlacklistedType(path, state) {
  83. if (path.node.type === state.type) {
  84. state.has = true;
  85. path.stop();
  86. }
  87. }
  88. traverse.hasType = function (tree, type, blacklistTypes) {
  89. if ((0, _includes().default)(blacklistTypes, tree.type)) return false;
  90. if (tree.type === type) return true;
  91. const state = {
  92. has: false,
  93. type: type
  94. };
  95. traverse(tree, {
  96. noScope: true,
  97. blacklist: blacklistTypes,
  98. enter: hasBlacklistedType
  99. }, null, state);
  100. return state.has;
  101. };
  102. traverse.cache = cache;