inferers.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = exports.Identifier = undefined;
  4. var _infererReference = require("./inferer-reference");
  5. Object.defineProperty(exports, "Identifier", {
  6. enumerable: true,
  7. get: function get() {
  8. return _interopRequireDefault(_infererReference).default;
  9. }
  10. });
  11. exports.VariableDeclarator = VariableDeclarator;
  12. exports.TypeCastExpression = TypeCastExpression;
  13. exports.NewExpression = NewExpression;
  14. exports.TemplateLiteral = TemplateLiteral;
  15. exports.UnaryExpression = UnaryExpression;
  16. exports.BinaryExpression = BinaryExpression;
  17. exports.LogicalExpression = LogicalExpression;
  18. exports.ConditionalExpression = ConditionalExpression;
  19. exports.SequenceExpression = SequenceExpression;
  20. exports.AssignmentExpression = AssignmentExpression;
  21. exports.UpdateExpression = UpdateExpression;
  22. exports.StringLiteral = StringLiteral;
  23. exports.NumericLiteral = NumericLiteral;
  24. exports.BooleanLiteral = BooleanLiteral;
  25. exports.NullLiteral = NullLiteral;
  26. exports.RegExpLiteral = RegExpLiteral;
  27. exports.ObjectExpression = ObjectExpression;
  28. exports.ArrayExpression = ArrayExpression;
  29. exports.RestElement = RestElement;
  30. exports.CallExpression = CallExpression;
  31. exports.TaggedTemplateExpression = TaggedTemplateExpression;
  32. var _babelTypes = require("babel-types");
  33. var t = _interopRequireWildcard(_babelTypes);
  34. 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; } }
  35. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  36. function VariableDeclarator() {
  37. var id = this.get("id");
  38. if (id.isIdentifier()) {
  39. return this.get("init").getTypeAnnotation();
  40. } else {
  41. return;
  42. }
  43. }
  44. function TypeCastExpression(node) {
  45. return node.typeAnnotation;
  46. }
  47. TypeCastExpression.validParent = true;
  48. function NewExpression(node) {
  49. if (this.get("callee").isIdentifier()) {
  50. return t.genericTypeAnnotation(node.callee);
  51. }
  52. }
  53. function TemplateLiteral() {
  54. return t.stringTypeAnnotation();
  55. }
  56. function UnaryExpression(node) {
  57. var operator = node.operator;
  58. if (operator === "void") {
  59. return t.voidTypeAnnotation();
  60. } else if (t.NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
  61. return t.numberTypeAnnotation();
  62. } else if (t.STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
  63. return t.stringTypeAnnotation();
  64. } else if (t.BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
  65. return t.booleanTypeAnnotation();
  66. }
  67. }
  68. function BinaryExpression(node) {
  69. var operator = node.operator;
  70. if (t.NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
  71. return t.numberTypeAnnotation();
  72. } else if (t.BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
  73. return t.booleanTypeAnnotation();
  74. } else if (operator === "+") {
  75. var right = this.get("right");
  76. var left = this.get("left");
  77. if (left.isBaseType("number") && right.isBaseType("number")) {
  78. return t.numberTypeAnnotation();
  79. } else if (left.isBaseType("string") || right.isBaseType("string")) {
  80. return t.stringTypeAnnotation();
  81. }
  82. return t.unionTypeAnnotation([t.stringTypeAnnotation(), t.numberTypeAnnotation()]);
  83. }
  84. }
  85. function LogicalExpression() {
  86. return t.createUnionTypeAnnotation([this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()]);
  87. }
  88. function ConditionalExpression() {
  89. return t.createUnionTypeAnnotation([this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()]);
  90. }
  91. function SequenceExpression() {
  92. return this.get("expressions").pop().getTypeAnnotation();
  93. }
  94. function AssignmentExpression() {
  95. return this.get("right").getTypeAnnotation();
  96. }
  97. function UpdateExpression(node) {
  98. var operator = node.operator;
  99. if (operator === "++" || operator === "--") {
  100. return t.numberTypeAnnotation();
  101. }
  102. }
  103. function StringLiteral() {
  104. return t.stringTypeAnnotation();
  105. }
  106. function NumericLiteral() {
  107. return t.numberTypeAnnotation();
  108. }
  109. function BooleanLiteral() {
  110. return t.booleanTypeAnnotation();
  111. }
  112. function NullLiteral() {
  113. return t.nullLiteralTypeAnnotation();
  114. }
  115. function RegExpLiteral() {
  116. return t.genericTypeAnnotation(t.identifier("RegExp"));
  117. }
  118. function ObjectExpression() {
  119. return t.genericTypeAnnotation(t.identifier("Object"));
  120. }
  121. function ArrayExpression() {
  122. return t.genericTypeAnnotation(t.identifier("Array"));
  123. }
  124. function RestElement() {
  125. return ArrayExpression();
  126. }
  127. RestElement.validParent = true;
  128. function Func() {
  129. return t.genericTypeAnnotation(t.identifier("Function"));
  130. }
  131. exports.FunctionExpression = Func;
  132. exports.ArrowFunctionExpression = Func;
  133. exports.FunctionDeclaration = Func;
  134. exports.ClassExpression = Func;
  135. exports.ClassDeclaration = Func;
  136. function CallExpression() {
  137. return resolveCall(this.get("callee"));
  138. }
  139. function TaggedTemplateExpression() {
  140. return resolveCall(this.get("tag"));
  141. }
  142. function resolveCall(callee) {
  143. callee = callee.resolve();
  144. if (callee.isFunction()) {
  145. if (callee.is("async")) {
  146. if (callee.is("generator")) {
  147. return t.genericTypeAnnotation(t.identifier("AsyncIterator"));
  148. } else {
  149. return t.genericTypeAnnotation(t.identifier("Promise"));
  150. }
  151. } else {
  152. if (callee.node.returnType) {
  153. return callee.node.returnType;
  154. } else {}
  155. }
  156. }
  157. }