inferers.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.VariableDeclarator = VariableDeclarator;
  6. exports.TypeCastExpression = TypeCastExpression;
  7. exports.NewExpression = NewExpression;
  8. exports.TemplateLiteral = TemplateLiteral;
  9. exports.UnaryExpression = UnaryExpression;
  10. exports.BinaryExpression = BinaryExpression;
  11. exports.LogicalExpression = LogicalExpression;
  12. exports.ConditionalExpression = ConditionalExpression;
  13. exports.SequenceExpression = SequenceExpression;
  14. exports.ParenthesizedExpression = ParenthesizedExpression;
  15. exports.AssignmentExpression = AssignmentExpression;
  16. exports.UpdateExpression = UpdateExpression;
  17. exports.StringLiteral = StringLiteral;
  18. exports.NumericLiteral = NumericLiteral;
  19. exports.BooleanLiteral = BooleanLiteral;
  20. exports.NullLiteral = NullLiteral;
  21. exports.RegExpLiteral = RegExpLiteral;
  22. exports.ObjectExpression = ObjectExpression;
  23. exports.ArrayExpression = ArrayExpression;
  24. exports.RestElement = RestElement;
  25. exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = Func;
  26. exports.CallExpression = CallExpression;
  27. exports.TaggedTemplateExpression = TaggedTemplateExpression;
  28. Object.defineProperty(exports, "Identifier", {
  29. enumerable: true,
  30. get: function () {
  31. return _infererReference.default;
  32. }
  33. });
  34. var t = require("@babel/types");
  35. var _infererReference = require("./inferer-reference");
  36. function VariableDeclarator() {
  37. var _type;
  38. const id = this.get("id");
  39. if (!id.isIdentifier()) return;
  40. const init = this.get("init");
  41. let type = init.getTypeAnnotation();
  42. if (((_type = type) == null ? void 0 : _type.type) === "AnyTypeAnnotation") {
  43. if (init.isCallExpression() && init.get("callee").isIdentifier({
  44. name: "Array"
  45. }) && !init.scope.hasBinding("Array", true)) {
  46. type = ArrayExpression();
  47. }
  48. }
  49. return type;
  50. }
  51. function TypeCastExpression(node) {
  52. return node.typeAnnotation;
  53. }
  54. TypeCastExpression.validParent = true;
  55. function NewExpression(node) {
  56. if (this.get("callee").isIdentifier()) {
  57. return t.genericTypeAnnotation(node.callee);
  58. }
  59. }
  60. function TemplateLiteral() {
  61. return t.stringTypeAnnotation();
  62. }
  63. function UnaryExpression(node) {
  64. const operator = node.operator;
  65. if (operator === "void") {
  66. return t.voidTypeAnnotation();
  67. } else if (t.NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
  68. return t.numberTypeAnnotation();
  69. } else if (t.STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
  70. return t.stringTypeAnnotation();
  71. } else if (t.BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
  72. return t.booleanTypeAnnotation();
  73. }
  74. }
  75. function BinaryExpression(node) {
  76. const operator = node.operator;
  77. if (t.NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
  78. return t.numberTypeAnnotation();
  79. } else if (t.BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
  80. return t.booleanTypeAnnotation();
  81. } else if (operator === "+") {
  82. const right = this.get("right");
  83. const left = this.get("left");
  84. if (left.isBaseType("number") && right.isBaseType("number")) {
  85. return t.numberTypeAnnotation();
  86. } else if (left.isBaseType("string") || right.isBaseType("string")) {
  87. return t.stringTypeAnnotation();
  88. }
  89. return t.unionTypeAnnotation([t.stringTypeAnnotation(), t.numberTypeAnnotation()]);
  90. }
  91. }
  92. function LogicalExpression() {
  93. const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
  94. if (t.isTSTypeAnnotation(argumentTypes[0]) && t.createTSUnionType) {
  95. return t.createTSUnionType(argumentTypes);
  96. }
  97. if (t.createFlowUnionType) {
  98. return t.createFlowUnionType(argumentTypes);
  99. }
  100. return t.createUnionTypeAnnotation(argumentTypes);
  101. }
  102. function ConditionalExpression() {
  103. const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
  104. if (t.isTSTypeAnnotation(argumentTypes[0]) && t.createTSUnionType) {
  105. return t.createTSUnionType(argumentTypes);
  106. }
  107. if (t.createFlowUnionType) {
  108. return t.createFlowUnionType(argumentTypes);
  109. }
  110. return t.createUnionTypeAnnotation(argumentTypes);
  111. }
  112. function SequenceExpression() {
  113. return this.get("expressions").pop().getTypeAnnotation();
  114. }
  115. function ParenthesizedExpression() {
  116. return this.get("expression").getTypeAnnotation();
  117. }
  118. function AssignmentExpression() {
  119. return this.get("right").getTypeAnnotation();
  120. }
  121. function UpdateExpression(node) {
  122. const operator = node.operator;
  123. if (operator === "++" || operator === "--") {
  124. return t.numberTypeAnnotation();
  125. }
  126. }
  127. function StringLiteral() {
  128. return t.stringTypeAnnotation();
  129. }
  130. function NumericLiteral() {
  131. return t.numberTypeAnnotation();
  132. }
  133. function BooleanLiteral() {
  134. return t.booleanTypeAnnotation();
  135. }
  136. function NullLiteral() {
  137. return t.nullLiteralTypeAnnotation();
  138. }
  139. function RegExpLiteral() {
  140. return t.genericTypeAnnotation(t.identifier("RegExp"));
  141. }
  142. function ObjectExpression() {
  143. return t.genericTypeAnnotation(t.identifier("Object"));
  144. }
  145. function ArrayExpression() {
  146. return t.genericTypeAnnotation(t.identifier("Array"));
  147. }
  148. function RestElement() {
  149. return ArrayExpression();
  150. }
  151. RestElement.validParent = true;
  152. function Func() {
  153. return t.genericTypeAnnotation(t.identifier("Function"));
  154. }
  155. const isArrayFrom = t.buildMatchMemberExpression("Array.from");
  156. const isObjectKeys = t.buildMatchMemberExpression("Object.keys");
  157. const isObjectValues = t.buildMatchMemberExpression("Object.values");
  158. const isObjectEntries = t.buildMatchMemberExpression("Object.entries");
  159. function CallExpression() {
  160. const {
  161. callee
  162. } = this.node;
  163. if (isObjectKeys(callee)) {
  164. return t.arrayTypeAnnotation(t.stringTypeAnnotation());
  165. } else if (isArrayFrom(callee) || isObjectValues(callee)) {
  166. return t.arrayTypeAnnotation(t.anyTypeAnnotation());
  167. } else if (isObjectEntries(callee)) {
  168. return t.arrayTypeAnnotation(t.tupleTypeAnnotation([t.stringTypeAnnotation(), t.anyTypeAnnotation()]));
  169. }
  170. return resolveCall(this.get("callee"));
  171. }
  172. function TaggedTemplateExpression() {
  173. return resolveCall(this.get("tag"));
  174. }
  175. function resolveCall(callee) {
  176. callee = callee.resolve();
  177. if (callee.isFunction()) {
  178. if (callee.is("async")) {
  179. if (callee.is("generator")) {
  180. return t.genericTypeAnnotation(t.identifier("AsyncIterator"));
  181. } else {
  182. return t.genericTypeAnnotation(t.identifier("Promise"));
  183. }
  184. } else {
  185. if (callee.node.returnType) {
  186. return callee.node.returnType;
  187. } else {}
  188. }
  189. }
  190. }