source: trip-planner-front/node_modules/@babel/traverse/lib/path/inference/inferers.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.8 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.VariableDeclarator = VariableDeclarator;
7exports.TypeCastExpression = TypeCastExpression;
8exports.NewExpression = NewExpression;
9exports.TemplateLiteral = TemplateLiteral;
10exports.UnaryExpression = UnaryExpression;
11exports.BinaryExpression = BinaryExpression;
12exports.LogicalExpression = LogicalExpression;
13exports.ConditionalExpression = ConditionalExpression;
14exports.SequenceExpression = SequenceExpression;
15exports.ParenthesizedExpression = ParenthesizedExpression;
16exports.AssignmentExpression = AssignmentExpression;
17exports.UpdateExpression = UpdateExpression;
18exports.StringLiteral = StringLiteral;
19exports.NumericLiteral = NumericLiteral;
20exports.BooleanLiteral = BooleanLiteral;
21exports.NullLiteral = NullLiteral;
22exports.RegExpLiteral = RegExpLiteral;
23exports.ObjectExpression = ObjectExpression;
24exports.ArrayExpression = ArrayExpression;
25exports.RestElement = RestElement;
26exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = Func;
27exports.CallExpression = CallExpression;
28exports.TaggedTemplateExpression = TaggedTemplateExpression;
29Object.defineProperty(exports, "Identifier", {
30 enumerable: true,
31 get: function () {
32 return _infererReference.default;
33 }
34});
35
36var _t = require("@babel/types");
37
38var _infererReference = require("./inferer-reference");
39
40const {
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 createFlowUnionType,
51 createTSUnionType,
52 createUnionTypeAnnotation,
53 genericTypeAnnotation,
54 identifier,
55 isTSTypeAnnotation,
56 nullLiteralTypeAnnotation,
57 numberTypeAnnotation,
58 stringTypeAnnotation,
59 tupleTypeAnnotation,
60 unionTypeAnnotation,
61 voidTypeAnnotation
62} = _t;
63
64function VariableDeclarator() {
65 var _type;
66
67 const id = this.get("id");
68 if (!id.isIdentifier()) return;
69 const init = this.get("init");
70 let type = init.getTypeAnnotation();
71
72 if (((_type = type) == null ? void 0 : _type.type) === "AnyTypeAnnotation") {
73 if (init.isCallExpression() && init.get("callee").isIdentifier({
74 name: "Array"
75 }) && !init.scope.hasBinding("Array", true)) {
76 type = ArrayExpression();
77 }
78 }
79
80 return type;
81}
82
83function TypeCastExpression(node) {
84 return node.typeAnnotation;
85}
86
87TypeCastExpression.validParent = true;
88
89function NewExpression(node) {
90 if (this.get("callee").isIdentifier()) {
91 return genericTypeAnnotation(node.callee);
92 }
93}
94
95function TemplateLiteral() {
96 return stringTypeAnnotation();
97}
98
99function UnaryExpression(node) {
100 const operator = node.operator;
101
102 if (operator === "void") {
103 return voidTypeAnnotation();
104 } else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
105 return numberTypeAnnotation();
106 } else if (STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
107 return stringTypeAnnotation();
108 } else if (BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
109 return booleanTypeAnnotation();
110 }
111}
112
113function BinaryExpression(node) {
114 const operator = node.operator;
115
116 if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
117 return numberTypeAnnotation();
118 } else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
119 return booleanTypeAnnotation();
120 } else if (operator === "+") {
121 const right = this.get("right");
122 const left = this.get("left");
123
124 if (left.isBaseType("number") && right.isBaseType("number")) {
125 return numberTypeAnnotation();
126 } else if (left.isBaseType("string") || right.isBaseType("string")) {
127 return stringTypeAnnotation();
128 }
129
130 return unionTypeAnnotation([stringTypeAnnotation(), numberTypeAnnotation()]);
131 }
132}
133
134function LogicalExpression() {
135 const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
136
137 if (isTSTypeAnnotation(argumentTypes[0]) && createTSUnionType) {
138 return createTSUnionType(argumentTypes);
139 }
140
141 if (createFlowUnionType) {
142 return createFlowUnionType(argumentTypes);
143 }
144
145 return createUnionTypeAnnotation(argumentTypes);
146}
147
148function ConditionalExpression() {
149 const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
150
151 if (isTSTypeAnnotation(argumentTypes[0]) && createTSUnionType) {
152 return createTSUnionType(argumentTypes);
153 }
154
155 if (createFlowUnionType) {
156 return createFlowUnionType(argumentTypes);
157 }
158
159 return createUnionTypeAnnotation(argumentTypes);
160}
161
162function SequenceExpression() {
163 return this.get("expressions").pop().getTypeAnnotation();
164}
165
166function ParenthesizedExpression() {
167 return this.get("expression").getTypeAnnotation();
168}
169
170function AssignmentExpression() {
171 return this.get("right").getTypeAnnotation();
172}
173
174function UpdateExpression(node) {
175 const operator = node.operator;
176
177 if (operator === "++" || operator === "--") {
178 return numberTypeAnnotation();
179 }
180}
181
182function StringLiteral() {
183 return stringTypeAnnotation();
184}
185
186function NumericLiteral() {
187 return numberTypeAnnotation();
188}
189
190function BooleanLiteral() {
191 return booleanTypeAnnotation();
192}
193
194function NullLiteral() {
195 return nullLiteralTypeAnnotation();
196}
197
198function RegExpLiteral() {
199 return genericTypeAnnotation(identifier("RegExp"));
200}
201
202function ObjectExpression() {
203 return genericTypeAnnotation(identifier("Object"));
204}
205
206function ArrayExpression() {
207 return genericTypeAnnotation(identifier("Array"));
208}
209
210function RestElement() {
211 return ArrayExpression();
212}
213
214RestElement.validParent = true;
215
216function Func() {
217 return genericTypeAnnotation(identifier("Function"));
218}
219
220const isArrayFrom = buildMatchMemberExpression("Array.from");
221const isObjectKeys = buildMatchMemberExpression("Object.keys");
222const isObjectValues = buildMatchMemberExpression("Object.values");
223const isObjectEntries = buildMatchMemberExpression("Object.entries");
224
225function CallExpression() {
226 const {
227 callee
228 } = this.node;
229
230 if (isObjectKeys(callee)) {
231 return arrayTypeAnnotation(stringTypeAnnotation());
232 } else if (isArrayFrom(callee) || isObjectValues(callee)) {
233 return arrayTypeAnnotation(anyTypeAnnotation());
234 } else if (isObjectEntries(callee)) {
235 return arrayTypeAnnotation(tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]));
236 }
237
238 return resolveCall(this.get("callee"));
239}
240
241function TaggedTemplateExpression() {
242 return resolveCall(this.get("tag"));
243}
244
245function resolveCall(callee) {
246 callee = callee.resolve();
247
248 if (callee.isFunction()) {
249 if (callee.is("async")) {
250 if (callee.is("generator")) {
251 return genericTypeAnnotation(identifier("AsyncIterator"));
252 } else {
253 return genericTypeAnnotation(identifier("Promise"));
254 }
255 } else {
256 if (callee.node.returnType) {
257 return callee.node.returnType;
258 } else {}
259 }
260 }
261}
Note: See TracBrowser for help on using the repository browser.