inferer-reference.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.js");
  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. function getTypeAnnotationBindingConstantViolations(binding, path, name) {
  31. const types = [];
  32. const functionConstantViolations = [];
  33. let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
  34. const testType = getConditionalAnnotation(binding, path, name);
  35. if (testType) {
  36. const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
  37. constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0);
  38. types.push(testType.typeAnnotation);
  39. }
  40. if (constantViolations.length) {
  41. constantViolations.push(...functionConstantViolations);
  42. for (const violation of constantViolations) {
  43. types.push(violation.getTypeAnnotation());
  44. }
  45. }
  46. if (!types.length) {
  47. return;
  48. }
  49. return (0, _util.createUnionType)(types);
  50. }
  51. function getConstantViolationsBefore(binding, path, functions) {
  52. const violations = binding.constantViolations.slice();
  53. violations.unshift(binding.path);
  54. return violations.filter(violation => {
  55. violation = violation.resolve();
  56. const status = violation._guessExecutionStatusRelativeTo(path);
  57. if (functions && status === "unknown") functions.push(violation);
  58. return status === "before";
  59. });
  60. }
  61. function inferAnnotationFromBinaryExpression(name, path) {
  62. const operator = path.node.operator;
  63. const right = path.get("right").resolve();
  64. const left = path.get("left").resolve();
  65. let target;
  66. if (left.isIdentifier({
  67. name
  68. })) {
  69. target = right;
  70. } else if (right.isIdentifier({
  71. name
  72. })) {
  73. target = left;
  74. }
  75. if (target) {
  76. if (operator === "===") {
  77. return target.getTypeAnnotation();
  78. }
  79. if (BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
  80. return numberTypeAnnotation();
  81. }
  82. return;
  83. }
  84. if (operator !== "===" && operator !== "==") return;
  85. let typeofPath;
  86. let typePath;
  87. if (left.isUnaryExpression({
  88. operator: "typeof"
  89. })) {
  90. typeofPath = left;
  91. typePath = right;
  92. } else if (right.isUnaryExpression({
  93. operator: "typeof"
  94. })) {
  95. typeofPath = right;
  96. typePath = left;
  97. }
  98. if (!typeofPath) return;
  99. if (!typeofPath.get("argument").isIdentifier({
  100. name
  101. })) return;
  102. typePath = typePath.resolve();
  103. if (!typePath.isLiteral()) return;
  104. const typeValue = typePath.node.value;
  105. if (typeof typeValue !== "string") return;
  106. return createTypeAnnotationBasedOnTypeof(typeValue);
  107. }
  108. function getParentConditionalPath(binding, path, name) {
  109. let parentPath;
  110. while (parentPath = path.parentPath) {
  111. if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
  112. if (path.key === "test") {
  113. return;
  114. }
  115. return parentPath;
  116. }
  117. if (parentPath.isFunction()) {
  118. if (parentPath.parentPath.scope.getBinding(name) !== binding) return;
  119. }
  120. path = parentPath;
  121. }
  122. }
  123. function getConditionalAnnotation(binding, path, name) {
  124. const ifStatement = getParentConditionalPath(binding, path, name);
  125. if (!ifStatement) return;
  126. const test = ifStatement.get("test");
  127. const paths = [test];
  128. const types = [];
  129. for (let i = 0; i < paths.length; i++) {
  130. const path = paths[i];
  131. if (path.isLogicalExpression()) {
  132. if (path.node.operator === "&&") {
  133. paths.push(path.get("left"));
  134. paths.push(path.get("right"));
  135. }
  136. } else if (path.isBinaryExpression()) {
  137. const type = inferAnnotationFromBinaryExpression(name, path);
  138. if (type) types.push(type);
  139. }
  140. }
  141. if (types.length) {
  142. return {
  143. typeAnnotation: (0, _util.createUnionType)(types),
  144. ifStatement
  145. };
  146. }
  147. return getConditionalAnnotation(binding, ifStatement, name);
  148. }
  149. //# sourceMappingURL=inferer-reference.js.map