| 1 | import type NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# from "../index"scip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/; |
| 2 | import { |
| 3 | BOOLEAN_NUMBER_BINARY_OPERATORSscip-typescript npm @babel/types 7.20.7 src/constants/`index.ts`/BOOLEAN_NUMBER_BINARY_OPERATORS., |
| 4 | createTypeAnnotationBasedOnTypeof, |
| 5 | numberTypeAnnotationscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/numberTypeAnnotation()., |
| 6 | voidTypeAnnotationscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/voidTypeAnnotation()., |
| 7 | } from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/; |
| 8 | import type * as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/ from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/; |
| 9 | import type Bindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/Binding# from "../../scope/binding"scip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/; |
| 10 | |
| 11 | import { createUnionTypescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`util.ts`/createUnionType(). } from "./util"scip-typescript npm @babel/traverse 7.20.13 src/path/inference/`util.ts`/; |
| 12 | |
| 13 | export default function (this: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Identifierscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#>, node: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Identifierscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#) { |
| 14 | if (!this.isReferencedscip-typescript npm @babel/traverse 7.20.13 src/path/lib/`virtual-types-validator.ts`/VirtualTypeNodePathValidators#isReferenced().()) return; |
| 15 | |
| 16 | // check if a binding exists of this value and if so then return a union type of all |
| 17 | // possible types that the binding could be |
| 18 | const binding = this.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..getBindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#getBinding().(node.namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name.); |
| 19 | if (binding) { |
| 20 | if (binding.identifierscip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/Binding#identifier..typeAnnotationscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#typeAnnotation.) { |
| 21 | return binding.identifierscip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/Binding#identifier..typeAnnotationscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#typeAnnotation.; |
| 22 | } else { |
| 23 | return getTypeAnnotationBindingConstantViolationsscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().( |
| 24 | binding, |
| 25 | this, |
| 26 | node.namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name., |
| 27 | ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // built-in values |
| 32 | if (node.namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name. === "undefined") { |
| 33 | return voidTypeAnnotationscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/voidTypeAnnotation().(); |
| 34 | } else if (node.namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name. === "NaN" || node.namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name. === "Infinity") { |
| 35 | return numberTypeAnnotationscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/numberTypeAnnotation().(); |
| 36 | } else if (node.namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name. === "arguments") { |
| 37 | // todo |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function getTypeAnnotationBindingConstantViolationsscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().( |
| 42 | bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().(binding): Bindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/Binding#, |
| 43 | pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().(path): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Identifierscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#>, |
| 44 | namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().(name): string, |
| 45 | ) { |
| 46 | const types = []; |
| 47 | |
| 48 | const functionConstantViolations: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#[] = []; |
| 49 | let constantViolations = getConstantViolationsBeforescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().( |
| 50 | bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().(binding), |
| 51 | pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().(path), |
| 52 | functionConstantViolations, |
| 53 | ); |
| 54 | |
| 55 | const testType = getConditionalAnnotationscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().(binding), pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().(path), namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().(name)); |
| 56 | if (testType) { |
| 57 | const testConstantViolations = getConstantViolationsBeforescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().( |
| 58 | bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getTypeAnnotationBindingConstantViolations().(binding), |
| 59 | testType.ifStatementscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().typeLiteral22:ifStatement., |
| 60 | ); |
| 61 | |
| 62 | // remove constant violations observed before the IfStatement |
| 63 | constantViolations = constantViolations.filterscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#filter().( |
| 64 | path => testConstantViolations.indexOfscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#indexOf().(path) < 0, |
| 65 | ); |
| 66 | |
| 67 | // clear current types and add in observed test type |
| 68 | types.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(testType.typeAnnotationscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().typeLiteral22:typeAnnotation.); |
| 69 | } |
| 70 | |
| 71 | if (constantViolations.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.) { |
| 72 | // pick one constant from each scope which will represent the last possible |
| 73 | // control flow path that it could've taken/been |
| 74 | /* This code is broken for the following problems: |
| 75 | * It thinks that assignments can only happen in scopes. |
| 76 | * What about conditionals, if statements without block, |
| 77 | * or guarded assignments. |
| 78 | * It also checks to see if one of the assignments is in the |
| 79 | * same scope and uses that as the only "violation". However, |
| 80 | * the binding is returned by `getConstantViolationsBefore` so we for |
| 81 | * sure always going to return that as the only "violation". |
| 82 | let rawConstantViolations = constantViolations.reverse(); |
| 83 | let visitedScopes = []; |
| 84 | constantViolations = []; |
| 85 | for (let violation of (rawConstantViolations: Array<NodePath>)) { |
| 86 | let violationScope = violation.scope; |
| 87 | if (visitedScopes.indexOf(violationScope) >= 0) continue; |
| 88 | |
| 89 | visitedScopes.push(violationScope); |
| 90 | constantViolations.push(violation); |
| 91 | |
| 92 | if (violationScope === path.scope) { |
| 93 | constantViolations = [violation]; |
| 94 | break; |
| 95 | } |
| 96 | }*/ |
| 97 | |
| 98 | // add back on function constant violations since we can't track calls |
| 99 | constantViolations.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(...functionConstantViolations); |
| 100 | |
| 101 | // push on inferred types of violated paths |
| 102 | for (const violation of constantViolations) { |
| 103 | types.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(violation.getTypeAnnotationscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`index.ts`/getTypeAnnotation().()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (!types.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | return createUnionTypescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`util.ts`/createUnionType().(types); |
| 112 | } |
| 113 | |
| 114 | function getConstantViolationsBeforescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().( |
| 115 | bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().(binding): Bindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/Binding#, |
| 116 | pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().(path): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, |
| 117 | functionsscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().(functions)?: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#[], |
| 118 | ) { |
| 119 | const violations = bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().(binding).constantViolationsscip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/Binding#constantViolations..slicescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#slice().(); |
| 120 | violations.unshiftscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#unshift().(bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().(binding).pathscip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/Binding#path.); |
| 121 | return violations.filterscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#filter().(violation => { |
| 122 | violation = violation.resolvescip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/resolve().(); |
| 123 | const status = violation._guessExecutionStatusRelativeToscip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/_guessExecutionStatusRelativeTo().(pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().(path)); |
| 124 | if (functionsscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().(functions) && status === "unknown") functionsscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConstantViolationsBefore().(functions).pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(violation); |
| 125 | return status === "before"; |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | function inferAnnotationFromBinaryExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/inferAnnotationFromBinaryExpression().( |
| 130 | namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/inferAnnotationFromBinaryExpression().(name): string, |
| 131 | pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/inferAnnotationFromBinaryExpression().(path): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.BinaryExpressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#>, |
| 132 | ) { |
| 133 | const operator = pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/inferAnnotationFromBinaryExpression().(path).nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#operator.; |
| 134 | |
| 135 | const right = pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/inferAnnotationFromBinaryExpression().(path).get("right").resolvescip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/resolve().(); |
| 136 | const left = pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/inferAnnotationFromBinaryExpression().(path).get("left").resolvescip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/resolve().(); |
| 137 | |
| 138 | let target; |
| 139 | if (left.isIdentifierscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isIdentifier().({ namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/name0: })) { |
| 140 | target = right; |
| 141 | } else if (right.isIdentifierscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isIdentifier().({ namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/name1: })) { |
| 142 | target = left; |
| 143 | } |
| 144 | |
| 145 | if (target) { |
| 146 | if (operator === "===") { |
| 147 | return target.getTypeAnnotationscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`index.ts`/getTypeAnnotation().(); |
| 148 | } |
| 149 | if (BOOLEAN_NUMBER_BINARY_OPERATORSscip-typescript npm @babel/types 7.20.7 src/constants/`index.ts`/BOOLEAN_NUMBER_BINARY_OPERATORS..indexOfscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#indexOf().(operator) >= 0) { |
| 150 | return numberTypeAnnotationscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/numberTypeAnnotation().(); |
| 151 | } |
| 152 | |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (operator !== "===" && operator !== "==") return; |
| 157 | |
| 158 | // |
| 159 | let typeofPath: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.UnaryExpressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/UnaryExpression#>; |
| 160 | let typePath: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression#>; |
| 161 | if (left.isUnaryExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isUnaryExpression().({ operatorscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/operator0:: "typeof" })) { |
| 162 | typeofPath = left; |
| 163 | typePath = right as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression#>; |
| 164 | } else if (right.isUnaryExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isUnaryExpression().({ operatorscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/operator1:: "typeof" })) { |
| 165 | typeofPath = right; |
| 166 | typePath = left as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression#>; |
| 167 | } |
| 168 | |
| 169 | if (!typeofPath) return; |
| 170 | // and that the argument of the typeof path references us! |
| 171 | if (!typeofPath.get("argument").isIdentifierscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isIdentifier().({ namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/name2: })) return; |
| 172 | |
| 173 | // ensure that the type path is a Literal |
| 174 | typePath = typePath.resolvescip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/resolve().() as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression#>; |
| 175 | if (!typePath.isLiteralscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isLiteral().()) return; |
| 176 | |
| 177 | // and that it's a string so we can infer it |
| 178 | // @ts-expect-error todo(flow->ts): value is not defined for NullLiteral and some other |
| 179 | const typeValue = typePath.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..value; |
| 180 | if (typeof typeValue !== "string") return; |
| 181 | |
| 182 | // turn type value into a type annotation |
| 183 | // @ts-expect-error todo(flow->ts): move validation from helper or relax type constraint to just a string |
| 184 | return createTypeAnnotationBasedOnTypeof(typeValue); |
| 185 | } |
| 186 | |
| 187 | function getParentConditionalPathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().( |
| 188 | bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().(binding): Bindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/Binding#, |
| 189 | pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().(path): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, |
| 190 | namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().(name): string, |
| 191 | ) { |
| 192 | let parentPath; |
| 193 | while ((parentPath = pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().(path).parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath.)) { |
| 194 | if (parentPath.isIfStatementscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isIfStatement().() || parentPath.isConditionalExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isConditionalExpression().()) { |
| 195 | if (pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().(path).keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. === "test") { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | return parentPath as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.IfStatementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/IfStatement# | tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.ConditionalExpressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ConditionalExpression#>; |
| 200 | } |
| 201 | if (parentPath.isFunctionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isFunction().()) { |
| 202 | if (parentPath.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath..scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..getBindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#getBinding().(namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().(name)) !== bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().(binding)) return; |
| 203 | } |
| 204 | |
| 205 | pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().(path) = parentPath; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | function getConditionalAnnotationscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().<Tscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().[T] extends tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node#>( |
| 210 | bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(binding): Bindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`binding.ts`/Binding#, |
| 211 | pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(path): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<Tscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().[T]>, |
| 212 | namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(name)?: string, |
| 213 | ): { |
| 214 | typeAnnotationscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().typeLiteral22:typeAnnotation.: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.FlowTypescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/FlowType# | tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.TSTypescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TSType#; |
| 215 | ifStatementscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().typeLiteral22:ifStatement.: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.IfStatementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/IfStatement# | tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.ConditionalExpressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ConditionalExpression#>; |
| 216 | } { |
| 217 | const ifStatement = getParentConditionalPathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getParentConditionalPath().(bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(binding), pathscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(path), namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(name)); |
| 218 | if (!ifStatement) return; |
| 219 | |
| 220 | const test = ifStatement.get("test"); |
| 221 | const paths = [test]; |
| 222 | const types = []; |
| 223 | |
| 224 | for (let i = 0; i < paths.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.; i++) { |
| 225 | const path = paths[i]; |
| 226 | |
| 227 | if (path.isLogicalExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isLogicalExpression().()) { |
| 228 | if (path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/LogicalExpression#operator. === "&&") { |
| 229 | paths.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(path.get("left")); |
| 230 | paths.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(path.get("right")); |
| 231 | } |
| 232 | } else if (path.isBinaryExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isBinaryExpression().()) { |
| 233 | const type = inferAnnotationFromBinaryExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/inferAnnotationFromBinaryExpression().(namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(name), path); |
| 234 | if (type) types.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().(type); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (types.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.) { |
| 239 | return { |
| 240 | typeAnnotationscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().typeLiteral22:typeAnnotation.: createUnionTypescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`util.ts`/createUnionType().(types), |
| 241 | ifStatementscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().typeLiteral22:ifStatement., |
| 242 | }; |
| 243 | } |
| 244 | |
| 245 | return getConditionalAnnotationscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(bindingscip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(binding), ifStatement, namescip-typescript npm @babel/traverse 7.20.13 src/path/inference/`inferer-reference.ts`/getConditionalAnnotation().(name)); |
| 246 | } |