Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(core): type a nested class's fields and ancestors from its own symbol (#2768)

Raghav Chamadiya8635cb29fc3a
diff --git a/packages/core/src/repowise/core/ingestion/call_receiver_typing.py b/packages/core/src/repowise/core/ingestion/call_receiver_typing.py
index 934f65e69..e0ba3437e 100644
--- a/packages/core/src/repowise/core/ingestion/call_receiver_typing.py
+++ b/packages/core/src/repowise/core/ingestion/call_receiver_typing.py
@@ -284,7 +284,7 @@ class ReceiverTypingMixin:
if receiver_name in body_types:
return body_types[receiver_name], "body"
if language in IMPLICIT_FIELD_LANGUAGES:
- class_id = _enclosing_id(caller_id)
+ class_id = self._caller_class_id(caller_id)
fields = self._field_types_in(file_path, language).get(class_id, {})
if receiver_name in fields:
return fields[receiver_name], "field"
@@ -373,7 +373,7 @@ class ReceiverTypingMixin:
) -> tuple[str, str, str] | None:
"""``(file, class id, tier)`` for a chain's head name."""
if head == _CHAIN_SELF[language]:
- class_id = _enclosing_id(caller_id)
+ class_id = self._caller_class_id(caller_id)
symbol = self._symbols_by_id.get(class_id)
if symbol is None or symbol.kind not in _TYPE_KINDS:
return None
@@ -422,6 +422,24 @@ class ReceiverTypingMixin:
ids = self._type_ids.get(file_path, {}).get(type_name, ())
return ids[0] if len(ids) == 1 else None
+ def _caller_class_id(self, caller_id: str) -> str:
+ """The id of the type whose method *caller_id* is.
+
+ A method id names only its own class (``path::Inner::m``) while a
+ nested class's id also names the outer one (``path::Outer::Inner``),
+ so the id prefix finds no nested class. The file's one type of that
+ name is the class; two of them leave the method id ambiguous, and a
+ method declared away from its type (a Go receiver, a C++ out-of-line
+ body) has none, so both keep the prefix.
+ """
+ caller = self._symbols_by_id.get(caller_id)
+ file_path = self._symbol_paths_by_id.get(caller_id)
+ if caller is not None and caller.parent_name and file_path is not None:
+ class_id = self._only_type_in(file_path, caller.parent_name)
+ if class_id is not None:
+ return class_id
+ return _enclosing_id(caller_id)
+
def _framework_receiver_type(
self,
file_path: str,
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index 1766f830c..1b7c99541 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -1364,9 +1364,9 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):
"""
if not self._heritage_parents:
return None
- class_id = _extract_class_id(caller_id)
- if class_id is None:
+ if _extract_class_id(caller_id) is None:
return None
+ class_id = self._caller_class_id(caller_id)
# The caller's own class answers even when Strategy 3 declined it for
# recursion; an ancestor's declaration of the name is not the target.
if self._declares(class_id, method_name) is not None:
diff --git a/tests/unit/ingestion/test_call_resolver_strategies.py b/tests/unit/ingestion/test_call_resolver_strategies.py
index bbe734f1b..2fb16985f 100644
--- a/tests/unit/ingestion/test_call_resolver_strategies.py
+++ b/tests/unit/ingestion/test_call_resolver_strategies.py
@@ -419,6 +419,101 @@ class TestFieldTypedReceiver:
)
assert not [e for e in _edges(parsed, tmp_path) if e[3].startswith("receiver_field_")]
+ def test_a_nested_class_field_types_its_receiver(self, tmp_path: Path) -> None:
+ """A nested class's id carries its outer class; its methods' ids do not."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "p/Token.java": (
+ "java",
+ "package p;\n"
+ "abstract class Token {\n"
+ " static final class Chars extends Token {\n"
+ " final TokenData data = new TokenData();\n"
+ " Chars data(String str) { data.set(str); return this; }\n"
+ " }\n"
+ "}\n",
+ ),
+ "p/TokenData.java": (
+ "java",
+ "package p;\nclass TokenData {\n void set(String str) { }\n}\n",
+ ),
+ "p/Tag.java": (
+ "java",
+ "package p;\npublic class Tag {\n public Tag set(int option) { return this; }\n}\n",
+ ),
+ },
+ )
+ assert (
+ "p/Token.java::Chars::data",
+ "p/TokenData.java::TokenData::set",
+ 0.90,
+ "receiver_field_same_package",
+ ) in _edges(parsed, tmp_path)
+
+ def test_two_nested_classes_sharing_a_name_type_no_field(self, tmp_path: Path) -> None:
+ """Both ``Holder.run`` bodies mint one id, so which class it is stays open."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "p/Pair.java": (
+ "java",
+ "package p;\n"
+ "class First {\n"
+ " static class Holder {\n"
+ " Finder f;\n"
+ " int run() { return f.find(); }\n"
+ " }\n"
+ "}\n"
+ "class Second {\n"
+ " static class Holder {\n"
+ " Other f;\n"
+ " int run() { return f.find(); }\n"
+ " }\n"
+ "}\n",
+ ),
+ "p/Finder.java": (
+ "java",
+ "package p;\nclass Finder {\n int find() { return 1; }\n}\n",
+ ),
+ "p/Other.java": (
+ "java",
+ "package p;\nclass Other {\n int find() { return 2; }\n}\n",
+ ),
+ },
+ )
+ assert not [e for e in _edges(parsed, tmp_path) if e[3].startswith("receiver_field_")]
+
+ def test_a_nested_class_reaches_an_inherited_method(self, tmp_path: Path) -> None:
+ """The hierarchy is keyed on the nested class's own id, outer name and all."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "Token.cs": (
+ "csharp",
+ "class Token\n{\n"
+ " class Chars : Base\n {\n"
+ " void Clear() { this.Reset(); }\n"
+ " }\n}\n",
+ ),
+ "Base.cs": ("csharp", "class Base\n{\n public void Reset() { }\n}\n"),
+ # A second declaration keeps the bare-name tiers from answering,
+ # so only the hierarchy can.
+ "Other.cs": ("csharp", "class Other\n{\n public void Reset() { }\n}\n"),
+ },
+ )
+ resolver = CallResolver(
+ parsed,
+ {p: set() for p in parsed},
+ repo_path=str(tmp_path),
+ heritage_parents={"Token.cs::Token::Chars": {"Base.cs::Base"}},
+ )
+ edges = [
+ (rc.caller_id, rc.callee_id, rc.origin)
+ for rc in resolver.resolve_file("Token.cs", parsed["Token.cs"].calls)
+ ]
+ assert ("Token.cs::Chars::Clear", "Base.cs::Base::Reset", "self_inherited") in edges
+
class TestPythonTypedReceiver:
"""Python reaches the same strategy through its own declaration shapes."""