index.js 6.3 KB

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