| 1 | import { |
| 2 | BOOLEAN_BINARY_OPERATORS, |
| 3 | BOOLEAN_UNARY_OPERATORS, |
| 4 | NUMBER_BINARY_OPERATORS, |
| 5 | NUMBER_UNARY_OPERATORS, |
| 6 | STRING_UNARY_OPERATORS, |
| 7 | anyTypeAnnotation, |
| 8 | arrayTypeAnnotation, |
| 9 | booleanTypeAnnotation, |
| 10 | buildMatchMemberExpression, |
| 11 | genericTypeAnnotation, |
| 12 | identifier, |
| 13 | nullLiteralTypeAnnotation, |
| 14 | numberTypeAnnotation, |
| 15 | stringTypeAnnotation, |
| 16 | tupleTypeAnnotation, |
| 17 | unionTypeAnnotation, |
| 18 | voidTypeAnnotation, |
| 19 | isIdentifier, |
| 20 | } from "@babel/types"; |
| 21 | import type * as t from "@babel/types"; |
| 22 | |
| 23 | export { default as Identifier } from "./inferer-reference"; |
| 24 | |
| 25 | import { createUnionType } from "./util"; |
| 26 | import type NodePath from ".."; |
| 27 | |
| 28 | export function VariableDeclarator(this: NodePath<t.VariableDeclarator>) { |
| 29 | if (!this.get("id").isIdentifier()) return; |
| 30 | return this.get("init").getTypeAnnotation(); |
| 31 | } |
| 32 | |
| 33 | export function TypeCastExpression(node: t.TypeCastExpression) { |
| 34 | return node.typeAnnotation; |
| 35 | } |
| 36 | |
| 37 | TypeCastExpression.validParent = true; |
| 38 | |
| 39 | export function TSAsExpression(node: t.TSAsExpression) { |
| 40 | return node.typeAnnotation; |
| 41 | } |
| 42 | |
| 43 | TSAsExpression.validParent = true; |
| 44 | |
| 45 | export function TSNonNullExpression(this: NodePath<t.TSNonNullExpression>) { |
| 46 | return this.get("expression").getTypeAnnotation(); |
| 47 | } |
| 48 | |
| 49 | export function NewExpression( |
| 50 | this: NodePath<t.NewExpression>, |
| 51 | node: t.NewExpression, |
| 52 | ) { |
| 53 | if (node.callee.type === "Identifier") { |
| 54 | // only resolve identifier callee |
| 55 | return genericTypeAnnotation(node.callee); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | export function TemplateLiteral() { |
| 60 | return stringTypeAnnotation(); |
| 61 | } |
| 62 | |
| 63 | export function UnaryExpression(node: t.UnaryExpression) { |
| 64 | const operator = node.operator; |
| 65 | |
| 66 | if (operator === "void") { |
| 67 | return voidTypeAnnotation(); |
| 68 | } else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) { |
| 69 | return numberTypeAnnotation(); |
| 70 | } else if (STRING_UNARY_OPERATORS.indexOf(operator) >= 0) { |
| 71 | return stringTypeAnnotation(); |
| 72 | } else if (BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) { |
| 73 | return booleanTypeAnnotation(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | export function BinaryExpression( |
| 78 | this: NodePath<t.BinaryExpression>, |
| 79 | node: t.BinaryExpression, |
| 80 | ) { |
| 81 | const operator = node.operator; |
| 82 | |
| 83 | if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) { |
| 84 | return numberTypeAnnotation(); |
| 85 | } else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) { |
| 86 | return booleanTypeAnnotation(); |
| 87 | } else if (operator === "+") { |
| 88 | const right = this.get("right"); |
| 89 | const left = this.get("left"); |
| 90 | |
| 91 | if (left.isBaseType("number") && right.isBaseType("number")) { |
| 92 | // both numbers so this will be a number |
| 93 | return numberTypeAnnotation(); |
| 94 | } else if (left.isBaseType("string") || right.isBaseType("string")) { |
| 95 | // one is a string so the result will be a string |
| 96 | return stringTypeAnnotation(); |
| 97 | } |
| 98 | |
| 99 | // unsure if left and right are strings or numbers so stay on the safe side |
| 100 | return unionTypeAnnotation([ |
| 101 | stringTypeAnnotation(), |
| 102 | numberTypeAnnotation(), |
| 103 | ]); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | export function LogicalExpression(this: NodePath<t.LogicalExpression>) { |
| 108 | const argumentTypes = [ |
| 109 | this.get("left").getTypeAnnotation(), |
| 110 | this.get("right").getTypeAnnotation(), |
| 111 | ]; |
| 112 | |
| 113 | return createUnionType(argumentTypes); |
| 114 | } |
| 115 | |
| 116 | export function ConditionalExpression(this: NodePath<t.ConditionalExpression>) { |
| 117 | const argumentTypes = [ |
| 118 | this.get("consequent").getTypeAnnotation(), |
| 119 | this.get("alternate").getTypeAnnotation(), |
| 120 | ]; |
| 121 | |
| 122 | return createUnionType(argumentTypes); |
| 123 | } |
| 124 | |
| 125 | export function SequenceExpression(this: NodePath<t.SequenceExpression>) { |
| 126 | return this.get("expressions").pop().getTypeAnnotation(); |
| 127 | } |
| 128 | |
| 129 | export function ParenthesizedExpression( |
| 130 | this: NodePath<t.ParenthesizedExpression>, |
| 131 | ) { |
| 132 | return this.get("expression").getTypeAnnotation(); |
| 133 | } |
| 134 | |
| 135 | export function AssignmentExpression(this: NodePath<t.AssignmentExpression>) { |
| 136 | return this.get("right").getTypeAnnotation(); |
| 137 | } |
| 138 | |
| 139 | export function UpdateExpression( |
| 140 | this: NodePath<t.UpdateExpression>, |
| 141 | node: t.UpdateExpression, |
| 142 | ) { |
| 143 | const operator = node.operator; |
| 144 | if (operator === "++" || operator === "--") { |
| 145 | return numberTypeAnnotation(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | export function StringLiteral() { |
| 150 | return stringTypeAnnotation(); |
| 151 | } |
| 152 | |
| 153 | export function NumericLiteral() { |
| 154 | return numberTypeAnnotation(); |
| 155 | } |
| 156 | |
| 157 | export function BooleanLiteral() { |
| 158 | return booleanTypeAnnotation(); |
| 159 | } |
| 160 | |
| 161 | export function NullLiteral() { |
| 162 | return nullLiteralTypeAnnotation(); |
| 163 | } |
| 164 | |
| 165 | export function RegExpLiteral() { |
| 166 | return genericTypeAnnotation(identifier("RegExp")); |
| 167 | } |
| 168 | |
| 169 | export function ObjectExpression() { |
| 170 | return genericTypeAnnotation(identifier("Object")); |
| 171 | } |
| 172 | |
| 173 | export function ArrayExpression() { |
| 174 | return genericTypeAnnotation(identifier("Array")); |
| 175 | } |
| 176 | |
| 177 | export function RestElement() { |
| 178 | return ArrayExpression(); |
| 179 | } |
| 180 | |
| 181 | RestElement.validParent = true; |
| 182 | |
| 183 | function Func() { |
| 184 | return genericTypeAnnotation(identifier("Function")); |
| 185 | } |
| 186 | |
| 187 | export { |
| 188 | Func as FunctionExpression, |
| 189 | Func as ArrowFunctionExpression, |
| 190 | Func as FunctionDeclaration, |
| 191 | Func as ClassExpression, |
| 192 | Func as ClassDeclaration, |
| 193 | }; |
| 194 | |
| 195 | const isArrayFrom = buildMatchMemberExpression("Array.from"); |
| 196 | const isObjectKeys = buildMatchMemberExpression("Object.keys"); |
| 197 | const isObjectValues = buildMatchMemberExpression("Object.values"); |
| 198 | const isObjectEntries = buildMatchMemberExpression("Object.entries"); |
| 199 | export function CallExpression(this: NodePath<t.CallExpression>) { |
| 200 | const { callee } = this.node; |
| 201 | if (isObjectKeys(callee)) { |
| 202 | return arrayTypeAnnotation(stringTypeAnnotation()); |
| 203 | } else if ( |
| 204 | isArrayFrom(callee) || |
| 205 | isObjectValues(callee) || |
| 206 | // Detect "var foo = Array()" calls so we can optimize for arrays vs iterables. |
| 207 | isIdentifier(callee, { name: "Array" }) |
| 208 | ) { |
| 209 | return arrayTypeAnnotation(anyTypeAnnotation()); |
| 210 | } else if (isObjectEntries(callee)) { |
| 211 | return arrayTypeAnnotation( |
| 212 | tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]), |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | return resolveCall(this.get("callee")); |
| 217 | } |
| 218 | |
| 219 | export function TaggedTemplateExpression( |
| 220 | this: NodePath<t.TaggedTemplateExpression>, |
| 221 | ) { |
| 222 | return resolveCall(this.get("tag")); |
| 223 | } |
| 224 | |
| 225 | function resolveCall(callee: NodePath) { |
| 226 | callee = callee.resolve(); |
| 227 | |
| 228 | if (callee.isFunction()) { |
| 229 | const { node } = callee; |
| 230 | if (node.async) { |
| 231 | if (node.generator) { |
| 232 | return genericTypeAnnotation(identifier("AsyncIterator")); |
| 233 | } else { |
| 234 | return genericTypeAnnotation(identifier("Promise")); |
| 235 | } |
| 236 | } else { |
| 237 | if (node.generator) { |
| 238 | return genericTypeAnnotation(identifier("Iterator")); |
| 239 | } else if (callee.node.returnType) { |
| 240 | return callee.node.returnType; |
| 241 | } else { |
| 242 | // todo: get union type of all return arguments |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |