index.js 6.5 KB

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