inferer-reference.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. var t = _interopRequireWildcard(require("@babel/types"));
  7. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  8. 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; }
  9. function _default(node) {
  10. if (!this.isReferenced()) return;
  11. const binding = this.scope.getBinding(node.name);
  12. if (binding) {
  13. if (binding.identifier.typeAnnotation) {
  14. return binding.identifier.typeAnnotation;
  15. } else {
  16. return getTypeAnnotationBindingConstantViolations(binding, this, node.name);
  17. }
  18. }
  19. if (node.name === "undefined") {
  20. return t.voidTypeAnnotation();
  21. } else if (node.name === "NaN" || node.name === "Infinity") {
  22. return t.numberTypeAnnotation();
  23. } else if (node.name === "arguments") {}
  24. }
  25. function getTypeAnnotationBindingConstantViolations(binding, path, name) {
  26. const types = [];
  27. const functionConstantViolations = [];
  28. let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
  29. const testType = getConditionalAnnotation(binding, path, name);
  30. if (testType) {
  31. const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
  32. constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0);
  33. types.push(testType.typeAnnotation);
  34. }
  35. if (constantViolations.length) {
  36. constantViolations = constantViolations.concat(functionConstantViolations);
  37. for (const violation of constantViolations) {
  38. types.push(violation.getTypeAnnotation());
  39. }
  40. }
  41. if (!types.length) {
  42. return;
  43. }
  44. if (t.isTSTypeAnnotation(types[0]) && t.createTSUnionType) {
  45. return t.createTSUnionType(types);
  46. }
  47. if (t.createFlowUnionType) {
  48. return t.createFlowUnionType(types);
  49. }
  50. return t.createUnionTypeAnnotation(types);
  51. }
  52. function getConstantViolationsBefore(binding, path, functions) {
  53. const violations = binding.constantViolations.slice();
  54. violations.unshift(binding.path);
  55. return violations.filter(violation => {
  56. violation = violation.resolve();
  57. const status = violation._guessExecutionStatusRelativeTo(path);
  58. if (functions && status === "unknown") functions.push(violation);
  59. return status === "before";
  60. });
  61. }
  62. function inferAnnotationFromBinaryExpression(name, path) {
  63. const operator = path.node.operator;
  64. const right = path.get("right").resolve();
  65. const left = path.get("left").resolve();
  66. let target;
  67. if (left.isIdentifier({
  68. name
  69. })) {
  70. target = right;
  71. } else if (right.isIdentifier({
  72. name
  73. })) {
  74. target = left;
  75. }
  76. if (target) {
  77. if (operator === "===") {
  78. return target.getTypeAnnotation();
  79. }
  80. if (t.BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
  81. return t.numberTypeAnnotation();
  82. }
  83. return;
  84. }
  85. if (operator !== "===" && operator !== "==") return;
  86. let typeofPath;
  87. let typePath;
  88. if (left.isUnaryExpression({
  89. operator: "typeof"
  90. })) {
  91. typeofPath = left;
  92. typePath = right;
  93. } else if (right.isUnaryExpression({
  94. operator: "typeof"
  95. })) {
  96. typeofPath = right;
  97. typePath = left;
  98. }
  99. if (!typeofPath) return;
  100. if (!typeofPath.get("argument").isIdentifier({
  101. name
  102. })) return;
  103. typePath = typePath.resolve();
  104. if (!typePath.isLiteral()) return;
  105. const typeValue = typePath.node.value;
  106. if (typeof typeValue !== "string") return;
  107. return t.createTypeAnnotationBasedOnTypeof(typeValue);
  108. }
  109. function getParentConditionalPath(binding, path, name) {
  110. let parentPath;
  111. while (parentPath = path.parentPath) {
  112. if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
  113. if (path.key === "test") {
  114. return;
  115. }
  116. return parentPath;
  117. }
  118. if (parentPath.isFunction()) {
  119. if (parentPath.parentPath.scope.getBinding(name) !== binding) return;
  120. }
  121. path = parentPath;
  122. }
  123. }
  124. function getConditionalAnnotation(binding, path, name) {
  125. const ifStatement = getParentConditionalPath(binding, path, name);
  126. if (!ifStatement) return;
  127. const test = ifStatement.get("test");
  128. const paths = [test];
  129. const types = [];
  130. for (let i = 0; i < paths.length; i++) {
  131. const path = paths[i];
  132. if (path.isLogicalExpression()) {
  133. if (path.node.operator === "&&") {
  134. paths.push(path.get("left"));
  135. paths.push(path.get("right"));
  136. }
  137. } else if (path.isBinaryExpression()) {
  138. const type = inferAnnotationFromBinaryExpression(name, path);
  139. if (type) types.push(type);
  140. }
  141. }
  142. if (types.length) {
  143. if (t.isTSTypeAnnotation(types[0]) && t.createTSUnionType) {
  144. return {
  145. typeAnnotation: t.createTSUnionType(types),
  146. ifStatement
  147. };
  148. }
  149. if (t.createFlowUnionType) {
  150. return {
  151. typeAnnotation: t.createFlowUnionType(types),
  152. ifStatement
  153. };
  154. }
  155. return {
  156. typeAnnotation: t.createUnionTypeAnnotation(types),
  157. ifStatement
  158. };
  159. }
  160. return getConditionalAnnotation(ifStatement, name);
  161. }