diff --git a/CHANGELOG.md b/CHANGELOG.md index 17022e634..5fd50d4f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -361,6 +361,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - In JavaScript, TypeScript, Python, Go, Rust, Java and Kotlin, a function named in the value of a top-level constant or variable, or of a class field, like `const routes = { home }`, `HANDLERS = {"home": home}`, a Go `map[string]func()` table, a Rust `static` list of functions, `static handlers = { click: onClick }`, a Java `List.of(Registry::home)` field or a Kotlin `listOf(::home)` property, is now referenced once, by the constant, variable, field or property that holds it. The file or class around it no longer also shows up as a second reference from the same place, so a function's callers and impact list each table once. - `codegraph sync` and the file watcher now follow an edit that changes which declaration a module exports as its default, like `export default A` becoming `export default B` while `A` stays, or the module dropping its default. References through `import Y from './x'`, `{ default as Y }`, `require('./x').default`, a barrel that passes the default on or a route that renders it as a layout, and calls on a value typed by it, now link to the new default, or stay unlinked, as a fresh index has them. Before, they kept pointing at the old declaration until the importing file changed or the project was indexed again. - `codegraph sync` and the file watcher now relink a named import that a name match had linked to a declaration elsewhere while its module didn't export the name, once an edit makes the module export it, like `import { EmptyState } from './layouts/shared'` after the barrel gains `export { EmptyState } from './EmptyState'`. An import a name match had linked to a class the module exported only as its default comes unlinked once the module stops exporting it. Before, the import, the references through it and calls on values typed by it kept the old link until the importing file changed or the project was indexed again. +- Kotlin calls through imported types now avoid unrelated same-named methods while retaining inherited members and project extension functions. (#1948) ## [1.6.2] - 2026-10-03 diff --git a/__tests__/kotlin-imported-type-call.test.ts b/__tests__/kotlin-imported-type-call.test.ts new file mode 100644 index 000000000..fbdc44b70 --- /dev/null +++ b/__tests__/kotlin-imported-type-call.test.ts @@ -0,0 +1,90 @@ +import { describe, expect, it } from 'vitest'; +import * as fs from 'fs'; +import * as os from 'os'; +import * as path from 'path'; +import CodeGraph from '../src/index'; + +describe('Kotlin calls through explicit imports', () => { + it('uses the imported type, while retaining values, extensions and nested constructors', async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-kotlin-import-')); + let cg: CodeGraph | undefined; + try { + for (const [file, source] of Object.entries({ + 'p/Outer.java': `package p; +public class Outer { public static class Inner { public Inner() {} } }`, + 'p/Factories.kt': `package p +object Factory { fun create(): Int = 2 } +class ExtFactory { companion object } +open class Base { fun hello() {} } +object Obj : Base() +object Modes +object Dimensions { val Height = 2 } +fun Int.toPx(): Int = this +class UnrelatedFactory { fun create(): Int = 1 }`, + 'p/Ext.kt': `package p +import androidx.compose.ui.Modifier +fun ExtFactory.Companion.extMake() {} +fun Modes.extOther() {} +fun Modes.hidden() {} +fun Modifier.pad() {}`, + 'q/Calls.kt': `package q +import android.app.TaskStackBuilder // external type +import androidx.savedstate.SavedStateRegistryController +import p.Factory as LocalFactory +import p.Factory +import p.ExtFactory // trailing comment +import p.Obj +import p.Modes +import p.extMake +import p.extOther +import p.pad +import androidx.compose.ui.Modifier +import p.Dimensions.Height +import p.toPx +import p.Outer +fun Modes.ext() {} +class Calls { + fun android() { TaskStackBuilder.create(null) } + fun androidx() { SavedStateRegistryController.create(this) } + fun aliased() { LocalFactory.create() } + fun direct() { Factory.create() } + fun extension() { Modes.ext() } + fun value() { Height.toPx() } + fun nested() { Outer.Inner() } + fun companionExtension() { ExtFactory.extMake() } + fun inherited() { Obj.hello() } + fun externalExtension() { Modifier.pad() } + fun crossFileExtension() { Modes.extOther() } + fun unimportedExtension() { Modes.hidden() } +}`, + })) { + const target = path.join(dir, file); + fs.mkdirSync(path.dirname(target), { recursive: true }); + fs.writeFileSync(target, source); + } + cg = CodeGraph.initSync(dir); + await cg.indexAll(); + const callees = (name: string) => { + const caller = cg!.searchNodes(name).map(r => r.node) + .find(n => n.qualifiedName === `q::Calls::${name}`)!; + return cg!.getCallees(caller.id).filter(c => c.edge.kind === 'calls') + .map(c => c.node.qualifiedName); + }; + expect(callees('android')).toEqual([]); + expect(callees('androidx')).toEqual([]); + expect(callees('aliased')).toEqual(['p::Factory::create']); + expect(callees('direct')).toEqual(['p::Factory::create']); + expect(callees('extension')).toEqual(['Modes::ext']); + expect(callees('value')).toEqual(['Int::toPx']); + expect(callees('nested')).toEqual(['p::Outer::Inner::Inner']); + expect(callees('companionExtension')).toEqual(['ExtFactory::extMake']); + expect(callees('inherited')).toEqual(['p::Base::hello']); + expect(callees('externalExtension')).toEqual(['Modifier::pad']); + expect(callees('crossFileExtension')).toEqual(['Modes::extOther']); + expect(callees('unimportedExtension')).toEqual([]); + } finally { + cg?.destroy(); + fs.rmSync(dir, { recursive: true, force: true }); + } + }); +}); diff --git a/src/resolution/name-matcher.ts b/src/resolution/name-matcher.ts index 6b582f6d9..0d4ef2066 100644 --- a/src/resolution/name-matcher.ts +++ b/src/resolution/name-matcher.ts @@ -17907,6 +17907,34 @@ export function tsImportedTypeFile(written: string, ref: UnresolvedRef, context: return file ?? undefined; } +function kotlinImportedType(name: string, ref: UnresolvedRef, context: ResolutionContext): string | undefined { + // ponytail: scan explicit imports here; cache per file only if indexing profiles justify it. + const source = context.readFile(ref.filePath); + if (!source) return undefined; + for (const match of source.matchAll(/^[ \t]*import[ \t]+([\w.]+)(?:[ \t]+as[ \t]+(\w+))?[ \t]*;?[ \t]*(?:\/\/[^\n]*)?$/gm)) { + const fqn = match[1]!; + if ((match[2] || fqn.split('.').pop()) !== name) continue; + const importedNode = context.getNodesByName(fqn.split('.').pop()!).find(n => + n.qualifiedName.replace(/::/g, '.') === fqn); + if (importedNode && !['class', 'interface', 'enum', 'type_alias'].includes(importedNode.kind)) return undefined; + return fqn; + } + return undefined; +} + +function kotlinExtensionInScope(target: Node, ref: UnresolvedRef, context: ResolutionContext): boolean { + if (target.filePath === ref.filePath) return true; + const declaration = context.readFile(target.filePath) ?? ''; + const caller = context.readFile(ref.filePath) ?? ''; + const targetPackage = /^\s*package\s+([\w.]+)/m.exec(declaration)?.[1] ?? ''; + const callerPackage = /^\s*package\s+([\w.]+)/m.exec(caller)?.[1] ?? ''; + if (targetPackage === callerPackage) return true; + for (const match of caller.matchAll(/^[ \t]*import[ \t]+([\w.]+(?:\.\*)?)(?:[ \t]+as[ \t]+\w+)?[ \t]*(?:\/\/[^\n]*)?$/gm)) { + if (match[1] === `${targetPackage}.${target.name}` || match[1] === `${targetPackage}.*`) return true; + } + return false; +} + /** * Java/Kotlin: infer a receiver's declared type by walking field declarations * in the class enclosing the call site. The field's `signature` is already in @@ -19789,6 +19817,44 @@ export function matchMethodCall( if (!literal) return null; } + // An explicit Kotlin type import fixes the receiver's identity. Resolve + // only that type (including Java interop), never a same-named local method. + const importedType = ref.language === 'kotlin' && dotMatch && /^[A-Z]\w*$/.test(objectOrClass!) + ? kotlinImportedType(objectOrClass!, ref, context) : undefined; + if (importedType) { + const localExtension = context.getNodesInFile(ref.filePath).find(n => + n.kind === 'method' && n.qualifiedName === `${objectOrClass}::${methodName}`); + if (localExtension) return { original: ref, targetNodeId: localExtension.id, + confidence: 0.9, resolvedBy: 'qualified-name' }; + const simpleName = importedType.split('.').pop()!; + const owners = context.getNodesByName(simpleName).filter(n => + (n.kind === 'class' || n.kind === 'interface' || n.kind === 'enum') && + (n.language === 'kotlin' || n.language === 'java') && + n.qualifiedName.replace(/::/g, '.') === importedType); + const methods = owners.flatMap(owner => context.getNodesInFile(owner.filePath).filter(n => + n.kind === 'method' && (n.language === 'kotlin' || n.language === 'java') && + (n.qualifiedName === `${owner.qualifiedName}::${methodName}` || + n.qualifiedName === `${owner.qualifiedName}::Companion::${methodName}`))); + if (owners.length === 1 && methods.length === 0) { + const constructor = context.getNodesInFile(owners[0]!.filePath).find(n => + n.kind === 'method' && + n.qualifiedName === `${owners[0]!.qualifiedName}::${methodName}::${methodName}`); + if (constructor) return { original: ref, targetNodeId: constructor.id, + confidence: 0.9, resolvedBy: 'qualified-name' }; + } + if (owners.length === 1 && methods.length > 0) return { original: ref, targetNodeId: methods[0]!.id, + confidence: 0.9, resolvedBy: 'qualified-name' }; + const extensions = context.getNodesByName(methodName!).filter(n => + n.kind === 'method' && n.language === 'kotlin' && + n.qualifiedName === `${objectOrClass}::${methodName}`); + if (extensions.length > 0) { + const visible = extensions.filter(n => kotlinExtensionInScope(n, ref, context)); + if (visible.length === 1) return { original: ref, targetNodeId: visible[0]!.id, + confidence: 0.9, resolvedBy: 'qualified-name' }; + return null; + } + if (owners.length === 0) return null; + } // A simple `receiver.method` / `receiver:method` / `receiver$method` shape whose // receiver type we can try to infer from its local declaration. // A python attribute receiver (`self.conn`, `cfg.client`) is not one: the @@ -27331,7 +27397,9 @@ function matchReferenceInner( // nested `ParameterizedTypesTest.Field`). if ((ref.language === 'java' || ref.language === 'kotlin') && ref.referenceKind !== 'imports' && isJavaOutsideImport(ref.referenceName.split('.')[0]!, ref, context)) { - return null; + // Kotlin can call an in-project extension on an imported library type. + return ref.language === 'kotlin' && ref.referenceKind === 'calls' + ? matchMethodCall(ref, context) : null; } // A symbolic name in a Scala type is a type (`F ~> G`) or a kind-projector @@ -27580,6 +27648,15 @@ function matchReferenceInner( // 2. Method call pattern result = nmTimed('methodCall', ref, () => matchMethodCall(ref, context)); if (result) return result; + if (ref.language === 'kotlin' && ref.referenceKind === 'calls') { + const importedReceiver = ref.referenceName.match(/^([A-Z]\w*)\.\w+$/)?.[1]; + if (importedReceiver) { + const importedType = kotlinImportedType(importedReceiver, ref, context); + if (importedType && !context.getNodesByName(importedType.split('.').pop()!).some(n => + (n.kind === 'class' || n.kind === 'interface' || n.kind === 'enum') && + n.qualifiedName.replace(/::/g, '.') === importedType)) return null; + } + } // 3. Exact name match result = nmTimed('exactName', ref, () => matchByExactName(ref, context));