introspection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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()) return false;
  91. const binding = this.scope.getBinding(this.node.name);
  92. if (!binding || binding.kind !== "module") return false;
  93. const path = binding.path;
  94. const parent = path.parentPath;
  95. if (!parent.isImportDeclaration()) return false;
  96. if (parent.node.source.value === moduleSource) {
  97. if (!importName) return true;
  98. } else {
  99. return false;
  100. }
  101. if (path.isImportDefaultSpecifier() && importName === "default") {
  102. return true;
  103. }
  104. if (path.isImportNamespaceSpecifier() && importName === "*") {
  105. return true;
  106. }
  107. if (path.isImportSpecifier() && path.node.imported.name === importName) {
  108. return true;
  109. }
  110. return false;
  111. }
  112. function getSource() {
  113. const node = this.node;
  114. if (node.end) {
  115. const code = this.hub.getCode();
  116. if (code) return code.slice(node.start, node.end);
  117. }
  118. return "";
  119. }
  120. function willIMaybeExecuteBefore(target) {
  121. return this._guessExecutionStatusRelativeTo(target) !== "after";
  122. }
  123. function getOuterFunction(path) {
  124. return (path.scope.getFunctionParent() || path.scope.getProgramParent()).path;
  125. }
  126. function isExecutionUncertain(type, key) {
  127. switch (type) {
  128. case "LogicalExpression":
  129. return key === "right";
  130. case "ConditionalExpression":
  131. case "IfStatement":
  132. return key === "consequent" || key === "alternate";
  133. case "WhileStatement":
  134. case "DoWhileStatement":
  135. case "ForInStatement":
  136. case "ForOfStatement":
  137. return key === "body";
  138. case "ForStatement":
  139. return key === "body" || key === "update";
  140. case "SwitchStatement":
  141. return key === "cases";
  142. case "TryStatement":
  143. return key === "handler";
  144. case "AssignmentPattern":
  145. return key === "right";
  146. case "OptionalMemberExpression":
  147. return key === "property";
  148. case "OptionalCallExpression":
  149. return key === "arguments";
  150. default:
  151. return false;
  152. }
  153. }
  154. function isExecutionUncertainInList(paths, maxIndex) {
  155. for (let i = 0; i < maxIndex; i++) {
  156. const path = paths[i];
  157. if (isExecutionUncertain(path.parent.type, path.parentKey)) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. function _guessExecutionStatusRelativeTo(target) {
  164. const funcParent = {
  165. this: getOuterFunction(this),
  166. target: getOuterFunction(target)
  167. };
  168. if (funcParent.target.node !== funcParent.this.node) {
  169. return this._guessExecutionStatusRelativeToDifferentFunctions(funcParent.target);
  170. }
  171. const paths = {
  172. target: target.getAncestry(),
  173. this: this.getAncestry()
  174. };
  175. if (paths.target.indexOf(this) >= 0) return "after";
  176. if (paths.this.indexOf(target) >= 0) return "before";
  177. let commonPath;
  178. const commonIndex = {
  179. target: 0,
  180. this: 0
  181. };
  182. while (!commonPath && commonIndex.this < paths.this.length) {
  183. const path = paths.this[commonIndex.this];
  184. commonIndex.target = paths.target.indexOf(path);
  185. if (commonIndex.target >= 0) {
  186. commonPath = path;
  187. } else {
  188. commonIndex.this++;
  189. }
  190. }
  191. if (!commonPath) {
  192. throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program.");
  193. }
  194. if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) {
  195. return "unknown";
  196. }
  197. const divergence = {
  198. this: paths.this[commonIndex.this - 1],
  199. target: paths.target[commonIndex.target - 1]
  200. };
  201. if (divergence.target.listKey && divergence.this.listKey && divergence.target.container === divergence.this.container) {
  202. return divergence.target.key > divergence.this.key ? "before" : "after";
  203. }
  204. const keys = t.VISITOR_KEYS[commonPath.type];
  205. const keyPosition = {
  206. this: keys.indexOf(divergence.this.parentKey),
  207. target: keys.indexOf(divergence.target.parentKey)
  208. };
  209. return keyPosition.target > keyPosition.this ? "before" : "after";
  210. }
  211. const executionOrderCheckedNodes = new WeakSet();
  212. function _guessExecutionStatusRelativeToDifferentFunctions(target) {
  213. if (!target.isFunctionDeclaration() || target.parentPath.isExportDeclaration()) {
  214. return "unknown";
  215. }
  216. const binding = target.scope.getBinding(target.node.id.name);
  217. if (!binding.references) return "before";
  218. const referencePaths = binding.referencePaths;
  219. let allStatus;
  220. for (const path of referencePaths) {
  221. const childOfFunction = !!path.find(path => path.node === target.node);
  222. if (childOfFunction) continue;
  223. if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
  224. return "unknown";
  225. }
  226. if (executionOrderCheckedNodes.has(path.node)) continue;
  227. executionOrderCheckedNodes.add(path.node);
  228. const status = this._guessExecutionStatusRelativeTo(path);
  229. executionOrderCheckedNodes.delete(path.node);
  230. if (allStatus && allStatus !== status) {
  231. return "unknown";
  232. } else {
  233. allStatus = status;
  234. }
  235. }
  236. return allStatus;
  237. }
  238. function resolve(dangerous, resolved) {
  239. return this._resolve(dangerous, resolved) || this;
  240. }
  241. function _resolve(dangerous, resolved) {
  242. if (resolved && resolved.indexOf(this) >= 0) return;
  243. resolved = resolved || [];
  244. resolved.push(this);
  245. if (this.isVariableDeclarator()) {
  246. if (this.get("id").isIdentifier()) {
  247. return this.get("init").resolve(dangerous, resolved);
  248. } else {}
  249. } else if (this.isReferencedIdentifier()) {
  250. const binding = this.scope.getBinding(this.node.name);
  251. if (!binding) return;
  252. if (!binding.constant) return;
  253. if (binding.kind === "module") return;
  254. if (binding.path !== this) {
  255. const ret = binding.path.resolve(dangerous, resolved);
  256. if (this.find(parent => parent.node === ret.node)) return;
  257. return ret;
  258. }
  259. } else if (this.isTypeCastExpression()) {
  260. return this.get("expression").resolve(dangerous, resolved);
  261. } else if (dangerous && this.isMemberExpression()) {
  262. const targetKey = this.toComputedKey();
  263. if (!t.isLiteral(targetKey)) return;
  264. const targetName = targetKey.value;
  265. const target = this.get("object").resolve(dangerous, resolved);
  266. if (target.isObjectExpression()) {
  267. const props = target.get("properties");
  268. for (const prop of props) {
  269. if (!prop.isProperty()) continue;
  270. const key = prop.get("key");
  271. let match = prop.isnt("computed") && key.isIdentifier({
  272. name: targetName
  273. });
  274. match = match || key.isLiteral({
  275. value: targetName
  276. });
  277. if (match) return prop.get("value").resolve(dangerous, resolved);
  278. }
  279. } else if (target.isArrayExpression() && !isNaN(+targetName)) {
  280. const elems = target.get("elements");
  281. const elem = elems[targetName];
  282. if (elem) return elem.resolve(dangerous, resolved);
  283. }
  284. }
  285. }
  286. function isConstantExpression() {
  287. if (this.isIdentifier()) {
  288. const binding = this.scope.getBinding(this.node.name);
  289. if (!binding) return false;
  290. return binding.constant;
  291. }
  292. if (this.isLiteral()) {
  293. if (this.isRegExpLiteral()) {
  294. return false;
  295. }
  296. if (this.isTemplateLiteral()) {
  297. return this.get("expressions").every(expression => expression.isConstantExpression());
  298. }
  299. return true;
  300. }
  301. if (this.isUnaryExpression()) {
  302. if (this.get("operator").node !== "void") {
  303. return false;
  304. }
  305. return this.get("argument").isConstantExpression();
  306. }
  307. if (this.isBinaryExpression()) {
  308. return this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
  309. }
  310. return false;
  311. }
  312. function isInStrictMode() {
  313. const start = this.isProgram() ? this : this.parentPath;
  314. const strictParent = start.find(path => {
  315. if (path.isProgram({
  316. sourceType: "module"
  317. })) return true;
  318. if (path.isClass()) return true;
  319. if (!path.isProgram() && !path.isFunction()) return false;
  320. if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
  321. return false;
  322. }
  323. let {
  324. node
  325. } = path;
  326. if (path.isFunction()) node = node.body;
  327. for (const directive of node.directives) {
  328. if (directive.value.value === "use strict") {
  329. return true;
  330. }
  331. }
  332. });
  333. return !!strictParent;
  334. }