* 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 )
| 4313 | * found only by its file path (`com/example/Foo.java` or `.kt`) still counts. |
| 4314 | */ |
| 4315 | function resolveJavaImportedReference( |
| 4316 | ref: UnresolvedRef, |
| 4317 | imports: ImportMapping[], |
| 4318 | context: ResolutionContext |
| 4319 | ): ResolvedRef | null { |
| 4320 | if (imports.length === 0) return null; |
| 4321 | |
| 4322 | for (const imp of imports) { |
| 4323 | const matchesBare = imp.localName === ref.referenceName; |
| 4324 | const matchesQualified = ref.referenceName.startsWith(imp.localName + '.'); |
| 4325 | if (!matchesBare && !matchesQualified) continue; |
| 4326 | const memberPath = matchesBare ? [] : ref.referenceName.substring(imp.localName.length + 1).split('.'); |
| 4327 | |
| 4328 | // An imported type, and what its name reaches: its members and nested |
| 4329 | // types. An imported function or property (`import p.helper`, an |
| 4330 | // extension among them) is left to the name strategies, which know a |
| 4331 | // Kotlin call's receiver: an untyped chain's last call is no call of it. |
| 4332 | const named = jvmImportedDeclarations(imp.source, context); |
| 4333 | const declarations = named.filter((n) => JVM_IMPORT_TYPE_KINDS.has(n.kind)); |
| 4334 | // A Kotlin name can be a function and a type at once (Compose's |
| 4335 | // `fun AppBar(…)` beside `sealed interface AppBar`): a call of the name |
| 4336 | // alone is the function's, which the file's own path still finds. |
| 4337 | const callOfFunction = matchesBare && ref.referenceKind === 'calls' && |
| 4338 | named.some((n) => n.kind === 'function' || n.kind === 'method'); |
| 4339 | if (declarations.length > 0 && !callOfFunction) { |
| 4340 | const owner = declarations.length === 1 ? declarations[0]! : pickJvmDeclaration(declarations, ref); |
| 4341 | const target = jvmImportedMember(owner, memberPath, ref, context); |
| 4342 | // Of a member's overloads, the one the call's argument count fits: |
| 4343 | // `ItemStack.Hash.of(stack, registries)`, not `of(stack)`. |
| 4344 | if (target) return retargetSelfOverload({ original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' }, ref, context); |
| 4345 | continue; |
| 4346 | } |
| 4347 | |
| 4348 | // `UtilsKt.helper()` from Java: a top-level Kotlin function. |
| 4349 | if (ref.language === 'java' && memberPath.length === 1) { |
| 4350 | const facadeMembers = kotlinFileFacadeMembers(imp.source, memberPath[0]!, context); |
| 4351 | if (facadeMembers.length > 0) { |
| 4352 | const target = facadeMembers.length === 1 ? facadeMembers[0]! : pickClosestJvmCandidate(facadeMembers, ref.filePath); |
| 4353 | return retargetSelfOverload({ original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' }, ref, context); |
| 4354 | } |
| 4355 | } |
| 4356 | |
| 4357 | // A declaration the index holds under another name: found by the |
| 4358 | // import's path (`com/example/Foo.java` or `.kt`, whichever the |
| 4359 | // declaring file is), under any source root. Its member, by name. |
| 4360 | const memberName = matchesBare ? imp.localName : memberPath.join('.'); |
| 4361 | const fqnPath = imp.source.replace(/\./g, '/'); |
| 4362 | const ownerPath = imp.source.includes('.') ? imp.source.substring(0, imp.source.lastIndexOf('.')).replace(/\./g, '/') : ''; |
| 4363 | const inFile = (node: Node, file: string): boolean => { |
| 4364 | const fp = node.filePath.replace(/\\/g, '/'); |
| 4365 | return ['.java', '.kt'].some((ext) => fp.endsWith(file + ext) && (fp.length === file.length + ext.length || fp[fp.length - file.length - ext.length - 1] === '/')); |
| 4366 | }; |
| 4367 | const candidates = context.getNodesByName(memberName).filter((n) => JVM_IMPORT_LANGUAGES.has(n.language)); |
| 4368 | const byPath = candidates.find((n) => inFile(n, fqnPath)) ?? |
| 4369 | // `import static com.example.Foo.bar;`: the member's owner names the file. |
| 4370 | (matchesBare && ownerPath ? candidates.find((n) => inFile(n, ownerPath)) : undefined); |
| 4371 | if (byPath) return retargetSelfOverload({ original: ref, targetNodeId: byPath.id, confidence: 0.9, resolvedBy: 'import' }, ref, context); |
| 4372 | } |
no test coverage detected