index.js 5.2 KB

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