Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inferer-reference.js 5.2KB

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