* 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 )
| 4560 | * found only by its file path (`com/example/Foo.java` or `.kt`) still counts. |
| 4561 | */ |
| 4562 | function resolveJavaImportedReference( |
| 4563 | ref: UnresolvedRef, |
| 4564 | imports: ImportMapping[], |
| 4565 | context: ResolutionContext |
| 4566 | ): ResolvedRef | null { |
| 4567 | if (imports.length === 0) return null; |
| 4568 | |
| 4569 | for (const imp of importsReadBy(imports, ref.referenceName)) { |
| 4570 | const matchesBare = imp.localName === ref.referenceName; |
| 4571 | const matchesQualified = ref.referenceName.startsWith(imp.localName + '.'); |
| 4572 | if (!matchesBare && !matchesQualified) continue; |
| 4573 | const memberPath = matchesBare ? [] : ref.referenceName.substring(imp.localName.length + 1).split('.'); |
| 4574 | |
| 4575 | // An imported type, and what its name reaches: its members and nested |
| 4576 | // types. An imported function or property (`import p.helper`, an |
| 4577 | // extension among them) is left to the name strategies, which know a |
| 4578 | // Kotlin call's receiver: an untyped chain's last call is no call of it. |
| 4579 | const named = jvmImportedDeclarations(imp.source, context); |
| 4580 | const declarations = named.filter((n) => JVM_IMPORT_TYPE_KINDS.has(n.kind)); |
| 4581 | // A Kotlin name can be a function and a type at once (Compose's |
| 4582 | // `fun AppBar(…)` beside `sealed interface AppBar`): a call of the name |
| 4583 | // alone is the function's, which the file's own path still finds. |
| 4584 | const callOfFunction = matchesBare && ref.referenceKind === 'calls' && |
| 4585 | named.some((n) => n.kind === 'function' || n.kind === 'method'); |
| 4586 | if (declarations.length > 0 && !callOfFunction) { |
| 4587 | const owner = declarations.length === 1 ? declarations[0]! : pickJvmDeclaration(declarations, ref); |
| 4588 | const target = jvmImportedMember(owner, memberPath, ref, context); |
| 4589 | // Of a member's overloads, the one the call's argument count fits: |
| 4590 | // `ItemStack.Hash.of(stack, registries)`, not `of(stack)`. |
| 4591 | if (target) return retargetSelfOverload({ original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' }, ref, context); |
| 4592 | continue; |
| 4593 | } |
| 4594 | |
| 4595 | // `UtilsKt.helper()` from Java: a top-level Kotlin function. |
| 4596 | if (ref.language === 'java' && memberPath.length === 1) { |
| 4597 | const facadeMembers = kotlinFileFacadeMembers(imp.source, memberPath[0]!, context); |
| 4598 | if (facadeMembers.length > 0) { |
| 4599 | const target = facadeMembers.length === 1 ? facadeMembers[0]! : pickClosestJvmCandidate(facadeMembers, ref.filePath); |
| 4600 | return retargetSelfOverload({ original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' }, ref, context); |
| 4601 | } |
| 4602 | } |
| 4603 | |
| 4604 | // A declaration the index holds under another name: found by the |
| 4605 | // import's path (`com/example/Foo.java` or `.kt`, whichever the |
| 4606 | // declaring file is), under any source root. Its member, by name. |
| 4607 | const memberName = matchesBare ? imp.localName : memberPath.join('.'); |
| 4608 | const fqnPath = imp.source.replace(/\./g, '/'); |
| 4609 | const ownerPath = imp.source.includes('.') ? imp.source.substring(0, imp.source.lastIndexOf('.')).replace(/\./g, '/') : ''; |
| 4610 | const inFile = (node: Node, file: string): boolean => { |
| 4611 | const fp = node.filePath.replace(/\\/g, '/'); |
| 4612 | return ['.java', '.kt'].some((ext) => fp.endsWith(file + ext) && (fp.length === file.length + ext.length || fp[fp.length - file.length - ext.length - 1] === '/')); |
| 4613 | }; |
| 4614 | const candidates = context.getNodesByName(memberName).filter((n) => JVM_IMPORT_LANGUAGES.has(n.language)); |
| 4615 | const byPath = candidates.find((n) => inFile(n, fqnPath)) ?? |
| 4616 | // `import static com.example.Foo.bar;`: the member's owner names the file. |
| 4617 | (matchesBare && ownerPath ? candidates.find((n) => inFile(n, ownerPath)) : undefined); |
| 4618 | if (byPath) return retargetSelfOverload({ original: ref, targetNodeId: byPath.id, confidence: 0.9, resolvedBy: 'import' }, ref, context); |
| 4619 | } |
no test coverage detected