introspection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 = exports.is = has;
  51. function isnt(key) {
  52. return !this.has(key);
  53. }
  54. function equals(key, value) {
  55. return this.node[key] === value;
  56. }
  57. function isNodeType(type) {
  58. return isType(this.type, type);
  59. }
  60. function canHaveVariableDeclarationOrExpression() {
  61. return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
  62. }
  63. function canSwapBetweenExpressionAndStatement(replacement) {
  64. if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
  65. return false;
  66. }
  67. if (this.isExpression()) {
  68. return isBlockStatement(replacement);
  69. } else if (this.isBlockStatement()) {
  70. return isExpression(replacement);
  71. }
  72. return false;
  73. }
  74. function isCompletionRecord(allowInsideFunction) {
  75. let path = this;
  76. let first = true;
  77. do {
  78. const {
  79. type,
  80. container
  81. } = path;
  82. if (!first && (path.isFunction() || type === "StaticBlock")) {
  83. return !!allowInsideFunction;
  84. }
  85. first = false;
  86. if (Array.isArray(container) && path.key !== container.length - 1) {
  87. return false;
  88. }
  89. } while ((path = path.parentPath) && !path.isProgram() && !path.isDoExpression());
  90. return true;
  91. }
  92. function isStatementOrBlock() {
  93. if (this.parentPath.isLabeledStatement() || isBlockStatement(this.container)) {
  94. return false;
  95. } else {
  96. return STATEMENT_OR_BLOCK_KEYS.includes(this.key);
  97. }
  98. }
  99. function referencesImport(moduleSource, importName) {
  100. if (!this.isReferencedIdentifier()) {
  101. if (this.isJSXMemberExpression() && this.node.property.name === importName || (this.isMemberExpression() || this.isOptionalMemberExpression()) && (this.node.computed ? isStringLiteral(this.node.property, {
  102. value: importName
  103. }) : this.node.property.name === importName)) {
  104. const object = this.get("object");
  105. return object.isReferencedIdentifier() && object.referencesImport(moduleSource, "*");
  106. }
  107. return false;
  108. }
  109. const binding = this.scope.getBinding(this.node.name);
  110. if (!binding || binding.kind !== "module") return false;
  111. const path = binding.path;
  112. const parent = path.parentPath;
  113. if (!parent.isImportDeclaration()) return false;
  114. if (parent.node.source.value === moduleSource) {
  115. if (!importName) return true;
  116. } else {
  117. return false;
  118. }
  119. if (path.isImportDefaultSpecifier() && importName === "default") {
  120. return true;
  121. }
  122. if (path.isImportNamespaceSpecifier() && importName === "*") {
  123. return true;
  124. }
  125. if (path.isImportSpecifier() && isIdentifier(path.node.imported, {
  126. name: importName
  127. })) {
  128. return true;
  129. }
  130. return false;
  131. }
  132. function getSource() {
  133. const node = this.node;
  134. if (node.end) {
  135. const code = this.hub.getCode();
  136. if (code) return code.slice(node.start, node.end);
  137. }
  138. return "";
  139. }
  140. function willIMaybeExecuteBefore(target) {
  141. return this._guessExecutionStatusRelativeTo(target) !== "after";
  142. }
  143. function getOuterFunction(path) {
  144. return path.isProgram() ? path : (path.parentPath.scope.getFunctionParent() || path.parentPath.scope.getProgramParent()).path;
  145. }
  146. function isExecutionUncertain(type, key) {
  147. switch (type) {
  148. case "LogicalExpression":
  149. return key === "right";
  150. case "ConditionalExpression":
  151. case "IfStatement":
  152. return key === "consequent" || key === "alternate";
  153. case "WhileStatement":
  154. case "DoWhileStatement":
  155. case "ForInStatement":
  156. case "ForOfStatement":
  157. return key === "body";
  158. case "ForStatement":
  159. return key === "body" || key === "update";
  160. case "SwitchStatement":
  161. return key === "cases";
  162. case "TryStatement":
  163. return key === "handler";
  164. case "AssignmentPattern":
  165. return key === "right";
  166. case "OptionalMemberExpression":
  167. return key === "property";
  168. case "OptionalCallExpression":
  169. return key === "arguments";
  170. default:
  171. return false;
  172. }
  173. }
  174. function isExecutionUncertainInList(paths, maxIndex) {
  175. for (let i = 0; i < maxIndex; i++) {
  176. const path = paths[i];
  177. if (isExecutionUncertain(path.parent.type, path.parentKey)) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. const SYMBOL_CHECKING = Symbol();
  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. function _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache) {
  236. if (!target.isFunctionDeclaration()) {
  237. if (_guessExecutionStatusRelativeToCached(base, target, cache) === "before") {
  238. return "before";
  239. }
  240. return "unknown";
  241. } else if (target.parentPath.isExportDeclaration()) {
  242. return "unknown";
  243. }
  244. const binding = target.scope.getBinding(target.node.id.name);
  245. if (!binding.references) return "before";
  246. const referencePaths = binding.referencePaths;
  247. let allStatus;
  248. for (const path of referencePaths) {
  249. const childOfFunction = !!path.find(path => path.node === target.node);
  250. if (childOfFunction) continue;
  251. if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
  252. return "unknown";
  253. }
  254. const status = _guessExecutionStatusRelativeToCached(base, path, cache);
  255. if (allStatus && allStatus !== status) {
  256. return "unknown";
  257. } else {
  258. allStatus = status;
  259. }
  260. }
  261. return allStatus;
  262. }
  263. function _guessExecutionStatusRelativeToDifferentFunctionsCached(base, target, cache) {
  264. let nodeMap = cache.get(base.node);
  265. let cached;
  266. if (!nodeMap) {
  267. cache.set(base.node, nodeMap = new Map());
  268. } else if (cached = nodeMap.get(target.node)) {
  269. if (cached === SYMBOL_CHECKING) {
  270. return "unknown";
  271. }
  272. return cached;
  273. }
  274. nodeMap.set(target.node, SYMBOL_CHECKING);
  275. const result = _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache);
  276. nodeMap.set(target.node, result);
  277. return result;
  278. }
  279. function resolve(dangerous, resolved) {
  280. return this._resolve(dangerous, resolved) || this;
  281. }
  282. function _resolve(dangerous, resolved) {
  283. if (resolved && resolved.indexOf(this) >= 0) return;
  284. resolved = resolved || [];
  285. resolved.push(this);
  286. if (this.isVariableDeclarator()) {
  287. if (this.get("id").isIdentifier()) {
  288. return this.get("init").resolve(dangerous, resolved);
  289. } else {}
  290. } else if (this.isReferencedIdentifier()) {
  291. const binding = this.scope.getBinding(this.node.name);
  292. if (!binding) return;
  293. if (!binding.constant) return;
  294. if (binding.kind === "module") return;
  295. if (binding.path !== this) {
  296. const ret = binding.path.resolve(dangerous, resolved);
  297. if (this.find(parent => parent.node === ret.node)) return;
  298. return ret;
  299. }
  300. } else if (this.isTypeCastExpression()) {
  301. return this.get("expression").resolve(dangerous, resolved);
  302. } else if (dangerous && this.isMemberExpression()) {
  303. const targetKey = this.toComputedKey();
  304. if (!isLiteral(targetKey)) return;
  305. const targetName = targetKey.value;
  306. const target = this.get("object").resolve(dangerous, resolved);
  307. if (target.isObjectExpression()) {
  308. const props = target.get("properties");
  309. for (const prop of props) {
  310. if (!prop.isProperty()) continue;
  311. const key = prop.get("key");
  312. let match = prop.isnt("computed") && key.isIdentifier({
  313. name: targetName
  314. });
  315. match = match || key.isLiteral({
  316. value: targetName
  317. });
  318. if (match) return prop.get("value").resolve(dangerous, resolved);
  319. }
  320. } else if (target.isArrayExpression() && !isNaN(+targetName)) {
  321. const elems = target.get("elements");
  322. const elem = elems[targetName];
  323. if (elem) return elem.resolve(dangerous, resolved);
  324. }
  325. }
  326. }
  327. function isConstantExpression() {
  328. if (this.isIdentifier()) {
  329. const binding = this.scope.getBinding(this.node.name);
  330. if (!binding) return false;
  331. return binding.constant;
  332. }
  333. if (this.isLiteral()) {
  334. if (this.isRegExpLiteral()) {
  335. return false;
  336. }
  337. if (this.isTemplateLiteral()) {
  338. return this.get("expressions").every(expression => expression.isConstantExpression());
  339. }
  340. return true;
  341. }
  342. if (this.isUnaryExpression()) {
  343. if (this.node.operator !== "void") {
  344. return false;
  345. }
  346. return this.get("argument").isConstantExpression();
  347. }
  348. if (this.isBinaryExpression()) {
  349. const {
  350. operator
  351. } = this.node;
  352. return operator !== "in" && operator !== "instanceof" && this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
  353. }
  354. return false;
  355. }
  356. function isInStrictMode() {
  357. const start = this.isProgram() ? this : this.parentPath;
  358. const strictParent = start.find(path => {
  359. if (path.isProgram({
  360. sourceType: "module"
  361. })) return true;
  362. if (path.isClass()) return true;
  363. if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
  364. return false;
  365. }
  366. let body;
  367. if (path.isFunction()) {
  368. body = path.node.body;
  369. } else if (path.isProgram()) {
  370. body = path.node;
  371. } else {
  372. return false;
  373. }
  374. for (const directive of body.directives) {
  375. if (directive.value.value === "use strict") {
  376. return true;
  377. }
  378. }
  379. });
  380. return !!strictParent;
  381. }
  382. //# sourceMappingURL=introspection.js.map