inferers.js 6.5 KB

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