MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / resolveMethodOnType

Function resolveMethodOnType

src/resolution/name-matcher.ts:536–637  ·  view source on GitHub ↗
(
  typeName: string,
  methodName: string,
  ref: UnresolvedRef,
  context: ResolutionContext,
  confidence: number,
  resolvedBy: ResolvedRef['resolvedBy'],
  /**
   * Optional FQN that identifies WHICH class declaration `typeName`
   * refers to in the caller's file. When multiple candidates share
   * the same qualifiedName (`FooConverter::convert` in both
   * `dao/converter/` and `service/converter/`), the FQN's
   * file-path-suffix picks the right one — the disambiguation
   * signal Java imports carry but the call site doesn't (#314).
   */
  preferredFqn?: string,
  /** Recursion guard for the supertype/conformance walk. */
  depth = 0,
)

Source from the content-addressed store, hash-verified

534// Exported for the precedence unit tests (#1079): they assert the
535// preferredFqn → same-file → matches[0] ordering directly.
536export function resolveMethodOnType(
537 typeName: string,
538 methodName: string,
539 ref: UnresolvedRef,
540 context: ResolutionContext,
541 confidence: number,
542 resolvedBy: ResolvedRef['resolvedBy'],
543 /**
544 * Optional FQN that identifies WHICH class declaration `typeName`
545 * refers to in the caller's file. When multiple candidates share
546 * the same qualifiedName (`FooConverter::convert` in both
547 * `dao/converter/` and `service/converter/`), the FQN's
548 * file-path-suffix picks the right one — the disambiguation
549 * signal Java imports carry but the call site doesn't (#314).
550 */
551 preferredFqn?: string,
552 /** Recursion guard for the supertype/conformance walk. */
553 depth = 0,
554): ResolvedRef | null {
555 // Look up methods by name and match by qualifiedName ending in
556 // `<typeName>::<methodName>`. This works whether the method is defined
557 // in-class (`class Foo { int bar() { ... } }`) or out-of-line in a separate
558 // file (`int Foo::bar() { ... }` in foo.cpp while class Foo is in foo.hpp).
559 // The previous same-file approach missed the latter — the typical C++ layout.
560 // Prefer the context's per-(type, method) memo: the raw name lookup fetches
561 // EVERY node sharing the method name — tens of thousands of rows for a
562 // collision-heavy Java name like `execute` — and re-filtering that per ref
563 // was a dominant term in the #1122 watchdog kill on large repos. Only the
564 // ref-independent filter is memoized; per-ref disambiguation stays below.
565 let matches: Node[];
566 if (context.getMethodMatches) {
567 matches = context.getMethodMatches(typeName, methodName, ref.language);
568 } else {
569 const methodCandidates = context.getNodesByName(methodName);
570 const want = `${typeName}::${methodName}`;
571 matches = [];
572 for (const m of methodCandidates) {
573 if (m.kind !== 'method') continue;
574 if (m.language !== ref.language) continue;
575 const qn = m.qualifiedName;
576 if (qn === want || qn.endsWith(`::${want}`)) {
577 matches.push(m);
578 }
579 }
580 }
581 if (matches.length === 0) {
582 // Conformance fallback: the method may be defined on a supertype `typeName`
583 // extends, or on a protocol / trait it conforms to (e.g. a Swift protocol-
584 // extension method, a C# default-interface or extension method, a Kotlin
585 // extension on a supertype). Walk supertypes transitively (depth-capped) via
586 // the resolved implements/extends edges — empty in the first resolution pass,
587 // populated in the conformance pass. Still VALIDATED (the method must exist on
588 // a supertype), so a wrong inference produces no edge.
589 if (depth < 4 && context.getSupertypes) {
590 const viaSupers = nmTimedT('rmot-supers', ref, (): ResolvedRef | null => {
591 for (const supertype of context.getSupertypes!(typeName, ref.language)) {
592 const via = resolveMethodOnType(
593 supertype, methodName, ref, context, confidence, resolvedBy, preferredFqn, depth + 1,

Callers 7

resolution.test.tsFile · 0.90
matchCppCallChainFunction · 0.85
matchScopedCallChainFunction · 0.85
matchDottedCallChainFunction · 0.85
matchMethodCallFunction · 0.85
matchGoFieldChainCallFunction · 0.85

Calls 4

nmTimedTFunction · 0.85
preferCallSiteFileFunction · 0.85
getMethodMatchesMethod · 0.80
getNodesByNameMethod · 0.65

Tested by

no test coverage detected