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(graph): give Kotlin data class and enum synthetic methods a readable return type (#4841)

Swati Ahuja34e92457d036
diff --git a/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/kotlin_jvm.py b/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/kotlin_jvm.py
index 70123fa6e..1d0b10f2b 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/kotlin_jvm.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/kotlin_jvm.py
@@ -100,29 +100,36 @@ def kotlin_synthetic_symbols(
continue
line = node.start_point[0] + 1
+ # Signatures take the stored ``function name(params) -> Type``
+ # shape of a parsed Kotlin function, so a chain reads the return.
if _is_data_class(node):
components = _primary_components(node, src)
for i, (_cname, ctype) in enumerate(components, start=1):
out.append(build_synthetic_symbol(
name=f"component{i}", kind="method",
- signature=f"public {ctype or 'Any'} component{i}()",
+ signature=f"function component{i}() -> {ctype or 'Any'}",
start_line=line, end_line=line,
file_info=file_info, parent_name=class_name,
))
- # copy(...) — same params as primary ctor.
+ # copy(...): the primary ctor's params. Their implicit defaults
+ # are left out: a bare ``copy(..)`` in a receiver lambda would
+ # then reach any imported data class's copy by name.
copy_params = ", ".join(
f"{n}: {t or 'Any'}" for n, t in components
)
out.append(build_synthetic_symbol(
name="copy", kind="method",
- signature=f"public {class_name} copy({copy_params})",
+ signature=f"function copy({copy_params}) -> {class_name}",
start_line=line, end_line=line,
file_info=file_info, parent_name=class_name,
))
- for nm in ("equals", "hashCode", "toString"):
+ for nm, sig in (
+ ("equals", "function equals(other: Any?) -> Boolean"),
+ ("hashCode", "function hashCode() -> Int"),
+ ("toString", "function toString() -> String"),
+ ):
out.append(build_synthetic_symbol(
- name=nm, kind="method",
- signature=f"public {nm}()",
+ name=nm, kind="method", signature=sig,
start_line=line, end_line=line,
file_info=file_info, parent_name=class_name,
))
@@ -130,13 +137,13 @@ def kotlin_synthetic_symbols(
if _is_enum_class(node):
out.append(build_synthetic_symbol(
name="values", kind="method",
- signature=f"public static {class_name}[] values()",
+ signature=f"function values() -> Array<{class_name}>",
start_line=line, end_line=line,
file_info=file_info, parent_name=class_name,
))
out.append(build_synthetic_symbol(
name="valueOf", kind="method",
- signature=f"public static {class_name} valueOf(String)",
+ signature=f"function valueOf(value: String) -> {class_name}",
start_line=line, end_line=line,
file_info=file_info, parent_name=class_name,
))
diff --git a/tests/unit/ingestion/test_jvm_synthesis.py b/tests/unit/ingestion/test_jvm_synthesis.py
index 0540893aa..50b92e4e1 100644
--- a/tests/unit/ingestion/test_jvm_synthesis.py
+++ b/tests/unit/ingestion/test_jvm_synthesis.py
@@ -6,6 +6,7 @@ from __future__ import annotations
from datetime import datetime
from pathlib import Path
+from repowise.core.ingestion.call_resolver import CallResolver
from repowise.core.ingestion.models import FileInfo
from repowise.core.ingestion.parser import parse_file
@@ -83,6 +84,43 @@ class TestKotlinDataClass:
)
assert "INSTANCE" in names, names
+ def test_synthetic_signatures_declare_their_return(self, tmp_path: Path) -> None:
+ src = "data class P(val x: Int, val n: Norm?)\nenum class Color { RED }\n"
+ f = tmp_path / "P.kt"
+ f.write_text(src)
+ pf = parse_file(_fi("P.kt", f, "kotlin"), src.encode("utf-8"))
+ sigs = {s.name: s.signature for s in pf.symbols}
+ assert sigs["component2"] == "function component2() -> Norm?"
+ assert sigs["copy"] == "function copy(x: Int, n: Norm?) -> P"
+ assert sigs["equals"] == "function equals(other: Any?) -> Boolean"
+ assert sigs["valueOf"] == "function valueOf(value: String) -> Color"
+
+ def test_a_chain_through_copy_or_componentN_resolves(self, tmp_path: Path) -> None:
+ src = (
+ "package app\n\n"
+ "data class Point(val x: Int, val norm: Norm) {\n"
+ " fun len(): Int = x\n"
+ "}\n\n"
+ "class Norm {\n"
+ " fun size(): Int = 1\n"
+ "}\n\n"
+ "fun use(point: Point) {\n"
+ " point.copy(x = 1, norm = Norm()).len()\n"
+ " point.component2().size()\n"
+ "}\n"
+ )
+ f = tmp_path / "App.kt"
+ f.write_text(src)
+ parsed = {"App.kt": parse_file(_fi("App.kt", f, "kotlin"), src.encode("utf-8"))}
+ resolver = CallResolver(parsed, {"App.kt": set()}, repo_path=str(tmp_path))
+ callees = {rc.callee_id for rc in resolver.resolve_file("App.kt", parsed["App.kt"].calls)}
+ assert {
+ "App.kt::Point::copy",
+ "App.kt::Point::len",
+ "App.kt::Point::component2",
+ "App.kt::Norm::size",
+ } <= callees
+
def test_plain_class_no_componentN(self, tmp_path: Path) -> None:
names = _names(
tmp_path, "C.kt", "kotlin",