introspection.js 12 KB

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