inferer-reference.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. var _t = require("@babel/types");
  7. var _util = require("./util");
  8. const {
  9. BOOLEAN_NUMBER_BINARY_OPERATORS,
  10. createTypeAnnotationBasedOnTypeof,
  11. numberTypeAnnotation,
  12. voidTypeAnnotation
  13. } = _t;
  14. function _default(node) {
  15. if (!this.isReferenced()) return;
  16. const binding = this.scope.getBinding(node.name);
  17. if (binding) {
  18. if (binding.identifier.typeAnnotation) {
  19. return binding.identifier.typeAnnotation;
  20. } else {
  21. return getTypeAnnotationBindingConstantViolations(binding, this, node.name);
  22. }
  23. }
  24. if (node.name === "undefined") {
  25. return voidTypeAnnotation();
  26. } else if (node.name === "NaN" || node.name === "Infinity") {
  27. return numberTypeAnnotation();
  28. } else if (node.name === "arguments") {
  29. }
  30. }
  31. function getTypeAnnotationBindingConstantViolations(binding, path, name) {
  32. const types = [];
  33. const functionConstantViolations = [];
  34. let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
  35. const testType = getConditionalAnnotation(binding, path, name);
  36. if (testType) {
  37. const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
  38. constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0);
  39. types.push(testType.typeAnnotation);
  40. }
  41. if (constantViolations.length) {
  42. constantViolations.push(...functionConstantViolations);
  43. for (const violation of constantViolations) {
  44. types.push(violation.getTypeAnnotation());
  45. }
  46. }
  47. if (!types.length) {
  48. return;
  49. }
  50. return (0, _util.createUnionType)(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 (BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
  81. return 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 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. return {
  144. typeAnnotation: (0, _util.createUnionType)(types),
  145. ifStatement
  146. };
  147. }
  148. return getConditionalAnnotation(binding, ifStatement, name);
  149. }
  150. //# sourceMappingURL=inferer-reference.js.map