introspection.js 11 KB

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