index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. const NodePath_Final = exports.default = 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. get removed() {
  53. return (this._traverseFlags & 1) > 0;
  54. }
  55. set removed(v) {
  56. if (v) this._traverseFlags |= 1;else this._traverseFlags &= -2;
  57. }
  58. get shouldStop() {
  59. return (this._traverseFlags & 2) > 0;
  60. }
  61. set shouldStop(v) {
  62. if (v) this._traverseFlags |= 2;else this._traverseFlags &= -3;
  63. }
  64. get shouldSkip() {
  65. return (this._traverseFlags & 4) > 0;
  66. }
  67. set shouldSkip(v) {
  68. if (v) this._traverseFlags |= 4;else this._traverseFlags &= -5;
  69. }
  70. static get({
  71. hub,
  72. parentPath,
  73. parent,
  74. container,
  75. listKey,
  76. key
  77. }) {
  78. if (!hub && parentPath) {
  79. hub = parentPath.hub;
  80. }
  81. if (!parent) {
  82. throw new Error("To get a node path the parent needs to exist");
  83. }
  84. const targetNode = container[key];
  85. const paths = cache.getOrCreateCachedPaths(hub, parent);
  86. let path = paths.get(targetNode);
  87. if (!path) {
  88. path = new NodePath(hub, parent);
  89. if (targetNode) paths.set(targetNode, path);
  90. }
  91. path.setup(parentPath, container, listKey, key);
  92. return path;
  93. }
  94. getScope(scope) {
  95. return this.isScope() ? new _index2.default(this) : scope;
  96. }
  97. setData(key, val) {
  98. if (this.data == null) {
  99. this.data = Object.create(null);
  100. }
  101. return this.data[key] = val;
  102. }
  103. getData(key, def) {
  104. if (this.data == null) {
  105. this.data = Object.create(null);
  106. }
  107. let val = this.data[key];
  108. if (val === undefined && def !== undefined) val = this.data[key] = def;
  109. return val;
  110. }
  111. hasNode() {
  112. return this.node != null;
  113. }
  114. buildCodeFrameError(msg, Error = SyntaxError) {
  115. return this.hub.buildError(this.node, msg, Error);
  116. }
  117. traverse(visitor, state) {
  118. (0, _index.default)(this.node, visitor, this.scope, state, this);
  119. }
  120. set(key, node) {
  121. validate(this.node, key, node);
  122. this.node[key] = node;
  123. }
  124. getPathLocation() {
  125. const parts = [];
  126. let path = this;
  127. do {
  128. let key = path.key;
  129. if (path.inList) key = `${path.listKey}[${key}]`;
  130. parts.unshift(key);
  131. } while (path = path.parentPath);
  132. return parts.join(".");
  133. }
  134. debug(message) {
  135. if (!debug.enabled) return;
  136. debug(`${this.getPathLocation()} ${this.type}: ${message}`);
  137. }
  138. toString() {
  139. return (0, _generator.default)(this.node).code;
  140. }
  141. get inList() {
  142. return !!this.listKey;
  143. }
  144. set inList(inList) {
  145. if (!inList) {
  146. this.listKey = null;
  147. }
  148. }
  149. get parentKey() {
  150. return this.listKey || this.key;
  151. }
  152. };
  153. const methods = {
  154. findParent: NodePath_ancestry.findParent,
  155. find: NodePath_ancestry.find,
  156. getFunctionParent: NodePath_ancestry.getFunctionParent,
  157. getStatementParent: NodePath_ancestry.getStatementParent,
  158. getEarliestCommonAncestorFrom: NodePath_ancestry.getEarliestCommonAncestorFrom,
  159. getDeepestCommonAncestorFrom: NodePath_ancestry.getDeepestCommonAncestorFrom,
  160. getAncestry: NodePath_ancestry.getAncestry,
  161. isAncestor: NodePath_ancestry.isAncestor,
  162. isDescendant: NodePath_ancestry.isDescendant,
  163. inType: NodePath_ancestry.inType,
  164. getTypeAnnotation: NodePath_inference.getTypeAnnotation,
  165. _getTypeAnnotation: NodePath_inference._getTypeAnnotation,
  166. isBaseType: NodePath_inference.isBaseType,
  167. couldBeBaseType: NodePath_inference.couldBeBaseType,
  168. baseTypeStrictlyMatches: NodePath_inference.baseTypeStrictlyMatches,
  169. isGenericType: NodePath_inference.isGenericType,
  170. replaceWithMultiple: NodePath_replacement.replaceWithMultiple,
  171. replaceWithSourceString: NodePath_replacement.replaceWithSourceString,
  172. replaceWith: NodePath_replacement.replaceWith,
  173. _replaceWith: NodePath_replacement._replaceWith,
  174. replaceExpressionWithStatements: NodePath_replacement.replaceExpressionWithStatements,
  175. replaceInline: NodePath_replacement.replaceInline,
  176. evaluateTruthy: NodePath_evaluation.evaluateTruthy,
  177. evaluate: NodePath_evaluation.evaluate,
  178. toComputedKey: NodePath_conversion.toComputedKey,
  179. ensureBlock: NodePath_conversion.ensureBlock,
  180. unwrapFunctionEnvironment: NodePath_conversion.unwrapFunctionEnvironment,
  181. arrowFunctionToExpression: NodePath_conversion.arrowFunctionToExpression,
  182. matchesPattern: NodePath_introspection.matchesPattern,
  183. has: NodePath_introspection.has,
  184. isStatic: NodePath_introspection.isStatic,
  185. is: NodePath_introspection.is,
  186. isnt: NodePath_introspection.isnt,
  187. equals: NodePath_introspection.equals,
  188. isNodeType: NodePath_introspection.isNodeType,
  189. canHaveVariableDeclarationOrExpression: NodePath_introspection.canHaveVariableDeclarationOrExpression,
  190. canSwapBetweenExpressionAndStatement: NodePath_introspection.canSwapBetweenExpressionAndStatement,
  191. isCompletionRecord: NodePath_introspection.isCompletionRecord,
  192. isStatementOrBlock: NodePath_introspection.isStatementOrBlock,
  193. referencesImport: NodePath_introspection.referencesImport,
  194. getSource: NodePath_introspection.getSource,
  195. willIMaybeExecuteBefore: NodePath_introspection.willIMaybeExecuteBefore,
  196. _guessExecutionStatusRelativeTo: NodePath_introspection._guessExecutionStatusRelativeTo,
  197. resolve: NodePath_introspection.resolve,
  198. _resolve: NodePath_introspection._resolve,
  199. isConstantExpression: NodePath_introspection.isConstantExpression,
  200. isInStrictMode: NodePath_introspection.isInStrictMode,
  201. call: NodePath_context.call,
  202. _call: NodePath_context._call,
  203. isDenylisted: NodePath_context.isDenylisted,
  204. isBlacklisted: NodePath_context.isBlacklisted,
  205. visit: NodePath_context.visit,
  206. skip: NodePath_context.skip,
  207. skipKey: NodePath_context.skipKey,
  208. stop: NodePath_context.stop,
  209. setScope: NodePath_context.setScope,
  210. setContext: NodePath_context.setContext,
  211. resync: NodePath_context.resync,
  212. _resyncParent: NodePath_context._resyncParent,
  213. _resyncKey: NodePath_context._resyncKey,
  214. _resyncList: NodePath_context._resyncList,
  215. _resyncRemoved: NodePath_context._resyncRemoved,
  216. popContext: NodePath_context.popContext,
  217. pushContext: NodePath_context.pushContext,
  218. setup: NodePath_context.setup,
  219. setKey: NodePath_context.setKey,
  220. requeue: NodePath_context.requeue,
  221. _getQueueContexts: NodePath_context._getQueueContexts,
  222. remove: NodePath_removal.remove,
  223. _removeFromScope: NodePath_removal._removeFromScope,
  224. _callRemovalHooks: NodePath_removal._callRemovalHooks,
  225. _remove: NodePath_removal._remove,
  226. _markRemoved: NodePath_removal._markRemoved,
  227. _assertUnremoved: NodePath_removal._assertUnremoved,
  228. insertBefore: NodePath_modification.insertBefore,
  229. _containerInsert: NodePath_modification._containerInsert,
  230. _containerInsertBefore: NodePath_modification._containerInsertBefore,
  231. _containerInsertAfter: NodePath_modification._containerInsertAfter,
  232. insertAfter: NodePath_modification.insertAfter,
  233. updateSiblingKeys: NodePath_modification.updateSiblingKeys,
  234. _verifyNodeList: NodePath_modification._verifyNodeList,
  235. unshiftContainer: NodePath_modification.unshiftContainer,
  236. pushContainer: NodePath_modification.pushContainer,
  237. hoist: NodePath_modification.hoist,
  238. getOpposite: NodePath_family.getOpposite,
  239. getCompletionRecords: NodePath_family.getCompletionRecords,
  240. getSibling: NodePath_family.getSibling,
  241. getPrevSibling: NodePath_family.getPrevSibling,
  242. getNextSibling: NodePath_family.getNextSibling,
  243. getAllNextSiblings: NodePath_family.getAllNextSiblings,
  244. getAllPrevSiblings: NodePath_family.getAllPrevSiblings,
  245. get: NodePath_family.get,
  246. _getKey: NodePath_family._getKey,
  247. _getPattern: NodePath_family._getPattern,
  248. getBindingIdentifiers: NodePath_family.getBindingIdentifiers,
  249. getOuterBindingIdentifiers: NodePath_family.getOuterBindingIdentifiers,
  250. getBindingIdentifierPaths: NodePath_family.getBindingIdentifierPaths,
  251. getOuterBindingIdentifierPaths: NodePath_family.getOuterBindingIdentifierPaths,
  252. shareCommentsWithSiblings: NodePath_comments.shareCommentsWithSiblings,
  253. addComment: NodePath_comments.addComment,
  254. addComments: NodePath_comments.addComments
  255. };
  256. Object.assign(NodePath_Final.prototype, methods);
  257. {
  258. NodePath_Final.prototype.arrowFunctionToShadowed = NodePath_conversion[String("arrowFunctionToShadowed")];
  259. }
  260. {
  261. NodePath_Final.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo;
  262. }
  263. for (const type of t.TYPES) {
  264. const typeKey = `is${type}`;
  265. const fn = t[typeKey];
  266. NodePath_Final.prototype[typeKey] = function (opts) {
  267. return fn(this.node, opts);
  268. };
  269. NodePath_Final.prototype[`assert${type}`] = function (opts) {
  270. if (!fn(this.node, opts)) {
  271. throw new TypeError(`Expected node path of type ${type}`);
  272. }
  273. };
  274. }
  275. Object.assign(NodePath_Final.prototype, NodePath_virtual_types_validator);
  276. for (const type of Object.keys(virtualTypes)) {
  277. if (type[0] === "_") continue;
  278. if (!t.TYPES.includes(type)) t.TYPES.push(type);
  279. }
  280. //# sourceMappingURL=index.js.map