source: imaps-frontend/node_modules/@babel/traverse/lib/path/inference/inferers.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 6.5 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.ArrayExpression = ArrayExpression;
7exports.AssignmentExpression = AssignmentExpression;
8exports.BinaryExpression = BinaryExpression;
9exports.BooleanLiteral = BooleanLiteral;
10exports.CallExpression = CallExpression;
11exports.ConditionalExpression = ConditionalExpression;
12exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = Func;
13Object.defineProperty(exports, "Identifier", {
14 enumerable: true,
15 get: function () {
16 return _infererReference.default;
17 }
18});
19exports.LogicalExpression = LogicalExpression;
20exports.NewExpression = NewExpression;
21exports.NullLiteral = NullLiteral;
22exports.NumericLiteral = NumericLiteral;
23exports.ObjectExpression = ObjectExpression;
24exports.ParenthesizedExpression = ParenthesizedExpression;
25exports.RegExpLiteral = RegExpLiteral;
26exports.RestElement = RestElement;
27exports.SequenceExpression = SequenceExpression;
28exports.StringLiteral = StringLiteral;
29exports.TSAsExpression = TSAsExpression;
30exports.TSNonNullExpression = TSNonNullExpression;
31exports.TaggedTemplateExpression = TaggedTemplateExpression;
32exports.TemplateLiteral = TemplateLiteral;
33exports.TypeCastExpression = TypeCastExpression;
34exports.UnaryExpression = UnaryExpression;
35exports.UpdateExpression = UpdateExpression;
36exports.VariableDeclarator = VariableDeclarator;
37var _t = require("@babel/types");
38var _infererReference = require("./inferer-reference.js");
39var _util = require("./util.js");
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 genericTypeAnnotation,
51 identifier,
52 nullLiteralTypeAnnotation,
53 numberTypeAnnotation,
54 stringTypeAnnotation,
55 tupleTypeAnnotation,
56 unionTypeAnnotation,
57 voidTypeAnnotation,
58 isIdentifier
59} = _t;
60function VariableDeclarator() {
61 if (!this.get("id").isIdentifier()) return;
62 return this.get("init").getTypeAnnotation();
63}
64function TypeCastExpression(node) {
65 return node.typeAnnotation;
66}
67TypeCastExpression.validParent = true;
68function TSAsExpression(node) {
69 return node.typeAnnotation;
70}
71TSAsExpression.validParent = true;
72function TSNonNullExpression() {
73 return this.get("expression").getTypeAnnotation();
74}
75function NewExpression(node) {
76 if (node.callee.type === "Identifier") {
77 return genericTypeAnnotation(node.callee);
78 }
79}
80function TemplateLiteral() {
81 return stringTypeAnnotation();
82}
83function 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}
95function 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}
112function LogicalExpression() {
113 const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
114 return (0, _util.createUnionType)(argumentTypes);
115}
116function ConditionalExpression() {
117 const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
118 return (0, _util.createUnionType)(argumentTypes);
119}
120function SequenceExpression() {
121 return this.get("expressions").pop().getTypeAnnotation();
122}
123function ParenthesizedExpression() {
124 return this.get("expression").getTypeAnnotation();
125}
126function AssignmentExpression() {
127 return this.get("right").getTypeAnnotation();
128}
129function UpdateExpression(node) {
130 const operator = node.operator;
131 if (operator === "++" || operator === "--") {
132 return numberTypeAnnotation();
133 }
134}
135function StringLiteral() {
136 return stringTypeAnnotation();
137}
138function NumericLiteral() {
139 return numberTypeAnnotation();
140}
141function BooleanLiteral() {
142 return booleanTypeAnnotation();
143}
144function NullLiteral() {
145 return nullLiteralTypeAnnotation();
146}
147function RegExpLiteral() {
148 return genericTypeAnnotation(identifier("RegExp"));
149}
150function ObjectExpression() {
151 return genericTypeAnnotation(identifier("Object"));
152}
153function ArrayExpression() {
154 return genericTypeAnnotation(identifier("Array"));
155}
156function RestElement() {
157 return ArrayExpression();
158}
159RestElement.validParent = true;
160function Func() {
161 return genericTypeAnnotation(identifier("Function"));
162}
163const isArrayFrom = buildMatchMemberExpression("Array.from");
164const isObjectKeys = buildMatchMemberExpression("Object.keys");
165const isObjectValues = buildMatchMemberExpression("Object.values");
166const isObjectEntries = buildMatchMemberExpression("Object.entries");
167function 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}
182function TaggedTemplateExpression() {
183 return resolveCall(this.get("tag"));
184}
185function 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
Note: See TracBrowser for help on using the repository browser.