index.js 5.3 KB

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