index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 =
  70. container[key];
  71. let paths = _cache.path.get(parent);
  72. if (!paths) {
  73. paths = new Map();
  74. _cache.path.set(parent, paths);
  75. }
  76. let path = paths.get(targetNode);
  77. if (!path) {
  78. path = new NodePath(hub, parent);
  79. if (targetNode) paths.set(targetNode, path);
  80. }
  81. path.setup(parentPath, container, listKey, key);
  82. return path;
  83. }
  84. getScope(scope) {
  85. return this.isScope() ? new _scope.default(this) : scope;
  86. }
  87. setData(key, val) {
  88. if (this.data == null) {
  89. this.data = Object.create(null);
  90. }
  91. return this.data[key] = val;
  92. }
  93. getData(key, def) {
  94. if (this.data == null) {
  95. this.data = Object.create(null);
  96. }
  97. let val = this.data[key];
  98. if (val === undefined && def !== undefined) val = this.data[key] = def;
  99. return val;
  100. }
  101. hasNode() {
  102. return this.node != null;
  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. 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. 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);
  174. {
  175. NodePath.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo;
  176. }
  177. for (const type of t.TYPES) {
  178. const typeKey = `is${type}`;
  179. const fn = t[typeKey];
  180. NodePath.prototype[typeKey] = function (opts) {
  181. return fn(this.node, opts);
  182. };
  183. NodePath.prototype[`assert${type}`] = function (opts) {
  184. if (!fn(this.node, opts)) {
  185. throw new TypeError(`Expected node path of type ${type}`);
  186. }
  187. };
  188. }
  189. Object.assign(NodePath.prototype, NodePath_virtual_types_validator);
  190. for (const type of Object.keys(virtualTypes)) {
  191. if (type[0] === "_") continue;
  192. if (!t.TYPES.includes(type)) t.TYPES.push(type);
  193. }
  194. var _default = NodePath;
  195. exports.default = _default;
  196. //# sourceMappingURL=index.js.map