[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.VariableDeclarator = VariableDeclarator;
|
---|
| 7 | exports.TypeCastExpression = TypeCastExpression;
|
---|
| 8 | exports.NewExpression = NewExpression;
|
---|
| 9 | exports.TemplateLiteral = TemplateLiteral;
|
---|
| 10 | exports.UnaryExpression = UnaryExpression;
|
---|
| 11 | exports.BinaryExpression = BinaryExpression;
|
---|
| 12 | exports.LogicalExpression = LogicalExpression;
|
---|
| 13 | exports.ConditionalExpression = ConditionalExpression;
|
---|
| 14 | exports.SequenceExpression = SequenceExpression;
|
---|
| 15 | exports.ParenthesizedExpression = ParenthesizedExpression;
|
---|
| 16 | exports.AssignmentExpression = AssignmentExpression;
|
---|
| 17 | exports.UpdateExpression = UpdateExpression;
|
---|
| 18 | exports.StringLiteral = StringLiteral;
|
---|
| 19 | exports.NumericLiteral = NumericLiteral;
|
---|
| 20 | exports.BooleanLiteral = BooleanLiteral;
|
---|
| 21 | exports.NullLiteral = NullLiteral;
|
---|
| 22 | exports.RegExpLiteral = RegExpLiteral;
|
---|
| 23 | exports.ObjectExpression = ObjectExpression;
|
---|
| 24 | exports.ArrayExpression = ArrayExpression;
|
---|
| 25 | exports.RestElement = RestElement;
|
---|
| 26 | exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = Func;
|
---|
| 27 | exports.CallExpression = CallExpression;
|
---|
| 28 | exports.TaggedTemplateExpression = TaggedTemplateExpression;
|
---|
| 29 | Object.defineProperty(exports, "Identifier", {
|
---|
| 30 | enumerable: true,
|
---|
| 31 | get: function () {
|
---|
| 32 | return _infererReference.default;
|
---|
| 33 | }
|
---|
| 34 | });
|
---|
| 35 |
|
---|
| 36 | var _t = require("@babel/types");
|
---|
| 37 |
|
---|
| 38 | var _infererReference = require("./inferer-reference");
|
---|
| 39 |
|
---|
| 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 | 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 |
|
---|
| 64 | function 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 |
|
---|
| 83 | function TypeCastExpression(node) {
|
---|
| 84 | return node.typeAnnotation;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | TypeCastExpression.validParent = true;
|
---|
| 88 |
|
---|
| 89 | function NewExpression(node) {
|
---|
| 90 | if (this.get("callee").isIdentifier()) {
|
---|
| 91 | return genericTypeAnnotation(node.callee);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | function TemplateLiteral() {
|
---|
| 96 | return stringTypeAnnotation();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | function 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 |
|
---|
| 113 | function 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 |
|
---|
| 134 | function 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 |
|
---|
| 148 | function 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 |
|
---|
| 162 | function SequenceExpression() {
|
---|
| 163 | return this.get("expressions").pop().getTypeAnnotation();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | function ParenthesizedExpression() {
|
---|
| 167 | return this.get("expression").getTypeAnnotation();
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | function AssignmentExpression() {
|
---|
| 171 | return this.get("right").getTypeAnnotation();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | function UpdateExpression(node) {
|
---|
| 175 | const operator = node.operator;
|
---|
| 176 |
|
---|
| 177 | if (operator === "++" || operator === "--") {
|
---|
| 178 | return numberTypeAnnotation();
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | function StringLiteral() {
|
---|
| 183 | return stringTypeAnnotation();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | function NumericLiteral() {
|
---|
| 187 | return numberTypeAnnotation();
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | function BooleanLiteral() {
|
---|
| 191 | return booleanTypeAnnotation();
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | function NullLiteral() {
|
---|
| 195 | return nullLiteralTypeAnnotation();
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | function RegExpLiteral() {
|
---|
| 199 | return genericTypeAnnotation(identifier("RegExp"));
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | function ObjectExpression() {
|
---|
| 203 | return genericTypeAnnotation(identifier("Object"));
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | function ArrayExpression() {
|
---|
| 207 | return genericTypeAnnotation(identifier("Array"));
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | function RestElement() {
|
---|
| 211 | return ArrayExpression();
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | RestElement.validParent = true;
|
---|
| 215 |
|
---|
| 216 | function Func() {
|
---|
| 217 | return genericTypeAnnotation(identifier("Function"));
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | const isArrayFrom = buildMatchMemberExpression("Array.from");
|
---|
| 221 | const isObjectKeys = buildMatchMemberExpression("Object.keys");
|
---|
| 222 | const isObjectValues = buildMatchMemberExpression("Object.values");
|
---|
| 223 | const isObjectEntries = buildMatchMemberExpression("Object.entries");
|
---|
| 224 |
|
---|
| 225 | function 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 |
|
---|
| 241 | function TaggedTemplateExpression() {
|
---|
| 242 | return resolveCall(this.get("tag"));
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | function 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 | } |
---|