fix(kotlin): resolve chained companion-factory calls Foo.getInstance().bar() (#750) (#752)

A Kotlin method called through a companion-object factory, fluent chain, or
constructor — `Foo.getInstance().bar()`, `Config.create(opts).build()`,
`STMTransaction(f).commit()` — dropped the receiver to a BARE method name, which
then name-matched a same-named method on an unrelated class (a wrong edge) or
failed to resolve. Ports the #645/#608 mechanism to Kotlin:

- Part 1: capture Kotlin return types in the extractor. tree-sitter-kotlin
  exposes no field names, so the return type is read positionally (the type node
  after function_value_parameters); inferred/Unit/Nothing returns yield none.
- Part 2: encode a CLASS/companion-factory call-receiver chain as `inner().method`.
  Gated to a capitalized receiver (`Foo.getInstance()` / `Foo(args)`) so instance
  chains (`list.filter{}.map{}`) keep their bare-name behavior — re-encoding those
  would only drop the edge, regressing recall in fluent codebases.
- Part 3: generalize matchJavaCallChain -> matchDottedCallChain (shared by the JVM
  dot-notation languages); resolve the method on the factory's return type, or on
  the constructed class for a Kotlin `Foo(args).method()` receiver. Validated via
  resolveMethodOnType, so a wrong inference yields NO edge.

Validated: synthetic decoy + args + absent-method safety tests; full suite green;
real-repo A/B on arrow-kt/arrow (734 .kt) — node count identical (no explosion),
+49 validated-correct chained edges, and the removed edges are wrong bare-name
guesses the fix correctly stops emitting (419/438 from test/doc files; the 18
from product code are stdlib `.apply{}`, self-loops, and bare-name mismatches) —
a net precision improvement, ~0 correct product edges lost. Java path unchanged
(constructor branch is Kotlin-gated). EXTRACTION_VERSION 6 -> 7.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-06-09 00:12:37 -04:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 7f6bdf7ad1
commit 3e04650850
6 changed files with 187 additions and 29 deletions
+32 -13
View File
@@ -2525,22 +2525,41 @@ export class TreeSitterExtractor {
calleeName = methodName;
}
} else if (
(this.language === 'cpp' || this.language === 'c') &&
(this.language === 'cpp' || this.language === 'c' || this.language === 'kotlin') &&
receiver &&
receiver.type === 'call_expression'
) {
// C/C++ receiver that is itself a call — `Foo::instance().bar()`,
// `openSession()->run()`, `mgr.view().render()`. Keep the inner
// call so resolution can infer bar()'s class from what the inner
// call RETURNS (#645). Encode as `<innerCallee>().<method>`; the
// `().` marker never appears in an ordinary ref, so the C++
// resolver can detect and split it. Other languages keep the
// bare-name behavior (dropping the receiver) below.
const innerFn = getChildByField(receiver, 'function');
const innerCallee = innerFn
? getNodeText(innerFn, this.source).replace(/->/g, '.').replace(/\s+/g, '')
: '';
calleeName = innerCallee ? `${innerCallee}().${methodName}` : methodName;
// Receiver that is itself a call — `Foo::instance().bar()`,
// `openSession()->run()`, `mgr.view().render()` (C/C++), or
// `Foo.getInstance().bar()` (Kotlin). Keep the inner call so
// resolution can infer bar()'s class from what the inner call
// RETURNS (#645/#608). Encode as `<innerCallee>().<method>`; the
// `().` marker never appears in an ordinary ref, so the resolver
// can detect and split it. Other languages keep the bare-name
// behavior (dropping the receiver) below.
let innerCallee: string;
let reencode: boolean;
if (this.language === 'kotlin') {
// tree-sitter-kotlin has no field names — the inner callee is the
// call_expression's first named child (a navigation_expression
// `Foo.getInstance`, or a bare identifier for a free call).
const innerNav = receiver.namedChild(0);
innerCallee = innerNav ? getNodeText(innerNav, this.source).replace(/\s+/g, '') : '';
// Only re-encode a CLASS / companion-factory chain, whose receiver
// chain starts with a capitalized type (`Foo.getInstance().bar()`).
// An instance chain (`list.filter{}.map{}`) has a lowercase receiver
// whose type we can't recover here — re-encoding it would only drop
// the edge (no chain resolution, no bare-name fallback), regressing
// recall in fluent codebases. Leave those to the bare-name path.
reencode = /^[A-Z]/.test(innerCallee);
} else {
const innerFn = getChildByField(receiver, 'function');
innerCallee = innerFn
? getNodeText(innerFn, this.source).replace(/->/g, '.').replace(/\s+/g, '')
: '';
reencode = !!innerCallee;
}
calleeName = reencode ? `${innerCallee}().${methodName}` : methodName;
} else {
calleeName = methodName;
}