inferers.js 6.7 KB

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