* Resolve a Java/Kotlin reference through an import of a project declaration: * `Foo.bar(...)` after `import com.example.Foo`, the class itself (`new Foo()` * emits `Foo` as a `references`/`instantiates` ref), and the member an * `import static com.example.Foo.bar` names. The import is read by qu
( ref: UnresolvedRef, imports: ImportMapping[], context: ResolutionContext )
| 4112 | * found only by its file path (`com/example/Foo.java` or `.kt`) still counts. |
| 4113 | */ |
| 4114 | function resolveJavaImportedReference( |
| 4115 | ref: UnresolvedRef, |
| 4116 | imports: ImportMapping[], |
| 4117 | context: ResolutionContext |
| 4118 | ): ResolvedRef | null { |
| 4119 | if (imports.length === 0) return null; |
| 4120 | |
| 4121 | for (const imp of imports) { |
| 4122 | const matchesBare = imp.localName === ref.referenceName; |
| 4123 | const matchesQualified = ref.referenceName.startsWith(imp.localName + '.'); |
| 4124 | if (!matchesBare && !matchesQualified) continue; |
| 4125 | const memberPath = matchesBare ? [] : ref.referenceName.substring(imp.localName.length + 1).split('.'); |
| 4126 | |
| 4127 | // An imported type, and what its name reaches: its members and nested |
| 4128 | // types. An imported function or property (`import p.helper`, an |
| 4129 | // extension among them) is left to the name strategies, which know a |
| 4130 | // Kotlin call's receiver: an untyped chain's last call is no call of it. |
| 4131 | const named = jvmImportedDeclarations(imp.source, context); |
| 4132 | const declarations = named.filter((n) => JVM_IMPORT_TYPE_KINDS.has(n.kind)); |
| 4133 | // A Kotlin name can be a function and a type at once (Compose's |
| 4134 | // `fun AppBar(…)` beside `sealed interface AppBar`): a call of the name |
| 4135 | // alone is the function's, which the file's own path still finds. |
| 4136 | const callOfFunction = matchesBare && ref.referenceKind === 'calls' && |
| 4137 | named.some((n) => n.kind === 'function' || n.kind === 'method'); |
| 4138 | if (declarations.length > 0 && !callOfFunction) { |
| 4139 | const owner = declarations.length === 1 ? declarations[0]! : pickJvmDeclaration(declarations, ref); |
| 4140 | const target = jvmImportedMember(owner, memberPath, ref, context); |
| 4141 | // Of a member's overloads, the one the call's argument count fits: |
| 4142 | // `ItemStack.Hash.of(stack, registries)`, not `of(stack)`. |
| 4143 | if (target) return retargetSelfOverload({ original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' }, ref, context); |
| 4144 | continue; |
| 4145 | } |
| 4146 | |
| 4147 | // `UtilsKt.helper()` from Java: a top-level Kotlin function. |
| 4148 | if (ref.language === 'java' && memberPath.length === 1) { |
| 4149 | const facadeMembers = kotlinFileFacadeMembers(imp.source, memberPath[0]!, context); |
| 4150 | if (facadeMembers.length > 0) { |
| 4151 | const target = facadeMembers.length === 1 ? facadeMembers[0]! : pickClosestJvmCandidate(facadeMembers, ref.filePath); |
| 4152 | return retargetSelfOverload({ original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' }, ref, context); |
| 4153 | } |
| 4154 | } |
| 4155 | |
| 4156 | // A declaration the index holds under another name: found by the |
| 4157 | // import's path (`com/example/Foo.java` or `.kt`, whichever the |
| 4158 | // declaring file is), under any source root. Its member, by name. |
| 4159 | const memberName = matchesBare ? imp.localName : memberPath.join('.'); |
| 4160 | const fqnPath = imp.source.replace(/\./g, '/'); |
| 4161 | const ownerPath = imp.source.includes('.') ? imp.source.substring(0, imp.source.lastIndexOf('.')).replace(/\./g, '/') : ''; |
| 4162 | const inFile = (node: Node, file: string): boolean => { |
| 4163 | const fp = node.filePath.replace(/\\/g, '/'); |
| 4164 | return ['.java', '.kt'].some((ext) => fp.endsWith(file + ext) && (fp.length === file.length + ext.length || fp[fp.length - file.length - ext.length - 1] === '/')); |
| 4165 | }; |
| 4166 | const candidates = context.getNodesByName(memberName).filter((n) => JVM_IMPORT_LANGUAGES.has(n.language)); |
| 4167 | const byPath = candidates.find((n) => inFile(n, fqnPath)) ?? |
| 4168 | // `import static com.example.Foo.bar;`: the member's owner names the file. |
| 4169 | (matchesBare && ownerPath ? candidates.find((n) => inFile(n, ownerPath)) : undefined); |
| 4170 | if (byPath) return retargetSelfOverload({ original: ref, targetNodeId: byPath.id, confidence: 0.9, resolvedBy: 'import' }, ref, context); |
| 4171 | } |
no test coverage detected