index.js 5.5 KB

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