introspection.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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.is = void 0;
  24. function _includes() {
  25. var data = _interopRequireDefault(require("lodash/includes"));
  26. _includes = function _includes() {
  27. return data;
  28. };
  29. return data;
  30. }
  31. function t() {
  32. var data = _interopRequireWildcard(require("@babel/types"));
  33. t = function t() {
  34. return data;
  35. };
  36. return data;
  37. }
  38. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  39. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  40. function matchesPattern(pattern, allowPartial) {
  41. return t().matchesPattern(this.node, pattern, allowPartial);
  42. }
  43. function has(key) {
  44. var val = this.node && this.node[key];
  45. if (val && Array.isArray(val)) {
  46. return !!val.length;
  47. } else {
  48. return !!val;
  49. }
  50. }
  51. function isStatic() {
  52. return this.scope.isStatic(this.node);
  53. }
  54. var is = has;
  55. exports.is = is;
  56. function isnt(key) {
  57. return !this.has(key);
  58. }
  59. function equals(key, value) {
  60. return this.node[key] === value;
  61. }
  62. function isNodeType(type) {
  63. return t().isType(this.type, type);
  64. }
  65. function canHaveVariableDeclarationOrExpression() {
  66. return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
  67. }
  68. function canSwapBetweenExpressionAndStatement(replacement) {
  69. if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
  70. return false;
  71. }
  72. if (this.isExpression()) {
  73. return t().isBlockStatement(replacement);
  74. } else if (this.isBlockStatement()) {
  75. return t().isExpression(replacement);
  76. }
  77. return false;
  78. }
  79. function isCompletionRecord(allowInsideFunction) {
  80. var path = this;
  81. var first = true;
  82. do {
  83. var container = path.container;
  84. if (path.isFunction() && !first) {
  85. return !!allowInsideFunction;
  86. }
  87. first = false;
  88. if (Array.isArray(container) && path.key !== container.length - 1) {
  89. return false;
  90. }
  91. } while ((path = path.parentPath) && !path.isProgram());
  92. return true;
  93. }
  94. function isStatementOrBlock() {
  95. if (this.parentPath.isLabeledStatement() || t().isBlockStatement(this.container)) {
  96. return false;
  97. } else {
  98. return (0, _includes().default)(t().STATEMENT_OR_BLOCK_KEYS, this.key);
  99. }
  100. }
  101. function referencesImport(moduleSource, importName) {
  102. if (!this.isReferencedIdentifier()) return false;
  103. var binding = this.scope.getBinding(this.node.name);
  104. if (!binding || binding.kind !== "module") return false;
  105. var path = binding.path;
  106. var parent = path.parentPath;
  107. if (!parent.isImportDeclaration()) return false;
  108. if (parent.node.source.value === moduleSource) {
  109. if (!importName) return true;
  110. } else {
  111. return false;
  112. }
  113. if (path.isImportDefaultSpecifier() && importName === "default") {
  114. return true;
  115. }
  116. if (path.isImportNamespaceSpecifier() && importName === "*") {
  117. return true;
  118. }
  119. if (path.isImportSpecifier() && path.node.imported.name === importName) {
  120. return true;
  121. }
  122. return false;
  123. }
  124. function getSource() {
  125. var node = this.node;
  126. if (node.end) {
  127. return this.hub.file.code.slice(node.start, node.end);
  128. } else {
  129. return "";
  130. }
  131. }
  132. function willIMaybeExecuteBefore(target) {
  133. return this._guessExecutionStatusRelativeTo(target) !== "after";
  134. }
  135. function _guessExecutionStatusRelativeTo(target) {
  136. var targetFuncParent = target.scope.getFunctionParent() || target.scope.getProgramParent();
  137. var selfFuncParent = this.scope.getFunctionParent() || target.scope.getProgramParent();
  138. if (targetFuncParent.node !== selfFuncParent.node) {
  139. var status = this._guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent);
  140. if (status) {
  141. return status;
  142. } else {
  143. target = targetFuncParent.path;
  144. }
  145. }
  146. var targetPaths = target.getAncestry();
  147. if (targetPaths.indexOf(this) >= 0) return "after";
  148. var selfPaths = this.getAncestry();
  149. var commonPath;
  150. var targetIndex;
  151. var selfIndex;
  152. for (selfIndex = 0; selfIndex < selfPaths.length; selfIndex++) {
  153. var selfPath = selfPaths[selfIndex];
  154. targetIndex = targetPaths.indexOf(selfPath);
  155. if (targetIndex >= 0) {
  156. commonPath = selfPath;
  157. break;
  158. }
  159. }
  160. if (!commonPath) {
  161. return "before";
  162. }
  163. var targetRelationship = targetPaths[targetIndex - 1];
  164. var selfRelationship = selfPaths[selfIndex - 1];
  165. if (!targetRelationship || !selfRelationship) {
  166. return "before";
  167. }
  168. if (targetRelationship.listKey && targetRelationship.container === selfRelationship.container) {
  169. return targetRelationship.key > selfRelationship.key ? "before" : "after";
  170. }
  171. var keys = t().VISITOR_KEYS[commonPath.type];
  172. var targetKeyPosition = keys.indexOf(targetRelationship.key);
  173. var selfKeyPosition = keys.indexOf(selfRelationship.key);
  174. return targetKeyPosition > selfKeyPosition ? "before" : "after";
  175. }
  176. function _guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent) {
  177. var targetFuncPath = targetFuncParent.path;
  178. if (!targetFuncPath.isFunctionDeclaration()) return;
  179. var binding = targetFuncPath.scope.getBinding(targetFuncPath.node.id.name);
  180. if (!binding.references) return "before";
  181. var referencePaths = binding.referencePaths;
  182. for (var _iterator = referencePaths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  183. var _ref;
  184. if (_isArray) {
  185. if (_i >= _iterator.length) break;
  186. _ref = _iterator[_i++];
  187. } else {
  188. _i = _iterator.next();
  189. if (_i.done) break;
  190. _ref = _i.value;
  191. }
  192. var path = _ref;
  193. if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
  194. return;
  195. }
  196. }
  197. var allStatus;
  198. for (var _iterator2 = referencePaths, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  199. var _ref2;
  200. if (_isArray2) {
  201. if (_i2 >= _iterator2.length) break;
  202. _ref2 = _iterator2[_i2++];
  203. } else {
  204. _i2 = _iterator2.next();
  205. if (_i2.done) break;
  206. _ref2 = _i2.value;
  207. }
  208. var _path = _ref2;
  209. var childOfFunction = !!_path.find(function (path) {
  210. return path.node === targetFuncPath.node;
  211. });
  212. if (childOfFunction) continue;
  213. var status = this._guessExecutionStatusRelativeTo(_path);
  214. if (allStatus) {
  215. if (allStatus !== status) return;
  216. } else {
  217. allStatus = status;
  218. }
  219. }
  220. return allStatus;
  221. }
  222. function resolve(dangerous, resolved) {
  223. return this._resolve(dangerous, resolved) || this;
  224. }
  225. function _resolve(dangerous, resolved) {
  226. if (resolved && resolved.indexOf(this) >= 0) return;
  227. resolved = resolved || [];
  228. resolved.push(this);
  229. if (this.isVariableDeclarator()) {
  230. if (this.get("id").isIdentifier()) {
  231. return this.get("init").resolve(dangerous, resolved);
  232. } else {}
  233. } else if (this.isReferencedIdentifier()) {
  234. var binding = this.scope.getBinding(this.node.name);
  235. if (!binding) return;
  236. if (!binding.constant) return;
  237. if (binding.kind === "module") return;
  238. if (binding.path !== this) {
  239. var ret = binding.path.resolve(dangerous, resolved);
  240. if (this.find(function (parent) {
  241. return parent.node === ret.node;
  242. })) return;
  243. return ret;
  244. }
  245. } else if (this.isTypeCastExpression()) {
  246. return this.get("expression").resolve(dangerous, resolved);
  247. } else if (dangerous && this.isMemberExpression()) {
  248. var targetKey = this.toComputedKey();
  249. if (!t().isLiteral(targetKey)) return;
  250. var targetName = targetKey.value;
  251. var target = this.get("object").resolve(dangerous, resolved);
  252. if (target.isObjectExpression()) {
  253. var props = target.get("properties");
  254. var _arr = props;
  255. for (var _i3 = 0; _i3 < _arr.length; _i3++) {
  256. var prop = _arr[_i3];
  257. if (!prop.isProperty()) continue;
  258. var key = prop.get("key");
  259. var match = prop.isnt("computed") && key.isIdentifier({
  260. name: targetName
  261. });
  262. match = match || key.isLiteral({
  263. value: targetName
  264. });
  265. if (match) return prop.get("value").resolve(dangerous, resolved);
  266. }
  267. } else if (target.isArrayExpression() && !isNaN(+targetName)) {
  268. var elems = target.get("elements");
  269. var elem = elems[targetName];
  270. if (elem) return elem.resolve(dangerous, resolved);
  271. }
  272. }
  273. }
  274. function isConstantExpression() {
  275. if (this.isIdentifier()) {
  276. var binding = this.scope.getBinding(this.node.name);
  277. if (!binding) {
  278. return false;
  279. }
  280. return binding.constant && binding.path.get("init").isConstantExpression();
  281. }
  282. if (this.isLiteral()) {
  283. if (this.isRegExpLiteral()) {
  284. return false;
  285. }
  286. if (this.isTemplateLiteral()) {
  287. return this.get("expressions").every(function (expression) {
  288. return expression.isConstantExpression();
  289. });
  290. }
  291. return true;
  292. }
  293. if (this.isUnaryExpression()) {
  294. if (this.get("operator").node !== "void") {
  295. return false;
  296. }
  297. return this.get("argument").isConstantExpression();
  298. }
  299. if (this.isBinaryExpression()) {
  300. return this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
  301. }
  302. return false;
  303. }