index.js 7.2 KB

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