index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.SHOULD_SKIP = exports.SHOULD_STOP = 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. 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. 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. for (const type of Object.keys(virtualTypes)) {
  182. if (type[0] === "_") continue;
  183. if (t.TYPES.indexOf(type) < 0) t.TYPES.push(type);
  184. const virtualType = virtualTypes[type];
  185. NodePath.prototype[`is${type}`] = function (opts) {
  186. return virtualType.checkPath(this, opts);
  187. };
  188. }
  189. var _default = NodePath;
  190. exports.default = _default;