You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.8 KiB

4 years ago
  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. var _includes = _interopRequireDefault(require("lodash/includes"));
  29. var t = _interopRequireWildcard(require("@babel/types"));
  30. var cache = _interopRequireWildcard(require("./cache"));
  31. var _path = _interopRequireDefault(require("./path"));
  32. var _scope = _interopRequireDefault(require("./scope"));
  33. var _hub = _interopRequireDefault(require("./hub"));
  34. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  35. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  36. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  37. function traverse(parent, opts, scope, state, parentPath) {
  38. if (!parent) return;
  39. if (!opts) opts = {};
  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 (!t.VISITOR_KEYS[parent.type]) {
  46. return;
  47. }
  48. visitors.explode(opts);
  49. traverse.node(parent, opts, scope, state, parentPath);
  50. }
  51. traverse.visitors = visitors;
  52. traverse.verify = visitors.verify;
  53. traverse.explode = visitors.explode;
  54. traverse.cheap = function (node, enter) {
  55. return t.traverseFast(node, enter);
  56. };
  57. traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
  58. const keys = t.VISITOR_KEYS[node.type];
  59. if (!keys) return;
  60. const context = new _context.default(scope, opts, state, parentPath);
  61. for (const key of keys) {
  62. if (skipKeys && skipKeys[key]) continue;
  63. if (context.visit(node, key)) return;
  64. }
  65. };
  66. traverse.clearNode = function (node, opts) {
  67. t.removeProperties(node, opts);
  68. cache.path.delete(node);
  69. };
  70. traverse.removeProperties = function (tree, opts) {
  71. t.traverseFast(tree, traverse.clearNode, opts);
  72. return tree;
  73. };
  74. function hasBlacklistedType(path, state) {
  75. if (path.node.type === state.type) {
  76. state.has = true;
  77. path.stop();
  78. }
  79. }
  80. traverse.hasType = function (tree, type, blacklistTypes) {
  81. if ((0, _includes.default)(blacklistTypes, tree.type)) return false;
  82. if (tree.type === type) return true;
  83. const state = {
  84. has: false,
  85. type: type
  86. };
  87. traverse(tree, {
  88. noScope: true,
  89. blacklist: blacklistTypes,
  90. enter: hasBlacklistedType
  91. }, null, state);
  92. return state.has;
  93. };
  94. traverse.cache = cache;