index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. isBaseType: NodePath_inference.isBaseType,
  166. couldBeBaseType: NodePath_inference.couldBeBaseType,
  167. baseTypeStrictlyMatches: NodePath_inference.baseTypeStrictlyMatches,
  168. isGenericType: NodePath_inference.isGenericType,
  169. replaceWithMultiple: NodePath_replacement.replaceWithMultiple,
  170. replaceWithSourceString: NodePath_replacement.replaceWithSourceString,
  171. replaceWith: NodePath_replacement.replaceWith,
  172. replaceExpressionWithStatements: NodePath_replacement.replaceExpressionWithStatements,
  173. replaceInline: NodePath_replacement.replaceInline,
  174. evaluateTruthy: NodePath_evaluation.evaluateTruthy,
  175. evaluate: NodePath_evaluation.evaluate,
  176. toComputedKey: NodePath_conversion.toComputedKey,
  177. ensureBlock: NodePath_conversion.ensureBlock,
  178. unwrapFunctionEnvironment: NodePath_conversion.unwrapFunctionEnvironment,
  179. arrowFunctionToExpression: NodePath_conversion.arrowFunctionToExpression,
  180. splitExportDeclaration: NodePath_conversion.splitExportDeclaration,
  181. ensureFunctionName: NodePath_conversion.ensureFunctionName,
  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. isConstantExpression: NodePath_introspection.isConstantExpression,
  199. isInStrictMode: NodePath_introspection.isInStrictMode,
  200. call: NodePath_context.call,
  201. isDenylisted: NodePath_context.isDenylisted,
  202. isBlacklisted: NodePath_context.isBlacklisted,
  203. visit: NodePath_context.visit,
  204. skip: NodePath_context.skip,
  205. skipKey: NodePath_context.skipKey,
  206. stop: NodePath_context.stop,
  207. setScope: NodePath_context.setScope,
  208. setContext: NodePath_context.setContext,
  209. resync: NodePath_context.resync,
  210. popContext: NodePath_context.popContext,
  211. pushContext: NodePath_context.pushContext,
  212. setup: NodePath_context.setup,
  213. setKey: NodePath_context.setKey,
  214. requeue: NodePath_context.requeue,
  215. requeueComputedKeyAndDecorators: NodePath_context.requeueComputedKeyAndDecorators,
  216. remove: NodePath_removal.remove,
  217. insertBefore: NodePath_modification.insertBefore,
  218. insertAfter: NodePath_modification.insertAfter,
  219. updateSiblingKeys: NodePath_modification.updateSiblingKeys,
  220. unshiftContainer: NodePath_modification.unshiftContainer,
  221. pushContainer: NodePath_modification.pushContainer,
  222. hoist: NodePath_modification.hoist,
  223. getOpposite: NodePath_family.getOpposite,
  224. getCompletionRecords: NodePath_family.getCompletionRecords,
  225. getSibling: NodePath_family.getSibling,
  226. getPrevSibling: NodePath_family.getPrevSibling,
  227. getNextSibling: NodePath_family.getNextSibling,
  228. getAllNextSiblings: NodePath_family.getAllNextSiblings,
  229. getAllPrevSiblings: NodePath_family.getAllPrevSiblings,
  230. get: NodePath_family.get,
  231. getAssignmentIdentifiers: NodePath_family.getAssignmentIdentifiers,
  232. getBindingIdentifiers: NodePath_family.getBindingIdentifiers,
  233. getOuterBindingIdentifiers: NodePath_family.getOuterBindingIdentifiers,
  234. getBindingIdentifierPaths: NodePath_family.getBindingIdentifierPaths,
  235. getOuterBindingIdentifierPaths: NodePath_family.getOuterBindingIdentifierPaths,
  236. shareCommentsWithSiblings: NodePath_comments.shareCommentsWithSiblings,
  237. addComment: NodePath_comments.addComment,
  238. addComments: NodePath_comments.addComments
  239. };
  240. Object.assign(NodePath_Final.prototype, methods);
  241. {
  242. NodePath_Final.prototype.arrowFunctionToShadowed = NodePath_conversion[String("arrowFunctionToShadowed")];
  243. }
  244. {
  245. NodePath_Final.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo;
  246. }
  247. {
  248. NodePath_Final.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo;
  249. Object.assign(NodePath_Final.prototype, {
  250. _getTypeAnnotation: NodePath_inference._getTypeAnnotation,
  251. _replaceWith: NodePath_replacement._replaceWith,
  252. _resolve: NodePath_introspection._resolve,
  253. _call: NodePath_context._call,
  254. _resyncParent: NodePath_context._resyncParent,
  255. _resyncKey: NodePath_context._resyncKey,
  256. _resyncList: NodePath_context._resyncList,
  257. _resyncRemoved: NodePath_context._resyncRemoved,
  258. _getQueueContexts: NodePath_context._getQueueContexts,
  259. _removeFromScope: NodePath_removal._removeFromScope,
  260. _callRemovalHooks: NodePath_removal._callRemovalHooks,
  261. _remove: NodePath_removal._remove,
  262. _markRemoved: NodePath_removal._markRemoved,
  263. _assertUnremoved: NodePath_removal._assertUnremoved,
  264. _containerInsert: NodePath_modification._containerInsert,
  265. _containerInsertBefore: NodePath_modification._containerInsertBefore,
  266. _containerInsertAfter: NodePath_modification._containerInsertAfter,
  267. _verifyNodeList: NodePath_modification._verifyNodeList,
  268. _getKey: NodePath_family._getKey,
  269. _getPattern: NodePath_family._getPattern
  270. });
  271. }
  272. for (const type of t.TYPES) {
  273. const typeKey = `is${type}`;
  274. const fn = t[typeKey];
  275. NodePath_Final.prototype[typeKey] = function (opts) {
  276. return fn(this.node, opts);
  277. };
  278. NodePath_Final.prototype[`assert${type}`] = function (opts) {
  279. if (!fn(this.node, opts)) {
  280. throw new TypeError(`Expected node path of type ${type}`);
  281. }
  282. };
  283. }
  284. Object.assign(NodePath_Final.prototype, NodePath_virtual_types_validator);
  285. for (const type of Object.keys(virtualTypes)) {
  286. if (type[0] === "_") continue;
  287. if (!t.TYPES.includes(type)) t.TYPES.push(type);
  288. }
  289. //# sourceMappingURL=index.js.map