source: imaps-frontend/node_modules/@babel/traverse/lib/path/inference/inferer-reference.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: 4.5 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7var _t = require("@babel/types");
8var _util = require("./util.js");
9const {
10 BOOLEAN_NUMBER_BINARY_OPERATORS,
11 createTypeAnnotationBasedOnTypeof,
12 numberTypeAnnotation,
13 voidTypeAnnotation
14} = _t;
15function _default(node) {
16 if (!this.isReferenced()) return;
17 const binding = this.scope.getBinding(node.name);
18 if (binding) {
19 if (binding.identifier.typeAnnotation) {
20 return binding.identifier.typeAnnotation;
21 } else {
22 return getTypeAnnotationBindingConstantViolations(binding, this, node.name);
23 }
24 }
25 if (node.name === "undefined") {
26 return voidTypeAnnotation();
27 } else if (node.name === "NaN" || node.name === "Infinity") {
28 return numberTypeAnnotation();
29 } else if (node.name === "arguments") {}
30}
31function getTypeAnnotationBindingConstantViolations(binding, path, name) {
32 const types = [];
33 const functionConstantViolations = [];
34 let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
35 const testType = getConditionalAnnotation(binding, path, name);
36 if (testType) {
37 const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
38 constantViolations = constantViolations.filter(path => !testConstantViolations.includes(path));
39 types.push(testType.typeAnnotation);
40 }
41 if (constantViolations.length) {
42 constantViolations.push(...functionConstantViolations);
43 for (const violation of constantViolations) {
44 types.push(violation.getTypeAnnotation());
45 }
46 }
47 if (!types.length) {
48 return;
49 }
50 return (0, _util.createUnionType)(types);
51}
52function getConstantViolationsBefore(binding, path, functions) {
53 const violations = binding.constantViolations.slice();
54 violations.unshift(binding.path);
55 return violations.filter(violation => {
56 violation = violation.resolve();
57 const status = violation._guessExecutionStatusRelativeTo(path);
58 if (functions && status === "unknown") functions.push(violation);
59 return status === "before";
60 });
61}
62function inferAnnotationFromBinaryExpression(name, path) {
63 const operator = path.node.operator;
64 const right = path.get("right").resolve();
65 const left = path.get("left").resolve();
66 let target;
67 if (left.isIdentifier({
68 name
69 })) {
70 target = right;
71 } else if (right.isIdentifier({
72 name
73 })) {
74 target = left;
75 }
76 if (target) {
77 if (operator === "===") {
78 return target.getTypeAnnotation();
79 }
80 if (BOOLEAN_NUMBER_BINARY_OPERATORS.includes(operator)) {
81 return numberTypeAnnotation();
82 }
83 return;
84 }
85 if (operator !== "===" && operator !== "==") return;
86 let typeofPath;
87 let typePath;
88 if (left.isUnaryExpression({
89 operator: "typeof"
90 })) {
91 typeofPath = left;
92 typePath = right;
93 } else if (right.isUnaryExpression({
94 operator: "typeof"
95 })) {
96 typeofPath = right;
97 typePath = left;
98 }
99 if (!typeofPath) return;
100 if (!typeofPath.get("argument").isIdentifier({
101 name
102 })) return;
103 typePath = typePath.resolve();
104 if (!typePath.isLiteral()) return;
105 const typeValue = typePath.node.value;
106 if (typeof typeValue !== "string") return;
107 return createTypeAnnotationBasedOnTypeof(typeValue);
108}
109function getParentConditionalPath(binding, path, name) {
110 let parentPath;
111 while (parentPath = path.parentPath) {
112 if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
113 if (path.key === "test") {
114 return;
115 }
116 return parentPath;
117 }
118 if (parentPath.isFunction()) {
119 if (parentPath.parentPath.scope.getBinding(name) !== binding) return;
120 }
121 path = parentPath;
122 }
123}
124function getConditionalAnnotation(binding, path, name) {
125 const ifStatement = getParentConditionalPath(binding, path, name);
126 if (!ifStatement) return;
127 const test = ifStatement.get("test");
128 const paths = [test];
129 const types = [];
130 for (let i = 0; i < paths.length; i++) {
131 const path = paths[i];
132 if (path.isLogicalExpression()) {
133 if (path.node.operator === "&&") {
134 paths.push(path.get("left"));
135 paths.push(path.get("right"));
136 }
137 } else if (path.isBinaryExpression()) {
138 const type = inferAnnotationFromBinaryExpression(name, path);
139 if (type) types.push(type);
140 }
141 }
142 if (types.length) {
143 return {
144 typeAnnotation: (0, _util.createUnionType)(types),
145 ifStatement
146 };
147 }
148 return getConditionalAnnotation(binding, ifStatement, name);
149}
150
151//# sourceMappingURL=inferer-reference.js.map
Note: See TracBrowser for help on using the repository browser.