source: trip-planner-front/node_modules/@babel/traverse/lib/path/inference/inferer-reference.js@ ceaed42

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

initial commit

  • Property mode set to 100644
File size: 5.0 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7
8var _t = require("@babel/types");
9
10const {
11 BOOLEAN_NUMBER_BINARY_OPERATORS,
12 createFlowUnionType,
13 createTSUnionType,
14 createTypeAnnotationBasedOnTypeof,
15 createUnionTypeAnnotation,
16 isTSTypeAnnotation,
17 numberTypeAnnotation,
18 voidTypeAnnotation
19} = _t;
20
21function _default(node) {
22 if (!this.isReferenced()) return;
23 const binding = this.scope.getBinding(node.name);
24
25 if (binding) {
26 if (binding.identifier.typeAnnotation) {
27 return binding.identifier.typeAnnotation;
28 } else {
29 return getTypeAnnotationBindingConstantViolations(binding, this, node.name);
30 }
31 }
32
33 if (node.name === "undefined") {
34 return voidTypeAnnotation();
35 } else if (node.name === "NaN" || node.name === "Infinity") {
36 return numberTypeAnnotation();
37 } else if (node.name === "arguments") {}
38}
39
40function getTypeAnnotationBindingConstantViolations(binding, path, name) {
41 const types = [];
42 const functionConstantViolations = [];
43 let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
44 const testType = getConditionalAnnotation(binding, path, name);
45
46 if (testType) {
47 const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
48 constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0);
49 types.push(testType.typeAnnotation);
50 }
51
52 if (constantViolations.length) {
53 constantViolations.push(...functionConstantViolations);
54
55 for (const violation of constantViolations) {
56 types.push(violation.getTypeAnnotation());
57 }
58 }
59
60 if (!types.length) {
61 return;
62 }
63
64 if (isTSTypeAnnotation(types[0]) && createTSUnionType) {
65 return createTSUnionType(types);
66 }
67
68 if (createFlowUnionType) {
69 return createFlowUnionType(types);
70 }
71
72 return createUnionTypeAnnotation(types);
73}
74
75function getConstantViolationsBefore(binding, path, functions) {
76 const violations = binding.constantViolations.slice();
77 violations.unshift(binding.path);
78 return violations.filter(violation => {
79 violation = violation.resolve();
80
81 const status = violation._guessExecutionStatusRelativeTo(path);
82
83 if (functions && status === "unknown") functions.push(violation);
84 return status === "before";
85 });
86}
87
88function inferAnnotationFromBinaryExpression(name, path) {
89 const operator = path.node.operator;
90 const right = path.get("right").resolve();
91 const left = path.get("left").resolve();
92 let target;
93
94 if (left.isIdentifier({
95 name
96 })) {
97 target = right;
98 } else if (right.isIdentifier({
99 name
100 })) {
101 target = left;
102 }
103
104 if (target) {
105 if (operator === "===") {
106 return target.getTypeAnnotation();
107 }
108
109 if (BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
110 return numberTypeAnnotation();
111 }
112
113 return;
114 }
115
116 if (operator !== "===" && operator !== "==") return;
117 let typeofPath;
118 let typePath;
119
120 if (left.isUnaryExpression({
121 operator: "typeof"
122 })) {
123 typeofPath = left;
124 typePath = right;
125 } else if (right.isUnaryExpression({
126 operator: "typeof"
127 })) {
128 typeofPath = right;
129 typePath = left;
130 }
131
132 if (!typeofPath) return;
133 if (!typeofPath.get("argument").isIdentifier({
134 name
135 })) return;
136 typePath = typePath.resolve();
137 if (!typePath.isLiteral()) return;
138 const typeValue = typePath.node.value;
139 if (typeof typeValue !== "string") return;
140 return createTypeAnnotationBasedOnTypeof(typeValue);
141}
142
143function getParentConditionalPath(binding, path, name) {
144 let parentPath;
145
146 while (parentPath = path.parentPath) {
147 if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
148 if (path.key === "test") {
149 return;
150 }
151
152 return parentPath;
153 }
154
155 if (parentPath.isFunction()) {
156 if (parentPath.parentPath.scope.getBinding(name) !== binding) return;
157 }
158
159 path = parentPath;
160 }
161}
162
163function getConditionalAnnotation(binding, path, name) {
164 const ifStatement = getParentConditionalPath(binding, path, name);
165 if (!ifStatement) return;
166 const test = ifStatement.get("test");
167 const paths = [test];
168 const types = [];
169
170 for (let i = 0; i < paths.length; i++) {
171 const path = paths[i];
172
173 if (path.isLogicalExpression()) {
174 if (path.node.operator === "&&") {
175 paths.push(path.get("left"));
176 paths.push(path.get("right"));
177 }
178 } else if (path.isBinaryExpression()) {
179 const type = inferAnnotationFromBinaryExpression(name, path);
180 if (type) types.push(type);
181 }
182 }
183
184 if (types.length) {
185 if (isTSTypeAnnotation(types[0]) && createTSUnionType) {
186 return {
187 typeAnnotation: createTSUnionType(types),
188 ifStatement
189 };
190 }
191
192 if (createFlowUnionType) {
193 return {
194 typeAnnotation: createFlowUnionType(types),
195 ifStatement
196 };
197 }
198
199 return {
200 typeAnnotation: createUnionTypeAnnotation(types),
201 ifStatement
202 };
203 }
204
205 return getConditionalAnnotation(ifStatement, name);
206}
Note: See TracBrowser for help on using the repository browser.