introspection.js 10 KB

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