inferers.js 6.8 KB

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