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