index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  6. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  7. var _virtualTypes = require("./lib/virtual-types");
  8. var virtualTypes = _interopRequireWildcard(_virtualTypes);
  9. var _debug2 = require("debug");
  10. var _debug3 = _interopRequireDefault(_debug2);
  11. var _invariant = require("invariant");
  12. var _invariant2 = _interopRequireDefault(_invariant);
  13. var _index = require("../index");
  14. var _index2 = _interopRequireDefault(_index);
  15. var _assign = require("lodash/assign");
  16. var _assign2 = _interopRequireDefault(_assign);
  17. var _scope = require("../scope");
  18. var _scope2 = _interopRequireDefault(_scope);
  19. var _babelTypes = require("babel-types");
  20. var t = _interopRequireWildcard(_babelTypes);
  21. var _cache = require("../cache");
  22. 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; } }
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. var _debug = (0, _debug3.default)("babel");
  25. var NodePath = function () {
  26. function NodePath(hub, parent) {
  27. (0, _classCallCheck3.default)(this, NodePath);
  28. this.parent = parent;
  29. this.hub = hub;
  30. this.contexts = [];
  31. this.data = {};
  32. this.shouldSkip = false;
  33. this.shouldStop = false;
  34. this.removed = false;
  35. this.state = null;
  36. this.opts = null;
  37. this.skipKeys = null;
  38. this.parentPath = null;
  39. this.context = null;
  40. this.container = null;
  41. this.listKey = null;
  42. this.inList = false;
  43. this.parentKey = null;
  44. this.key = null;
  45. this.node = null;
  46. this.scope = null;
  47. this.type = null;
  48. this.typeAnnotation = null;
  49. }
  50. NodePath.get = function get(_ref) {
  51. var hub = _ref.hub,
  52. parentPath = _ref.parentPath,
  53. parent = _ref.parent,
  54. container = _ref.container,
  55. listKey = _ref.listKey,
  56. key = _ref.key;
  57. if (!hub && parentPath) {
  58. hub = parentPath.hub;
  59. }
  60. (0, _invariant2.default)(parent, "To get a node path the parent needs to exist");
  61. var targetNode = container[key];
  62. var paths = _cache.path.get(parent) || [];
  63. if (!_cache.path.has(parent)) {
  64. _cache.path.set(parent, paths);
  65. }
  66. var path = void 0;
  67. for (var i = 0; i < paths.length; i++) {
  68. var pathCheck = paths[i];
  69. if (pathCheck.node === targetNode) {
  70. path = pathCheck;
  71. break;
  72. }
  73. }
  74. if (!path) {
  75. path = new NodePath(hub, parent);
  76. paths.push(path);
  77. }
  78. path.setup(parentPath, container, listKey, key);
  79. return path;
  80. };
  81. NodePath.prototype.getScope = function getScope(scope) {
  82. var ourScope = scope;
  83. if (this.isScope()) {
  84. ourScope = new _scope2.default(this, scope);
  85. }
  86. return ourScope;
  87. };
  88. NodePath.prototype.setData = function setData(key, val) {
  89. return this.data[key] = val;
  90. };
  91. NodePath.prototype.getData = function getData(key, def) {
  92. var val = this.data[key];
  93. if (!val && def) val = this.data[key] = def;
  94. return val;
  95. };
  96. NodePath.prototype.buildCodeFrameError = function buildCodeFrameError(msg) {
  97. var Error = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : SyntaxError;
  98. return this.hub.file.buildCodeFrameError(this.node, msg, Error);
  99. };
  100. NodePath.prototype.traverse = function traverse(visitor, state) {
  101. (0, _index2.default)(this.node, visitor, this.scope, state, this);
  102. };
  103. NodePath.prototype.mark = function mark(type, message) {
  104. this.hub.file.metadata.marked.push({
  105. type: type,
  106. message: message,
  107. loc: this.node.loc
  108. });
  109. };
  110. NodePath.prototype.set = function set(key, node) {
  111. t.validate(this.node, key, node);
  112. this.node[key] = node;
  113. };
  114. NodePath.prototype.getPathLocation = function getPathLocation() {
  115. var parts = [];
  116. var path = this;
  117. do {
  118. var key = path.key;
  119. if (path.inList) key = path.listKey + "[" + key + "]";
  120. parts.unshift(key);
  121. } while (path = path.parentPath);
  122. return parts.join(".");
  123. };
  124. NodePath.prototype.debug = function debug(buildMessage) {
  125. if (!_debug.enabled) return;
  126. _debug(this.getPathLocation() + " " + this.type + ": " + buildMessage());
  127. };
  128. return NodePath;
  129. }();
  130. exports.default = NodePath;
  131. (0, _assign2.default)(NodePath.prototype, require("./ancestry"));
  132. (0, _assign2.default)(NodePath.prototype, require("./inference"));
  133. (0, _assign2.default)(NodePath.prototype, require("./replacement"));
  134. (0, _assign2.default)(NodePath.prototype, require("./evaluation"));
  135. (0, _assign2.default)(NodePath.prototype, require("./conversion"));
  136. (0, _assign2.default)(NodePath.prototype, require("./introspection"));
  137. (0, _assign2.default)(NodePath.prototype, require("./context"));
  138. (0, _assign2.default)(NodePath.prototype, require("./removal"));
  139. (0, _assign2.default)(NodePath.prototype, require("./modification"));
  140. (0, _assign2.default)(NodePath.prototype, require("./family"));
  141. (0, _assign2.default)(NodePath.prototype, require("./comments"));
  142. var _loop2 = function _loop2() {
  143. if (_isArray) {
  144. if (_i >= _iterator.length) return "break";
  145. _ref2 = _iterator[_i++];
  146. } else {
  147. _i = _iterator.next();
  148. if (_i.done) return "break";
  149. _ref2 = _i.value;
  150. }
  151. var type = _ref2;
  152. var typeKey = "is" + type;
  153. NodePath.prototype[typeKey] = function (opts) {
  154. return t[typeKey](this.node, opts);
  155. };
  156. NodePath.prototype["assert" + type] = function (opts) {
  157. if (!this[typeKey](opts)) {
  158. throw new TypeError("Expected node path of type " + type);
  159. }
  160. };
  161. };
  162. for (var _iterator = t.TYPES, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  163. var _ref2;
  164. var _ret2 = _loop2();
  165. if (_ret2 === "break") break;
  166. }
  167. var _loop = function _loop(type) {
  168. if (type[0] === "_") return "continue";
  169. if (t.TYPES.indexOf(type) < 0) t.TYPES.push(type);
  170. var virtualType = virtualTypes[type];
  171. NodePath.prototype["is" + type] = function (opts) {
  172. return virtualType.checkPath(this, opts);
  173. };
  174. };
  175. for (var type in virtualTypes) {
  176. var _ret = _loop(type);
  177. if (_ret === "continue") continue;
  178. }
  179. module.exports = exports["default"];