inferer-reference.js 5.2 KB

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