index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 = exports.REMOVED = 1 << 0;
  31. const SHOULD_STOP = exports.SHOULD_STOP = 1 << 1;
  32. const SHOULD_SKIP = exports.SHOULD_SKIP = 1 << 2;
  33. class NodePath {
  34. constructor(hub, parent) {
  35. this.contexts = [];
  36. this.state = null;
  37. this.opts = null;
  38. this._traverseFlags = 0;
  39. this.skipKeys = null;
  40. this.parentPath = null;
  41. this.container = null;
  42. this.listKey = null;
  43. this.key = null;
  44. this.node = null;
  45. this.type = null;
  46. this.parent = parent;
  47. this.hub = hub;
  48. this.data = null;
  49. this.context = null;
  50. this.scope = null;
  51. }
  52. static get({
  53. hub,
  54. parentPath,
  55. parent,
  56. container,
  57. listKey,
  58. key
  59. }) {
  60. if (!hub && parentPath) {
  61. hub = parentPath.hub;
  62. }
  63. if (!parent) {
  64. throw new Error("To get a node path the parent needs to exist");
  65. }
  66. const targetNode = container[key];
  67. const paths = cache.getOrCreateCachedPaths(hub, parent);
  68. let path = paths.get(targetNode);
  69. if (!path) {
  70. path = new NodePath(hub, parent);
  71. if (targetNode) paths.set(targetNode, path);
  72. }
  73. path.setup(parentPath, container, listKey, key);
  74. return path;
  75. }
  76. getScope(scope) {
  77. return this.isScope() ? new _index2.default(this) : scope;
  78. }
  79. setData(key, val) {
  80. if (this.data == null) {
  81. this.data = Object.create(null);
  82. }
  83. return this.data[key] = val;
  84. }
  85. getData(key, def) {
  86. if (this.data == null) {
  87. this.data = Object.create(null);
  88. }
  89. let val = this.data[key];
  90. if (val === undefined && def !== undefined) val = this.data[key] = def;
  91. return val;
  92. }
  93. hasNode() {
  94. return this.node != null;
  95. }
  96. buildCodeFrameError(msg, Error = SyntaxError) {
  97. return this.hub.buildError(this.node, msg, Error);
  98. }
  99. traverse(visitor, state) {
  100. (0, _index.default)(this.node, visitor, this.scope, state, this);
  101. }
  102. set(key, node) {
  103. validate(this.node, key, node);
  104. this.node[key] = node;
  105. }
  106. getPathLocation() {
  107. const parts = [];
  108. let path = this;
  109. do {
  110. let key = path.key;
  111. if (path.inList) key = `${path.listKey}[${key}]`;
  112. parts.unshift(key);
  113. } while (path = path.parentPath);
  114. return parts.join(".");
  115. }
  116. debug(message) {
  117. if (!debug.enabled) return;
  118. debug(`${this.getPathLocation()} ${this.type}: ${message}`);
  119. }
  120. toString() {
  121. return (0, _generator.default)(this.node).code;
  122. }
  123. get inList() {
  124. return !!this.listKey;
  125. }
  126. set inList(inList) {
  127. if (!inList) {
  128. this.listKey = null;
  129. }
  130. }
  131. get parentKey() {
  132. return this.listKey || this.key;
  133. }
  134. get shouldSkip() {
  135. return !!(this._traverseFlags & SHOULD_SKIP);
  136. }
  137. set shouldSkip(v) {
  138. if (v) {
  139. this._traverseFlags |= SHOULD_SKIP;
  140. } else {
  141. this._traverseFlags &= ~SHOULD_SKIP;
  142. }
  143. }
  144. get shouldStop() {
  145. return !!(this._traverseFlags & SHOULD_STOP);
  146. }
  147. set shouldStop(v) {
  148. if (v) {
  149. this._traverseFlags |= SHOULD_STOP;
  150. } else {
  151. this._traverseFlags &= ~SHOULD_STOP;
  152. }
  153. }
  154. get removed() {
  155. return !!(this._traverseFlags & REMOVED);
  156. }
  157. set removed(v) {
  158. if (v) {
  159. this._traverseFlags |= REMOVED;
  160. } else {
  161. this._traverseFlags &= ~REMOVED;
  162. }
  163. }
  164. }
  165. 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);
  166. {
  167. NodePath.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo;
  168. }
  169. for (const type of t.TYPES) {
  170. const typeKey = `is${type}`;
  171. const fn = t[typeKey];
  172. NodePath.prototype[typeKey] = function (opts) {
  173. return fn(this.node, opts);
  174. };
  175. NodePath.prototype[`assert${type}`] = function (opts) {
  176. if (!fn(this.node, opts)) {
  177. throw new TypeError(`Expected node path of type ${type}`);
  178. }
  179. };
  180. }
  181. Object.assign(NodePath.prototype, NodePath_virtual_types_validator);
  182. for (const type of Object.keys(virtualTypes)) {
  183. if (type[0] === "_") continue;
  184. if (!t.TYPES.includes(type)) t.TYPES.push(type);
  185. }
  186. var _default = exports.default = NodePath;
  187. //# sourceMappingURL=index.js.map