introspection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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.isProgram() ? path : (path.parentPath.scope.getFunctionParent() || path.parentPath.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. const SYMBOL_CHECKING = Symbol();
  185. function _guessExecutionStatusRelativeTo(target) {
  186. return _guessExecutionStatusRelativeToCached(this, target, new Map());
  187. }
  188. function _guessExecutionStatusRelativeToCached(base, target, cache) {
  189. const funcParent = {
  190. this: getOuterFunction(base),
  191. target: getOuterFunction(target)
  192. };
  193. if (funcParent.target.node !== funcParent.this.node) {
  194. return _guessExecutionStatusRelativeToDifferentFunctionsCached(base, funcParent.target, cache);
  195. }
  196. const paths = {
  197. target: target.getAncestry(),
  198. this: base.getAncestry()
  199. };
  200. if (paths.target.indexOf(base) >= 0) return "after";
  201. if (paths.this.indexOf(target) >= 0) return "before";
  202. let commonPath;
  203. const commonIndex = {
  204. target: 0,
  205. this: 0
  206. };
  207. while (!commonPath && commonIndex.this < paths.this.length) {
  208. const path = paths.this[commonIndex.this];
  209. commonIndex.target = paths.target.indexOf(path);
  210. if (commonIndex.target >= 0) {
  211. commonPath = path;
  212. } else {
  213. commonIndex.this++;
  214. }
  215. }
  216. if (!commonPath) {
  217. throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program.");
  218. }
  219. if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) {
  220. return "unknown";
  221. }
  222. const divergence = {
  223. this: paths.this[commonIndex.this - 1],
  224. target: paths.target[commonIndex.target - 1]
  225. };
  226. if (divergence.target.listKey && divergence.this.listKey && divergence.target.container === divergence.this.container) {
  227. return divergence.target.key > divergence.this.key ? "before" : "after";
  228. }
  229. const keys = VISITOR_KEYS[commonPath.type];
  230. const keyPosition = {
  231. this: keys.indexOf(divergence.this.parentKey),
  232. target: keys.indexOf(divergence.target.parentKey)
  233. };
  234. return keyPosition.target > keyPosition.this ? "before" : "after";
  235. }
  236. function _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache) {
  237. if (!target.isFunctionDeclaration()) {
  238. if (_guessExecutionStatusRelativeToCached(base, target, cache) === "before") {
  239. return "before";
  240. }
  241. return "unknown";
  242. } else if (target.parentPath.isExportDeclaration()) {
  243. return "unknown";
  244. }
  245. const binding = target.scope.getBinding(target.node.id.name);
  246. if (!binding.references) return "before";
  247. const referencePaths = binding.referencePaths;
  248. let allStatus;
  249. for (const path of referencePaths) {
  250. const childOfFunction = !!path.find(path => path.node === target.node);
  251. if (childOfFunction) continue;
  252. if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
  253. return "unknown";
  254. }
  255. const status = _guessExecutionStatusRelativeToCached(base, path, cache);
  256. if (allStatus && allStatus !== status) {
  257. return "unknown";
  258. } else {
  259. allStatus = status;
  260. }
  261. }
  262. return allStatus;
  263. }
  264. function _guessExecutionStatusRelativeToDifferentFunctionsCached(base, target, cache) {
  265. let nodeMap = cache.get(base.node);
  266. let cached;
  267. if (!nodeMap) {
  268. cache.set(base.node, nodeMap = new Map());
  269. } else if (cached = nodeMap.get(target.node)) {
  270. if (cached === SYMBOL_CHECKING) {
  271. return "unknown";
  272. }
  273. return cached;
  274. }
  275. nodeMap.set(target.node, SYMBOL_CHECKING);
  276. const result = _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache);
  277. nodeMap.set(target.node, result);
  278. return result;
  279. }
  280. function resolve(dangerous, resolved) {
  281. return this._resolve(dangerous, resolved) || this;
  282. }
  283. function _resolve(dangerous, resolved) {
  284. if (resolved && resolved.indexOf(this) >= 0) return;
  285. resolved = resolved || [];
  286. resolved.push(this);
  287. if (this.isVariableDeclarator()) {
  288. if (this.get("id").isIdentifier()) {
  289. return this.get("init").resolve(dangerous, resolved);
  290. } else {
  291. }
  292. } else if (this.isReferencedIdentifier()) {
  293. const binding = this.scope.getBinding(this.node.name);
  294. if (!binding) return;
  295. if (!binding.constant) return;
  296. if (binding.kind === "module") return;
  297. if (binding.path !== this) {
  298. const ret = binding.path.resolve(dangerous, resolved);
  299. if (this.find(parent => parent.node === ret.node)) return;
  300. return ret;
  301. }
  302. } else if (this.isTypeCastExpression()) {
  303. return this.get("expression").resolve(dangerous, resolved);
  304. } else if (dangerous && this.isMemberExpression()) {
  305. const targetKey = this.toComputedKey();
  306. if (!isLiteral(targetKey)) return;
  307. const targetName = targetKey.value;
  308. const target = this.get("object").resolve(dangerous, resolved);
  309. if (target.isObjectExpression()) {
  310. const props = target.get("properties");
  311. for (const prop of props) {
  312. if (!prop.isProperty()) continue;
  313. const key = prop.get("key");
  314. let match = prop.isnt("computed") && key.isIdentifier({
  315. name: targetName
  316. });
  317. match = match || key.isLiteral({
  318. value: targetName
  319. });
  320. if (match) return prop.get("value").resolve(dangerous, resolved);
  321. }
  322. } else if (target.isArrayExpression() && !isNaN(+targetName)) {
  323. const elems = target.get("elements");
  324. const elem = elems[targetName];
  325. if (elem) return elem.resolve(dangerous, resolved);
  326. }
  327. }
  328. }
  329. function isConstantExpression() {
  330. if (this.isIdentifier()) {
  331. const binding = this.scope.getBinding(this.node.name);
  332. if (!binding) return false;
  333. return binding.constant;
  334. }
  335. if (this.isLiteral()) {
  336. if (this.isRegExpLiteral()) {
  337. return false;
  338. }
  339. if (this.isTemplateLiteral()) {
  340. return this.get("expressions").every(expression => expression.isConstantExpression());
  341. }
  342. return true;
  343. }
  344. if (this.isUnaryExpression()) {
  345. if (this.node.operator !== "void") {
  346. return false;
  347. }
  348. return this.get("argument").isConstantExpression();
  349. }
  350. if (this.isBinaryExpression()) {
  351. const {
  352. operator
  353. } = this.node;
  354. return operator !== "in" && operator !== "instanceof" && this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
  355. }
  356. return false;
  357. }
  358. function isInStrictMode() {
  359. const start = this.isProgram() ? this : this.parentPath;
  360. const strictParent = start.find(path => {
  361. if (path.isProgram({
  362. sourceType: "module"
  363. })) return true;
  364. if (path.isClass()) return true;
  365. if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
  366. return false;
  367. }
  368. let body;
  369. if (path.isFunction()) {
  370. body = path.node.body;
  371. } else if (path.isProgram()) {
  372. body = path.node;
  373. } else {
  374. return false;
  375. }
  376. for (const directive of body.directives) {
  377. if (directive.value.value === "use strict") {
  378. return true;
  379. }
  380. }
  381. });
  382. return !!strictParent;
  383. }
  384. //# sourceMappingURL=introspection.js.map