index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.visitors = exports.Hub = exports.Scope = exports.NodePath = undefined;
  4. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  5. var _getIterator3 = _interopRequireDefault(_getIterator2);
  6. var _path = require("./path");
  7. Object.defineProperty(exports, "NodePath", {
  8. enumerable: true,
  9. get: function get() {
  10. return _interopRequireDefault(_path).default;
  11. }
  12. });
  13. var _scope = require("./scope");
  14. Object.defineProperty(exports, "Scope", {
  15. enumerable: true,
  16. get: function get() {
  17. return _interopRequireDefault(_scope).default;
  18. }
  19. });
  20. var _hub = require("./hub");
  21. Object.defineProperty(exports, "Hub", {
  22. enumerable: true,
  23. get: function get() {
  24. return _interopRequireDefault(_hub).default;
  25. }
  26. });
  27. exports.default = traverse;
  28. var _context = require("./context");
  29. var _context2 = _interopRequireDefault(_context);
  30. var _visitors = require("./visitors");
  31. var visitors = _interopRequireWildcard(_visitors);
  32. var _babelMessages = require("babel-messages");
  33. var messages = _interopRequireWildcard(_babelMessages);
  34. var _includes = require("lodash/includes");
  35. var _includes2 = _interopRequireDefault(_includes);
  36. var _babelTypes = require("babel-types");
  37. var t = _interopRequireWildcard(_babelTypes);
  38. var _cache = require("./cache");
  39. var cache = _interopRequireWildcard(_cache);
  40. 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  41. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  42. exports.visitors = visitors;
  43. function traverse(parent, opts, scope, state, parentPath) {
  44. if (!parent) return;
  45. if (!opts) opts = {};
  46. if (!opts.noScope && !scope) {
  47. if (parent.type !== "Program" && parent.type !== "File") {
  48. throw new Error(messages.get("traverseNeedsParent", parent.type));
  49. }
  50. }
  51. visitors.explode(opts);
  52. traverse.node(parent, opts, scope, state, parentPath);
  53. }
  54. traverse.visitors = visitors;
  55. traverse.verify = visitors.verify;
  56. traverse.explode = visitors.explode;
  57. traverse.NodePath = require("./path");
  58. traverse.Scope = require("./scope");
  59. traverse.Hub = require("./hub");
  60. traverse.cheap = function (node, enter) {
  61. return t.traverseFast(node, enter);
  62. };
  63. traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
  64. var keys = t.VISITOR_KEYS[node.type];
  65. if (!keys) return;
  66. var context = new _context2.default(scope, opts, state, parentPath);
  67. for (var _iterator = keys, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  68. var _ref;
  69. if (_isArray) {
  70. if (_i >= _iterator.length) break;
  71. _ref = _iterator[_i++];
  72. } else {
  73. _i = _iterator.next();
  74. if (_i.done) break;
  75. _ref = _i.value;
  76. }
  77. var key = _ref;
  78. if (skipKeys && skipKeys[key]) continue;
  79. if (context.visit(node, key)) return;
  80. }
  81. };
  82. traverse.clearNode = function (node, opts) {
  83. t.removeProperties(node, opts);
  84. cache.path.delete(node);
  85. };
  86. traverse.removeProperties = function (tree, opts) {
  87. t.traverseFast(tree, traverse.clearNode, opts);
  88. return tree;
  89. };
  90. function hasBlacklistedType(path, state) {
  91. if (path.node.type === state.type) {
  92. state.has = true;
  93. path.stop();
  94. }
  95. }
  96. traverse.hasType = function (tree, scope, type, blacklistTypes) {
  97. if ((0, _includes2.default)(blacklistTypes, tree.type)) return false;
  98. if (tree.type === type) return true;
  99. var state = {
  100. has: false,
  101. type: type
  102. };
  103. traverse(tree, {
  104. blacklist: blacklistTypes,
  105. enter: hasBlacklistedType
  106. }, scope, state);
  107. return state.has;
  108. };
  109. traverse.clearCache = function () {
  110. cache.clear();
  111. };
  112. traverse.clearCache.clearPath = cache.clearPath;
  113. traverse.clearCache.clearScope = cache.clearScope;
  114. traverse.copyCache = function (source, destination) {
  115. if (cache.path.has(source)) {
  116. cache.path.set(destination, cache.path.get(source));
  117. }
  118. };