| 1 | import type NodePath from "../index"; |
| 2 | import * as inferers from "./inferers"; |
| 3 | import { |
| 4 | anyTypeAnnotation, |
| 5 | isAnyTypeAnnotation, |
| 6 | isArrayTypeAnnotation, |
| 7 | isBooleanTypeAnnotation, |
| 8 | isEmptyTypeAnnotation, |
| 9 | isFlowBaseAnnotation, |
| 10 | isGenericTypeAnnotation, |
| 11 | isIdentifier, |
| 12 | isMixedTypeAnnotation, |
| 13 | isNumberTypeAnnotation, |
| 14 | isStringTypeAnnotation, |
| 15 | isTSArrayType, |
| 16 | isTSTypeAnnotation, |
| 17 | isTSTypeReference, |
| 18 | isTupleTypeAnnotation, |
| 19 | isTypeAnnotation, |
| 20 | isUnionTypeAnnotation, |
| 21 | isVoidTypeAnnotation, |
| 22 | stringTypeAnnotation, |
| 23 | voidTypeAnnotation, |
| 24 | } from "@babel/types"; |
| 25 | import type * as t from "@babel/types"; |
| 26 | |
| 27 | /** |
| 28 | * Infer the type of the current `NodePath`. |
| 29 | */ |
| 30 | |
| 31 | export function getTypeAnnotation(this: NodePath): t.FlowType | t.TSType { |
| 32 | let type = this.getData("typeAnnotation"); |
| 33 | if (type != null) { |
| 34 | return type; |
| 35 | } |
| 36 | type = this._getTypeAnnotation() || anyTypeAnnotation(); |
| 37 | if (isTypeAnnotation(type) || isTSTypeAnnotation(type)) { |
| 38 | type = type.typeAnnotation; |
| 39 | } |
| 40 | this.setData("typeAnnotation", type); |
| 41 | return type; |
| 42 | } |
| 43 | |
| 44 | // Used to avoid infinite recursion in cases like |
| 45 | // var b, c; if (0) { c = 1; b = c; } c = b; |
| 46 | // It also works with indirect recursion. |
| 47 | const typeAnnotationInferringNodes = new WeakSet(); |
| 48 | |
| 49 | /** |
| 50 | * todo: split up this method |
| 51 | */ |
| 52 | |
| 53 | export function _getTypeAnnotation(this: NodePath): any { |
| 54 | const node = this.node; |
| 55 | |
| 56 | if (!node) { |
| 57 | // handle initializerless variables, add in checks for loop initializers too |
| 58 | if (this.key === "init" && this.parentPath.isVariableDeclarator()) { |
| 59 | const declar = this.parentPath.parentPath; |
| 60 | const declarParent = declar.parentPath; |
| 61 | |
| 62 | // for (let NODE in bar) {} |
| 63 | if (declar.key === "left" && declarParent.isForInStatement()) { |
| 64 | return stringTypeAnnotation(); |
| 65 | } |
| 66 | |
| 67 | // for (let NODE of bar) {} |
| 68 | if (declar.key === "left" && declarParent.isForOfStatement()) { |
| 69 | return anyTypeAnnotation(); |
| 70 | } |
| 71 | |
| 72 | return voidTypeAnnotation(); |
| 73 | } else { |
| 74 | return; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // @ts-expect-error typeAnnotation may not index node |
| 79 | if (node.typeAnnotation) { |
| 80 | // @ts-expect-error typeAnnotation may not index node |
| 81 | return node.typeAnnotation; |
| 82 | } |
| 83 | |
| 84 | if (typeAnnotationInferringNodes.has(node)) { |
| 85 | // Bail out from type inference to avoid infinite loops |
| 86 | return; |
| 87 | } |
| 88 | typeAnnotationInferringNodes.add(node); |
| 89 | |
| 90 | try { |
| 91 | let inferer = |
| 92 | // @ts-expect-error inferers do not cover all AST types |
| 93 | inferers[node.type]; |
| 94 | if (inferer) { |
| 95 | return inferer.call(this, node); |
| 96 | } |
| 97 | |
| 98 | // @ts-expect-error inferers do not cover all AST types |
| 99 | inferer = inferers[this.parentPath.type]; |
| 100 | if (inferer?.validParent) { |
| 101 | return this.parentPath.getTypeAnnotation(); |
| 102 | } |
| 103 | } finally { |
| 104 | typeAnnotationInferringNodes.delete(node); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | export function isBaseType( |
| 109 | this: NodePath, |
| 110 | baseName: string, |
| 111 | soft?: boolean, |
| 112 | ): boolean { |
| 113 | return _isBaseType(baseName, this.getTypeAnnotation(), soft); |
| 114 | } |
| 115 | |
| 116 | function _isBaseType( |
| 117 | baseName: string, |
| 118 | type?: t.FlowType | t.TSType, |
| 119 | soft?: boolean, |
| 120 | ): boolean { |
| 121 | if (baseName === "string") { |
| 122 | return isStringTypeAnnotation(type); |
| 123 | } else if (baseName === "number") { |
| 124 | return isNumberTypeAnnotation(type); |
| 125 | } else if (baseName === "boolean") { |
| 126 | return isBooleanTypeAnnotation(type); |
| 127 | } else if (baseName === "any") { |
| 128 | return isAnyTypeAnnotation(type); |
| 129 | } else if (baseName === "mixed") { |
| 130 | return isMixedTypeAnnotation(type); |
| 131 | } else if (baseName === "empty") { |
| 132 | return isEmptyTypeAnnotation(type); |
| 133 | } else if (baseName === "void") { |
| 134 | return isVoidTypeAnnotation(type); |
| 135 | } else { |
| 136 | if (soft) { |
| 137 | return false; |
| 138 | } else { |
| 139 | throw new Error(`Unknown base type ${baseName}`); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | export function couldBeBaseType(this: NodePath, name: string): boolean { |
| 145 | const type = this.getTypeAnnotation(); |
| 146 | if (isAnyTypeAnnotation(type)) return true; |
| 147 | |
| 148 | if (isUnionTypeAnnotation(type)) { |
| 149 | for (const type2 of type.types) { |
| 150 | if (isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) { |
| 151 | return true; |
| 152 | } |
| 153 | } |
| 154 | return false; |
| 155 | } else { |
| 156 | return _isBaseType(name, type, true); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | export function baseTypeStrictlyMatches( |
| 161 | this: NodePath, |
| 162 | rightArg: NodePath, |
| 163 | ): boolean { |
| 164 | const left = this.getTypeAnnotation(); |
| 165 | const right = rightArg.getTypeAnnotation(); |
| 166 | |
| 167 | if (!isAnyTypeAnnotation(left) && isFlowBaseAnnotation(left)) { |
| 168 | return right.type === left.type; |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | export function isGenericType(this: NodePath, genericName: string): boolean { |
| 174 | const type = this.getTypeAnnotation(); |
| 175 | if (genericName === "Array") { |
| 176 | // T[] |
| 177 | if ( |
| 178 | isTSArrayType(type) || |
| 179 | isArrayTypeAnnotation(type) || |
| 180 | isTupleTypeAnnotation(type) |
| 181 | ) { |
| 182 | return true; |
| 183 | } |
| 184 | } |
| 185 | return ( |
| 186 | (isGenericTypeAnnotation(type) && |
| 187 | isIdentifier(type.id, { |
| 188 | name: genericName, |
| 189 | })) || |
| 190 | (isTSTypeReference(type) && |
| 191 | isIdentifier(type.typeName, { |
| 192 | name: genericName, |
| 193 | })) |
| 194 | ); |
| 195 | } |