You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
6.7 KiB

4 years ago
  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 = parent;
  37. this.hub = hub;
  38. this.contexts = [];
  39. this.data = null;
  40. this._traverseFlags = 0;
  41. this.state = null;
  42. this.opts = null;
  43. this.skipKeys = null;
  44. this.parentPath = null;
  45. this.context = null;
  46. this.container = null;
  47. this.listKey = null;
  48. this.key = null;
  49. this.node = null;
  50. this.scope = null;
  51. this.type = 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. const paths = _cache.path.get(parent) || [];
  69. if (!_cache.path.has(parent)) {
  70. _cache.path.set(parent, paths);
  71. }
  72. let path;
  73. for (let i = 0; i < paths.length; i++) {
  74. const pathCheck = paths[i];
  75. if (pathCheck.node === targetNode) {
  76. path = pathCheck;
  77. break;
  78. }
  79. }
  80. if (!path) {
  81. path = new NodePath(hub, parent);
  82. paths.push(path);
  83. }
  84. path.setup(parentPath, container, listKey, key);
  85. return path;
  86. }
  87. getScope(scope) {
  88. return this.isScope() ? new _scope.default(this) : scope;
  89. }
  90. setData(key, val) {
  91. if (this.data == null) {
  92. this.data = Object.create(null);
  93. }
  94. return this.data[key] = val;
  95. }
  96. getData(key, def) {
  97. if (this.data == null) {
  98. this.data = Object.create(null);
  99. }
  100. let val = this.data[key];
  101. if (val === undefined && def !== undefined) val = this.data[key] = def;
  102. return val;
  103. }
  104. buildCodeFrameError(msg, Error = SyntaxError) {
  105. return this.hub.buildError(this.node, msg, Error);
  106. }
  107. traverse(visitor, state) {
  108. (0, _index.default)(this.node, visitor, this.scope, state, this);
  109. }
  110. set(key, node) {
  111. t.validate(this.node, key, node);
  112. this.node[key] = node;
  113. }
  114. getPathLocation() {
  115. const parts = [];
  116. let path = this;
  117. do {
  118. let 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. debug(message) {
  125. if (!debug.enabled) return;
  126. debug(`${this.getPathLocation()} ${this.type}: ${message}`);
  127. }
  128. toString() {
  129. return (0, _generator.default)(this.node).code;
  130. }
  131. get inList() {
  132. return !!this.listKey;
  133. }
  134. set inList(inList) {
  135. if (!inList) {
  136. this.listKey = null;
  137. }
  138. }
  139. get parentKey() {
  140. return this.listKey || this.key;
  141. }
  142. get shouldSkip() {
  143. return !!(this._traverseFlags & SHOULD_SKIP);
  144. }
  145. set shouldSkip(v) {
  146. if (v) {
  147. this._traverseFlags |= SHOULD_SKIP;
  148. } else {
  149. this._traverseFlags &= ~SHOULD_SKIP;
  150. }
  151. }
  152. get shouldStop() {
  153. return !!(this._traverseFlags & SHOULD_STOP);
  154. }
  155. set shouldStop(v) {
  156. if (v) {
  157. this._traverseFlags |= SHOULD_STOP;
  158. } else {
  159. this._traverseFlags &= ~SHOULD_STOP;
  160. }
  161. }
  162. get removed() {
  163. return !!(this._traverseFlags & REMOVED);
  164. }
  165. set removed(v) {
  166. if (v) {
  167. this._traverseFlags |= REMOVED;
  168. } else {
  169. this._traverseFlags &= ~REMOVED;
  170. }
  171. }
  172. }
  173. exports.default = NodePath;
  174. 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);
  175. for (const type of t.TYPES) {
  176. const typeKey = `is${type}`;
  177. const fn = t[typeKey];
  178. NodePath.prototype[typeKey] = function (opts) {
  179. return fn(this.node, opts);
  180. };
  181. NodePath.prototype[`assert${type}`] = function (opts) {
  182. if (!fn(this.node, opts)) {
  183. throw new TypeError(`Expected node path of type ${type}`);
  184. }
  185. };
  186. }
  187. for (const type of Object.keys(virtualTypes)) {
  188. if (type[0] === "_") continue;
  189. if (t.TYPES.indexOf(type) < 0) t.TYPES.push(type);
  190. const virtualType = virtualTypes[type];
  191. NodePath.prototype[`is${type}`] = function (opts) {
  192. return virtualType.checkPath(this, opts);
  193. };
  194. }