index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.js");
  7. var _debug = require("debug");
  8. var _index = require("../index.js");
  9. var _index2 = require("../scope/index.js");
  10. var _t = require("@babel/types");
  11. var t = _t;
  12. var cache = require("../cache.js");
  13. var _generator = require("@babel/generator");
  14. var NodePath_ancestry = require("./ancestry.js");
  15. var NodePath_inference = require("./inference/index.js");
  16. var NodePath_replacement = require("./replacement.js");
  17. var NodePath_evaluation = require("./evaluation.js");
  18. var NodePath_conversion = require("./conversion.js");
  19. var NodePath_introspection = require("./introspection.js");
  20. var NodePath_context = require("./context.js");
  21. var NodePath_removal = require("./removal.js");
  22. var NodePath_modification = require("./modification.js");
  23. var NodePath_family = require("./family.js");
  24. var NodePath_comments = require("./comments.js");
  25. var NodePath_virtual_types_validator = require("./lib/virtual-types-validator.js");
  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. const paths = cache.getOrCreateCachedPaths(hub, parent);
  71. let path = paths.get(targetNode);
  72. if (!path) {
  73. path = new NodePath(hub, parent);
  74. if (targetNode) paths.set(targetNode, path);
  75. }
  76. path.setup(parentPath, container, listKey, key);
  77. return path;
  78. }
  79. getScope(scope) {
  80. return this.isScope() ? new _index2.default(this) : scope;
  81. }
  82. setData(key, val) {
  83. if (this.data == null) {
  84. this.data = Object.create(null);
  85. }
  86. return this.data[key] = val;
  87. }
  88. getData(key, def) {
  89. if (this.data == null) {
  90. this.data = Object.create(null);
  91. }
  92. let val = this.data[key];
  93. if (val === undefined && def !== undefined) val = this.data[key] = def;
  94. return val;
  95. }
  96. hasNode() {
  97. return this.node != null;
  98. }
  99. buildCodeFrameError(msg, Error = SyntaxError) {
  100. return this.hub.buildError(this.node, msg, Error);
  101. }
  102. traverse(visitor, state) {
  103. (0, _index.default)(this.node, visitor, this.scope, state, this);
  104. }
  105. set(key, node) {
  106. validate(this.node, key, node);
  107. this.node[key] = node;
  108. }
  109. getPathLocation() {
  110. const parts = [];
  111. let path = this;
  112. do {
  113. let key = path.key;
  114. if (path.inList) key = `${path.listKey}[${key}]`;
  115. parts.unshift(key);
  116. } while (path = path.parentPath);
  117. return parts.join(".");
  118. }
  119. debug(message) {
  120. if (!debug.enabled) return;
  121. debug(`${this.getPathLocation()} ${this.type}: ${message}`);
  122. }
  123. toString() {
  124. return (0, _generator.default)(this.node).code;
  125. }
  126. get inList() {
  127. return !!this.listKey;
  128. }
  129. set inList(inList) {
  130. if (!inList) {
  131. this.listKey = null;
  132. }
  133. }
  134. get parentKey() {
  135. return this.listKey || this.key;
  136. }
  137. get shouldSkip() {
  138. return !!(this._traverseFlags & SHOULD_SKIP);
  139. }
  140. set shouldSkip(v) {
  141. if (v) {
  142. this._traverseFlags |= SHOULD_SKIP;
  143. } else {
  144. this._traverseFlags &= ~SHOULD_SKIP;
  145. }
  146. }
  147. get shouldStop() {
  148. return !!(this._traverseFlags & SHOULD_STOP);
  149. }
  150. set shouldStop(v) {
  151. if (v) {
  152. this._traverseFlags |= SHOULD_STOP;
  153. } else {
  154. this._traverseFlags &= ~SHOULD_STOP;
  155. }
  156. }
  157. get removed() {
  158. return !!(this._traverseFlags & REMOVED);
  159. }
  160. set removed(v) {
  161. if (v) {
  162. this._traverseFlags |= REMOVED;
  163. } else {
  164. this._traverseFlags &= ~REMOVED;
  165. }
  166. }
  167. }
  168. 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);
  169. {
  170. NodePath.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo;
  171. }
  172. for (const type of t.TYPES) {
  173. const typeKey = `is${type}`;
  174. const fn = t[typeKey];
  175. NodePath.prototype[typeKey] = function (opts) {
  176. return fn(this.node, opts);
  177. };
  178. NodePath.prototype[`assert${type}`] = function (opts) {
  179. if (!fn(this.node, opts)) {
  180. throw new TypeError(`Expected node path of type ${type}`);
  181. }
  182. };
  183. }
  184. Object.assign(NodePath.prototype, NodePath_virtual_types_validator);
  185. for (const type of Object.keys(virtualTypes)) {
  186. if (type[0] === "_") continue;
  187. if (!t.TYPES.includes(type)) t.TYPES.push(type);
  188. }
  189. var _default = NodePath;
  190. exports.default = _default;
  191. //# sourceMappingURL=index.js.map