introspection.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports._guessExecutionStatusRelativeTo = _guessExecutionStatusRelativeTo;
  6. exports._resolve = _resolve;
  7. exports.canHaveVariableDeclarationOrExpression = canHaveVariableDeclarationOrExpression;
  8. exports.canSwapBetweenExpressionAndStatement = canSwapBetweenExpressionAndStatement;
  9. exports.getSource = getSource;
  10. exports.isCompletionRecord = isCompletionRecord;
  11. exports.isConstantExpression = isConstantExpression;
  12. exports.isInStrictMode = isInStrictMode;
  13. exports.isNodeType = isNodeType;
  14. exports.isStatementOrBlock = isStatementOrBlock;
  15. exports.isStatic = isStatic;
  16. exports.matchesPattern = matchesPattern;
  17. exports.referencesImport = referencesImport;
  18. exports.resolve = resolve;
  19. exports.willIMaybeExecuteBefore = willIMaybeExecuteBefore;
  20. var _t = require("@babel/types");
  21. const {
  22. STATEMENT_OR_BLOCK_KEYS,
  23. VISITOR_KEYS,
  24. isBlockStatement,
  25. isExpression,
  26. isIdentifier,
  27. isLiteral,
  28. isStringLiteral,
  29. isType,
  30. matchesPattern: _matchesPattern
  31. } = _t;
  32. function matchesPattern(pattern, allowPartial) {
  33. return _matchesPattern(this.node, pattern, allowPartial);
  34. }
  35. exports.has = function has(key) {
  36. var _this$node;
  37. const val = (_this$node = this.node) == null ? void 0 : _this$node[key];
  38. if (val && Array.isArray(val)) {
  39. return !!val.length;
  40. } else {
  41. return !!val;
  42. }
  43. };
  44. function isStatic() {
  45. return this.scope.isStatic(this.node);
  46. }
  47. exports.is = exports.has;
  48. exports.isnt = function isnt(key) {
  49. return !this.has(key);
  50. };
  51. exports.equals = function equals(key, value) {
  52. return this.node[key] === value;
  53. };
  54. function isNodeType(type) {
  55. return isType(this.type, type);
  56. }
  57. function canHaveVariableDeclarationOrExpression() {
  58. return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
  59. }
  60. function canSwapBetweenExpressionAndStatement(replacement) {
  61. if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
  62. return false;
  63. }
  64. if (this.isExpression()) {
  65. return isBlockStatement(replacement);
  66. } else if (this.isBlockStatement()) {
  67. return isExpression(replacement);
  68. }
  69. return false;
  70. }
  71. function isCompletionRecord(allowInsideFunction) {
  72. let path = this;
  73. let first = true;
  74. do {
  75. const {
  76. type,
  77. container
  78. } = path;
  79. if (!first && (path.isFunction() || type === "StaticBlock")) {
  80. return !!allowInsideFunction;
  81. }
  82. first = false;
  83. if (Array.isArray(container) && path.key !== container.length - 1) {
  84. return false;
  85. }
  86. } while ((path = path.parentPath) && !path.isProgram() && !path.isDoExpression());
  87. return true;
  88. }
  89. function isStatementOrBlock() {
  90. if (this.parentPath.isLabeledStatement() || isBlockStatement(this.container)) {
  91. return false;
  92. } else {
  93. return STATEMENT_OR_BLOCK_KEYS.includes(this.key);
  94. }
  95. }
  96. function referencesImport(moduleSource, importName) {
  97. if (!this.isReferencedIdentifier()) {
  98. if (this.isJSXMemberExpression() && this.node.property.name === importName || (this.isMemberExpression() || this.isOptionalMemberExpression()) && (this.node.computed ? isStringLiteral(this.node.property, {
  99. value: importName
  100. }) : this.node.property.name === importName)) {
  101. const object = this.get("object");
  102. return object.isReferencedIdentifier() && object.referencesImport(moduleSource, "*");
  103. }
  104. return false;
  105. }
  106. const binding = this.scope.getBinding(this.node.name);
  107. if ((binding == null ? void 0 : binding.kind) !== "module") return false;
  108. const path = binding.path;
  109. const parent = path.parentPath;
  110. if (!parent.isImportDeclaration()) return false;
  111. if (parent.node.source.value === moduleSource) {
  112. if (!importName) return true;
  113. } else {
  114. return false;
  115. }
  116. if (path.isImportDefaultSpecifier() && importName === "default") {
  117. return true;
  118. }
  119. if (path.isImportNamespaceSpecifier() && importName === "*") {
  120. return true;
  121. }
  122. if (path.isImportSpecifier() && isIdentifier(path.node.imported, {
  123. name: importName
  124. })) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. function getSource() {
  130. const node = this.node;
  131. if (node.end) {
  132. const code = this.hub.getCode();
  133. if (code) return code.slice(node.start, node.end);
  134. }
  135. return "";
  136. }
  137. function willIMaybeExecuteBefore(target) {
  138. return this._guessExecutionStatusRelativeTo(target) !== "after";
  139. }
  140. function getOuterFunction(path) {
  141. return path.isProgram() ? path : (path.parentPath.scope.getFunctionParent() || path.parentPath.scope.getProgramParent()).path;
  142. }
  143. function isExecutionUncertain(type, key) {
  144. switch (type) {
  145. case "LogicalExpression":
  146. return key === "right";
  147. case "ConditionalExpression":
  148. case "IfStatement":
  149. return key === "consequent" || key === "alternate";
  150. case "WhileStatement":
  151. case "DoWhileStatement":
  152. case "ForInStatement":
  153. case "ForOfStatement":
  154. return key === "body";
  155. case "ForStatement":
  156. return key === "body" || key === "update";
  157. case "SwitchStatement":
  158. return key === "cases";
  159. case "TryStatement":
  160. return key === "handler";
  161. case "AssignmentPattern":
  162. return key === "right";
  163. case "OptionalMemberExpression":
  164. return key === "property";
  165. case "OptionalCallExpression":
  166. return key === "arguments";
  167. default:
  168. return false;
  169. }
  170. }
  171. function isExecutionUncertainInList(paths, maxIndex) {
  172. for (let i = 0; i < maxIndex; i++) {
  173. const path = paths[i];
  174. if (isExecutionUncertain(path.parent.type, path.parentKey)) {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. const SYMBOL_CHECKING = Symbol();
  181. function _guessExecutionStatusRelativeTo(target) {
  182. return _guessExecutionStatusRelativeToCached(this, target, new Map());
  183. }
  184. function _guessExecutionStatusRelativeToCached(base, target, cache) {
  185. const funcParent = {
  186. this: getOuterFunction(base),
  187. target: getOuterFunction(target)
  188. };
  189. if (funcParent.target.node !== funcParent.this.node) {
  190. return _guessExecutionStatusRelativeToDifferentFunctionsCached(base, funcParent.target, cache);
  191. }
  192. const paths = {
  193. target: target.getAncestry(),
  194. this: base.getAncestry()
  195. };
  196. if (paths.target.includes(base)) return "after";
  197. if (paths.this.includes(target)) return "before";
  198. let commonPath;
  199. const commonIndex = {
  200. target: 0,
  201. this: 0
  202. };
  203. while (!commonPath && commonIndex.this < paths.this.length) {
  204. const path = paths.this[commonIndex.this];
  205. commonIndex.target = paths.target.indexOf(path);
  206. if (commonIndex.target >= 0) {
  207. commonPath = path;
  208. } else {
  209. commonIndex.this++;
  210. }
  211. }
  212. if (!commonPath) {
  213. throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program.");
  214. }
  215. if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) {
  216. return "unknown";
  217. }
  218. const divergence = {
  219. this: paths.this[commonIndex.this - 1],
  220. target: paths.target[commonIndex.target - 1]
  221. };
  222. if (divergence.target.listKey && divergence.this.listKey && divergence.target.container === divergence.this.container) {
  223. return divergence.target.key > divergence.this.key ? "before" : "after";
  224. }
  225. const keys = VISITOR_KEYS[commonPath.type];
  226. const keyPosition = {
  227. this: keys.indexOf(divergence.this.parentKey),
  228. target: keys.indexOf(divergence.target.parentKey)
  229. };
  230. return keyPosition.target > keyPosition.this ? "before" : "after";
  231. }
  232. function _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache) {
  233. if (!target.isFunctionDeclaration()) {
  234. if (_guessExecutionStatusRelativeToCached(base, target, cache) === "before") {
  235. return "before";
  236. }
  237. return "unknown";
  238. } else if (target.parentPath.isExportDeclaration()) {
  239. return "unknown";
  240. }
  241. const binding = target.scope.getBinding(target.node.id.name);
  242. if (!binding.references) return "before";
  243. const referencePaths = binding.referencePaths;
  244. let allStatus;
  245. for (const path of referencePaths) {
  246. const childOfFunction = !!path.find(path => path.node === target.node);
  247. if (childOfFunction) continue;
  248. if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
  249. return "unknown";
  250. }
  251. const status = _guessExecutionStatusRelativeToCached(base, path, cache);
  252. if (allStatus && allStatus !== status) {
  253. return "unknown";
  254. } else {
  255. allStatus = status;
  256. }
  257. }
  258. return allStatus;
  259. }
  260. function _guessExecutionStatusRelativeToDifferentFunctionsCached(base, target, cache) {
  261. let nodeMap = cache.get(base.node);
  262. let cached;
  263. if (!nodeMap) {
  264. cache.set(base.node, nodeMap = new Map());
  265. } else if (cached = nodeMap.get(target.node)) {
  266. if (cached === SYMBOL_CHECKING) {
  267. return "unknown";
  268. }
  269. return cached;
  270. }
  271. nodeMap.set(target.node, SYMBOL_CHECKING);
  272. const result = _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache);
  273. nodeMap.set(target.node, result);
  274. return result;
  275. }
  276. function resolve(dangerous, resolved) {
  277. return _resolve.call(this, dangerous, resolved) || this;
  278. }
  279. function _resolve(dangerous, resolved) {
  280. var _resolved;
  281. if ((_resolved = resolved) != null && _resolved.includes(this)) return;
  282. resolved = resolved || [];
  283. resolved.push(this);
  284. if (this.isVariableDeclarator()) {
  285. if (this.get("id").isIdentifier()) {
  286. return this.get("init").resolve(dangerous, resolved);
  287. } else {}
  288. } else if (this.isReferencedIdentifier()) {
  289. const binding = this.scope.getBinding(this.node.name);
  290. if (!binding) return;
  291. if (!binding.constant) return;
  292. if (binding.kind === "module") return;
  293. if (binding.path !== this) {
  294. const ret = binding.path.resolve(dangerous, resolved);
  295. if (this.find(parent => parent.node === ret.node)) return;
  296. return ret;
  297. }
  298. } else if (this.isTypeCastExpression()) {
  299. return this.get("expression").resolve(dangerous, resolved);
  300. } else if (dangerous && this.isMemberExpression()) {
  301. const targetKey = this.toComputedKey();
  302. if (!isLiteral(targetKey)) return;
  303. const targetName = targetKey.value;
  304. const target = this.get("object").resolve(dangerous, resolved);
  305. if (target.isObjectExpression()) {
  306. const props = target.get("properties");
  307. for (const prop of props) {
  308. if (!prop.isProperty()) continue;
  309. const key = prop.get("key");
  310. let match = prop.isnt("computed") && key.isIdentifier({
  311. name: targetName
  312. });
  313. match = match || key.isLiteral({
  314. value: targetName
  315. });
  316. if (match) return prop.get("value").resolve(dangerous, resolved);
  317. }
  318. } else if (target.isArrayExpression() && !isNaN(+targetName)) {
  319. const elems = target.get("elements");
  320. const elem = elems[targetName];
  321. if (elem) return elem.resolve(dangerous, resolved);
  322. }
  323. }
  324. }
  325. function isConstantExpression() {
  326. if (this.isIdentifier()) {
  327. const binding = this.scope.getBinding(this.node.name);
  328. if (!binding) return false;
  329. return binding.constant;
  330. }
  331. if (this.isLiteral()) {
  332. if (this.isRegExpLiteral()) {
  333. return false;
  334. }
  335. if (this.isTemplateLiteral()) {
  336. return this.get("expressions").every(expression => expression.isConstantExpression());
  337. }
  338. return true;
  339. }
  340. if (this.isUnaryExpression()) {
  341. if (this.node.operator !== "void") {
  342. return false;
  343. }
  344. return this.get("argument").isConstantExpression();
  345. }
  346. if (this.isBinaryExpression()) {
  347. const {
  348. operator
  349. } = this.node;
  350. return operator !== "in" && operator !== "instanceof" && this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
  351. }
  352. if (this.isMemberExpression()) {
  353. return !this.node.computed && this.get("object").isIdentifier({
  354. name: "Symbol"
  355. }) && !this.scope.hasBinding("Symbol", {
  356. noGlobals: true
  357. });
  358. }
  359. if (this.isCallExpression()) {
  360. return this.node.arguments.length === 1 && this.get("callee").matchesPattern("Symbol.for") && !this.scope.hasBinding("Symbol", {
  361. noGlobals: true
  362. }) && this.get("arguments")[0].isStringLiteral();
  363. }
  364. return false;
  365. }
  366. function isInStrictMode() {
  367. const start = this.isProgram() ? this : this.parentPath;
  368. const strictParent = start.find(path => {
  369. if (path.isProgram({
  370. sourceType: "module"
  371. })) return true;
  372. if (path.isClass()) return true;
  373. if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
  374. return false;
  375. }
  376. let body;
  377. if (path.isFunction()) {
  378. body = path.node.body;
  379. } else if (path.isProgram()) {
  380. body = path.node;
  381. } else {
  382. return false;
  383. }
  384. for (const directive of body.directives) {
  385. if (directive.value.value === "use strict") {
  386. return true;
  387. }
  388. }
  389. return false;
  390. });
  391. return !!strictParent;
  392. }
  393. //# sourceMappingURL=introspection.js.map