introspection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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.equals = equals;
  10. exports.getSource = getSource;
  11. exports.has = has;
  12. exports.is = void 0;
  13. exports.isCompletionRecord = isCompletionRecord;
  14. exports.isConstantExpression = isConstantExpression;
  15. exports.isInStrictMode = isInStrictMode;
  16. exports.isNodeType = isNodeType;
  17. exports.isStatementOrBlock = isStatementOrBlock;
  18. exports.isStatic = isStatic;
  19. exports.isnt = isnt;
  20. exports.matchesPattern = matchesPattern;
  21. exports.referencesImport = referencesImport;
  22. exports.resolve = resolve;
  23. exports.willIMaybeExecuteBefore = willIMaybeExecuteBefore;
  24. var _t = require("@babel/types");
  25. const {
  26. STATEMENT_OR_BLOCK_KEYS,
  27. VISITOR_KEYS,
  28. isBlockStatement,
  29. isExpression,
  30. isIdentifier,
  31. isLiteral,
  32. isStringLiteral,
  33. isType,
  34. matchesPattern: _matchesPattern
  35. } = _t;
  36. function matchesPattern(pattern, allowPartial) {
  37. return _matchesPattern(this.node, pattern, allowPartial);
  38. }
  39. function has(key) {
  40. const val = this.node && this.node[key];
  41. if (val && Array.isArray(val)) {
  42. return !!val.length;
  43. } else {
  44. return !!val;
  45. }
  46. }
  47. function isStatic() {
  48. return this.scope.isStatic(this.node);
  49. }
  50. const is = has;
  51. exports.is = is;
  52. function isnt(key) {
  53. return !this.has(key);
  54. }
  55. function equals(key, value) {
  56. return this.node[key] === value;
  57. }
  58. function isNodeType(type) {
  59. return isType(this.type, type);
  60. }
  61. function canHaveVariableDeclarationOrExpression() {
  62. return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
  63. }
  64. function canSwapBetweenExpressionAndStatement(replacement) {
  65. if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
  66. return false;
  67. }
  68. if (this.isExpression()) {
  69. return isBlockStatement(replacement);
  70. } else if (this.isBlockStatement()) {
  71. return isExpression(replacement);
  72. }
  73. return false;
  74. }
  75. function isCompletionRecord(allowInsideFunction) {
  76. let path = this;
  77. let first = true;
  78. do {
  79. const {
  80. type,
  81. container
  82. } = path;
  83. if (!first && (path.isFunction() || type === "StaticBlock")) {
  84. return !!allowInsideFunction;
  85. }
  86. first = false;
  87. if (Array.isArray(container) && path.key !== container.length - 1) {
  88. return false;
  89. }
  90. } while ((path = path.parentPath) && !path.isProgram() && !path.isDoExpression());
  91. return true;
  92. }
  93. function isStatementOrBlock() {
  94. if (this.parentPath.isLabeledStatement() || isBlockStatement(this.container)) {
  95. return false;
  96. } else {
  97. return STATEMENT_OR_BLOCK_KEYS.includes(this.key);
  98. }
  99. }
  100. function referencesImport(moduleSource, importName) {
  101. if (!this.isReferencedIdentifier()) {
  102. if (this.isJSXMemberExpression() && this.node.property.name === importName || (this.isMemberExpression() || this.isOptionalMemberExpression()) && (this.node.computed ? isStringLiteral(this.node.property, {
  103. value: importName
  104. }) : this.node.property.name === importName)) {
  105. const object = this.get("object");
  106. return object.isReferencedIdentifier() && object.referencesImport(moduleSource, "*");
  107. }
  108. return false;
  109. }
  110. const binding = this.scope.getBinding(this.node.name);
  111. if (!binding || binding.kind !== "module") return false;
  112. const path = binding.path;
  113. const parent = path.parentPath;
  114. if (!parent.isImportDeclaration()) return false;
  115. if (parent.node.source.value === moduleSource) {
  116. if (!importName) return true;
  117. } else {
  118. return false;
  119. }
  120. if (path.isImportDefaultSpecifier() && importName === "default") {
  121. return true;
  122. }
  123. if (path.isImportNamespaceSpecifier() && importName === "*") {
  124. return true;
  125. }
  126. if (path.isImportSpecifier() && isIdentifier(path.node.imported, {
  127. name: importName
  128. })) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. function getSource() {
  134. const node = this.node;
  135. if (node.end) {
  136. const code = this.hub.getCode();
  137. if (code) return code.slice(node.start, node.end);
  138. }
  139. return "";
  140. }
  141. function willIMaybeExecuteBefore(target) {
  142. return this._guessExecutionStatusRelativeTo(target) !== "after";
  143. }
  144. function getOuterFunction(path) {
  145. return (path.scope.getFunctionParent() || path.scope.getProgramParent()).path;
  146. }
  147. function isExecutionUncertain(type, key) {
  148. switch (type) {
  149. case "LogicalExpression":
  150. return key === "right";
  151. case "ConditionalExpression":
  152. case "IfStatement":
  153. return key === "consequent" || key === "alternate";
  154. case "WhileStatement":
  155. case "DoWhileStatement":
  156. case "ForInStatement":
  157. case "ForOfStatement":
  158. return key === "body";
  159. case "ForStatement":
  160. return key === "body" || key === "update";
  161. case "SwitchStatement":
  162. return key === "cases";
  163. case "TryStatement":
  164. return key === "handler";
  165. case "AssignmentPattern":
  166. return key === "right";
  167. case "OptionalMemberExpression":
  168. return key === "property";
  169. case "OptionalCallExpression":
  170. return key === "arguments";
  171. default:
  172. return false;
  173. }
  174. }
  175. function isExecutionUncertainInList(paths, maxIndex) {
  176. for (let i = 0; i < maxIndex; i++) {
  177. const path = paths[i];
  178. if (isExecutionUncertain(path.parent.type, path.parentKey)) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. function _guessExecutionStatusRelativeTo(target) {
  185. return _guessExecutionStatusRelativeToCached(this, target, new Map());
  186. }
  187. function _guessExecutionStatusRelativeToCached(base, target, cache) {
  188. const funcParent = {
  189. this: getOuterFunction(base),
  190. target: getOuterFunction(target)
  191. };
  192. if (funcParent.target.node !== funcParent.this.node) {
  193. return _guessExecutionStatusRelativeToDifferentFunctionsCached(base, funcParent.target, cache);
  194. }
  195. const paths = {
  196. target: target.getAncestry(),
  197. this: base.getAncestry()
  198. };
  199. if (paths.target.indexOf(base) >= 0) return "after";
  200. if (paths.this.indexOf(target) >= 0) return "before";
  201. let commonPath;
  202. const commonIndex = {
  203. target: 0,
  204. this: 0
  205. };
  206. while (!commonPath && commonIndex.this < paths.this.length) {
  207. const path = paths.this[commonIndex.this];
  208. commonIndex.target = paths.target.indexOf(path);
  209. if (commonIndex.target >= 0) {
  210. commonPath = path;
  211. } else {
  212. commonIndex.this++;
  213. }
  214. }
  215. if (!commonPath) {
  216. throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program.");
  217. }
  218. if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) {
  219. return "unknown";
  220. }
  221. const divergence = {
  222. this: paths.this[commonIndex.this - 1],
  223. target: paths.target[commonIndex.target - 1]
  224. };
  225. if (divergence.target.listKey && divergence.this.listKey && divergence.target.container === divergence.this.container) {
  226. return divergence.target.key > divergence.this.key ? "before" : "after";
  227. }
  228. const keys = VISITOR_KEYS[commonPath.type];
  229. const keyPosition = {
  230. this: keys.indexOf(divergence.this.parentKey),
  231. target: keys.indexOf(divergence.target.parentKey)
  232. };
  233. return keyPosition.target > keyPosition.this ? "before" : "after";
  234. }
  235. const executionOrderCheckedNodes = new Set();
  236. function _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache) {
  237. if (!target.isFunctionDeclaration() || target.parentPath.isExportDeclaration()) {
  238. return "unknown";
  239. }
  240. const binding = target.scope.getBinding(target.node.id.name);
  241. if (!binding.references) return "before";
  242. const referencePaths = binding.referencePaths;
  243. let allStatus;
  244. for (const path of referencePaths) {
  245. const childOfFunction = !!path.find(path => path.node === target.node);
  246. if (childOfFunction) continue;
  247. if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
  248. return "unknown";
  249. }
  250. if (executionOrderCheckedNodes.has(path.node)) continue;
  251. executionOrderCheckedNodes.add(path.node);
  252. try {
  253. const status = _guessExecutionStatusRelativeToCached(base, path, cache);
  254. if (allStatus && allStatus !== status) {
  255. return "unknown";
  256. } else {
  257. allStatus = status;
  258. }
  259. } finally {
  260. executionOrderCheckedNodes.delete(path.node);
  261. }
  262. }
  263. return allStatus;
  264. }
  265. function _guessExecutionStatusRelativeToDifferentFunctionsCached(base, target, cache) {
  266. let nodeMap = cache.get(base.node);
  267. if (!nodeMap) {
  268. cache.set(base.node, nodeMap = new Map());
  269. } else if (nodeMap.has(target.node)) {
  270. return nodeMap.get(target.node);
  271. }
  272. const result = _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache);
  273. nodeMap.set(target.node, result);
  274. return result;
  275. }
  276. function resolve(dangerous, resolved) {
  277. return this._resolve(dangerous, resolved) || this;
  278. }
  279. function _resolve(dangerous, resolved) {
  280. if (resolved && resolved.indexOf(this) >= 0) return;
  281. resolved = resolved || [];
  282. resolved.push(this);
  283. if (this.isVariableDeclarator()) {
  284. if (this.get("id").isIdentifier()) {
  285. return this.get("init").resolve(dangerous, resolved);
  286. } else {
  287. }
  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. return false;
  353. }
  354. function isInStrictMode() {
  355. const start = this.isProgram() ? this : this.parentPath;
  356. const strictParent = start.find(path => {
  357. if (path.isProgram({
  358. sourceType: "module"
  359. })) return true;
  360. if (path.isClass()) return true;
  361. if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
  362. return false;
  363. }
  364. let body;
  365. if (path.isFunction()) {
  366. body = path.node.body;
  367. } else if (path.isProgram()) {
  368. body = path.node;
  369. } else {
  370. return false;
  371. }
  372. for (const directive of body.directives) {
  373. if (directive.value.value === "use strict") {
  374. return true;
  375. }
  376. }
  377. });
  378. return !!strictParent;
  379. }
  380. //# sourceMappingURL=introspection.js.map