Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(graph): bind Kotlin alias imports by alias and drop the fake wildcard module (#3607)
Swati Ahuja0e6e53957dec
diff --git a/packages/core/src/repowise/core/ingestion/extractors/bindings/kotlin.py b/packages/core/src/repowise/core/ingestion/extractors/bindings/kotlin.pyindex 096a59cbe..e9e4228b0 100644--- a/packages/core/src/repowise/core/ingestion/extractors/bindings/kotlin.py+++ b/packages/core/src/repowise/core/ingestion/extractors/bindings/kotlin.py@@ -9,13 +9,32 @@ from ..helpers import node_textdef extract_kotlin_bindings(stmt_node: Node, src: str) -> tuple[list[str], list[NamedBinding]]:- """Extract bindings from Kotlin import declarations."""+ """Extract bindings from Kotlin import declarations.++ The grammar keeps ``.*`` and ``as Alias`` outside the+ ``qualified_identifier``: ``import a.b.*`` names the package ``a.b``, and+ ``import a.b.Foo as Bar`` puts ``Bar`` in a sibling ``identifier``. A+ wildcard binds no name of its own; an alias binds ``Bar`` to ``Foo``.++ Kotlin allows no newline between ``as`` and the alias, but the grammar+ recovers a dangling ``import a.b.Foo as`` by taking the next line's first+ word, so an alias on a later line is not one. Backticks stay on the alias:+ symbol names and call targets keep theirs too, so ``as `in``` matches the+ call ```in`()``.+ """+ exported = alias = None+ as_row = Nonefor child in stmt_node.children:+ if child.type == "*":+ return ["*"], [NamedBinding(local_name="*", exported_name=None, source_file=None)]if child.type == "qualified_identifier":- full = node_text(child, src)- parts = full.split(".")- local = parts[-1]- if local == "*":- return ["*"], [NamedBinding(local_name="*", exported_name=None, source_file=None)]- return [local], [NamedBinding(local_name=local, exported_name=local, source_file=None)]- return [], []+ exported = node_text(child, src).rpartition(".")[2]+ elif child.type == "as":+ as_row = child.end_point[0]+ elif child.type == "identifier" and exported and child.start_point[0] == as_row:+ alias = node_text(child, src)+ if not exported:+ return [], []+ return [exported], [+ NamedBinding(local_name=alias or exported, exported_name=exported, source_file=None)+ ]diff --git a/tests/unit/ingestion/test_jvm_calls.py b/tests/unit/ingestion/test_jvm_calls.pyindex b991c78ed..ebc27403d 100644--- a/tests/unit/ingestion/test_jvm_calls.py+++ b/tests/unit/ingestion/test_jvm_calls.py@@ -151,3 +151,81 @@ class TestJavaMethodReference:"C.java::C::m" in u and "Foo.java::Foo::bar" in vfor u, v, _ in edges), edges+++class TestKotlinImportBindings:+ """Kotlin ``as`` aliases and ``.*`` wildcards, through the call resolver."""++ def _parse_with_imports(+ self, tmp_path: Path, files: dict[str, tuple[str, str]], resolved: dict[str, str]+ ) -> dict[str, ParsedFile]:+ # Stand in for the graph builder: pin each import to its target file.+ parsed = _parse_all(tmp_path, files)+ for pf in parsed.values():+ for imp in pf.imports:+ imp.resolved_file = resolved.get(imp.module_path)+ return parsed++ def test_aliased_call_resolves_to_original_symbol(self, tmp_path: Path) -> None:+ caller = "src/main/kotlin/com/app/App.kt"+ lib = "src/main/kotlin/com/lib/Greeting.kt"+ files = {+ caller: (+ "kotlin",+ "package com.app\n"+ "import com.lib.greet as hello\n"+ "fun run() { hello() }\n",+ ),+ lib: ("kotlin", "package com.lib\nfun greet(): Int = 1\n"),+ }+ parsed = self._parse_with_imports(tmp_path, files, {"com.lib.greet": lib})+ edges = _resolve_calls(parsed, tmp_path, {caller: {lib}, lib: set()})+ assert any(+ u.endswith("App.kt::run") and v.endswith("Greeting.kt::greet") for u, v, _ in edges+ ), edges++ def test_backticked_keyword_alias_resolves(self, tmp_path: Path) -> None:+ # Call targets keep their backticks, so the alias must keep them too.+ caller = "src/main/kotlin/com/app/App.kt"+ lib = "src/main/kotlin/com/lib/Greeting.kt"+ files = {+ caller: (+ "kotlin",+ "package com.app\n"+ "import com.lib.greet as `in`\n"+ "fun run() { `in`() }\n",+ ),+ lib: ("kotlin", "package com.lib\nfun greet(): Int = 1\n"),+ }+ parsed = self._parse_with_imports(tmp_path, files, {"com.lib.greet": lib})+ edges = _resolve_calls(parsed, tmp_path, {caller: {lib}, lib: set()})+ assert any(+ u.endswith("App.kt::run") and v.endswith("Greeting.kt::greet") for u, v, _ in edges+ ), edges++ def test_wildcard_import_binds_no_package_name(self, tmp_path: Path) -> None:+ caller = "src/main/kotlin/com/app/App.kt"+ helper = "src/main/kotlin/com/lib/routing/Helper.kt"+ routing = "src/main/kotlin/com/lib/routing/Routing.kt"+ files = {+ caller: (+ "kotlin",+ "package com.app\n"+ "import com.lib.routing.*\n"+ "fun run() { routing() }\n",+ ),+ helper: ("kotlin", "package com.lib.routing\nfun helper(): Int = 1\n"),+ routing: ("kotlin", "package com.lib.routing\nfun routing(): Int = 1\n"),+ }+ # The wildcard resolves to one representative file of the package,+ # which is not the file declaring ``routing``.+ parsed = self._parse_with_imports(tmp_path, files, {"com.lib.routing.*": helper})+ (imp,) = parsed[caller].imports+ assert imp.imported_names == ["*"]+ assert [b.local_name for b in imp.bindings] == ["*"]+ edges = _resolve_calls(+ parsed, tmp_path, {caller: {helper, routing}, helper: set(), routing: set()}+ )+ assert any(+ u.endswith("App.kt::run") and v.endswith("Routing.kt::routing") for u, v, _ in edges+ ), edgesdiff --git a/tests/unit/ingestion/test_kotlin_extractors.py b/tests/unit/ingestion/test_kotlin_extractors.pyindex f40e2d17d..badbcbfb1 100644--- a/tests/unit/ingestion/test_kotlin_extractors.py+++ b/tests/unit/ingestion/test_kotlin_extractors.py@@ -106,3 +106,29 @@ class TestKotlinWildcardImports:result = parser.parse_file(_file(), src)modules = [imp.module_path for imp in result.imports]assert modules == ["com.example.Foo"]++ def test_wildcard_import_binds_only_the_wildcard(self, parser: ASTParser) -> None:+ src = b"package x\nimport io.ktor.server.routing.*\nclass App\n"+ (imp,) = parser.parse_file(_file(), src).imports+ assert imp.imported_names == ["*"]+ assert [(b.local_name, b.exported_name) for b in imp.bindings] == [("*", None)]+++class TestKotlinAliasImports:+ def test_alias_binds_alias_to_original_name(self, parser: ASTParser) -> None:+ src = b"package x\nimport com.example.Foo as Bar\nclass App\n"+ (imp,) = parser.parse_file(_file(), src).imports+ assert imp.module_path == "com.example.Foo"+ assert imp.imported_names == ["Foo"]+ assert [(b.local_name, b.exported_name) for b in imp.bindings] == [("Bar", "Foo")]+ assert imp.local_names == ["Bar"]++ def test_plain_import_binds_its_own_name(self, parser: ASTParser) -> None:+ src = b"package x\nimport com.example.Foo\nclass App\n"+ (imp,) = parser.parse_file(_file(), src).imports+ assert [(b.local_name, b.exported_name) for b in imp.bindings] == [("Foo", "Foo")]++ def test_dangling_as_takes_no_alias_from_the_next_line(self, parser: ASTParser) -> None:+ src = b"package x\nimport com.example.Foo as\nclass App\n"+ imp = parser.parse_file(_file(), src).imports[0]+ assert [(b.local_name, b.exported_name) for b in imp.bindings] == [("Foo", "Foo")]