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(kotlin): recover top-level declarations with two annotations (#3839)
Amand593f099e973
diff --git a/packages/core/src/repowise/core/ingestion/parser/__init__.py b/packages/core/src/repowise/core/ingestion/parser/__init__.pyindex aa5023817..7e611f089 100644--- a/packages/core/src/repowise/core/ingestion/parser/__init__.py+++ b/packages/core/src/repowise/core/ingestion/parser/__init__.py@@ -48,7 +48,7 @@ from ..extractors.imports.ts_js import _cjs_imports, _is_dynamic_esm_importfrom ..extractors.visibility import ts_export_aliasesfrom ..language_configs import LANGUAGE_CONFIGS, LanguageConfigfrom ..models import FileInfo, Import, ParsedFile, Symbol, compute_content_hash-from ..parser_helpers import _run_query+from ..parser_helpers import _collect_error_nodes, _run_queryfrom ..python_local_refs import local_refs_from_tree, parse_python_modulefrom ..python_registrars import registering_functionsfrom ..sfc_source import component_call_sites, prepare_source@@ -78,6 +78,7 @@ from .grammars import (_get_language,_load_compiled_query,_parse_tree,+ _recover_kotlin_annotated_declarations,failed_query_languages,grammar_tag_for,log,@@ -112,6 +113,7 @@ from .symbol_rules import (_node_text,_refine_symbol_kind,_refine_visibility,+ _restore_kotlin_annotations,_rust_outer_attributes,_symbol_end_line,)@@ -194,6 +196,17 @@ class ASTParser:source = prepare_source(lang, source, path=file_info.path)root, parse_errors, grammar_tag, language = _parse_tree(lang, language, grammar_tag, source)++ recovered_annotations: dict[int, tuple[int, str]] = {}+ if lang == "kotlin":+ root, source, recovered_annotations = _recover_kotlin_annotated_declarations(+ language,+ root,+ source,+ )+ if recovered_annotations:+ parse_errors = _collect_error_nodes(root)+src = source.decode("utf-8", errors="replace")# Execute the compiled query ONCE per file; every pass below reads@@ -204,6 +217,7 @@ class ASTParser:symbols = _finalize_symbols(_extract_symbols(matches, config, file_info, src), root, src, file_info)+ _restore_kotlin_annotations(symbols, recovered_annotations)imports = _extract_imports(matches, config, file_info, src)calls = _extract_calls(matches, config, file_info, src, symbols)# An SFC's markup tags (``<Foo />``) are component calls the TS buffer lost.@@ -295,9 +309,7 @@ def _derive_exports(symbols: list[Symbol]) -> list[str]:_DEFAULT_PARSER: ASTParser | None = None-def _python_module_facts(- src: str, symbols: list[Symbol]-) -> tuple[frozenset[str], frozenset[str]]:+def _python_module_facts(src: str, symbols: list[Symbol]) -> tuple[frozenset[str], frozenset[str]]:"""``(local_refs, registrars)`` for a Python module, from one ``ast`` parse.See ``python_local_refs`` and ``python_registrars``.@@ -321,4 +333,3 @@ def parse_file(file_info: FileInfo, source: bytes) -> ParsedFile:if _DEFAULT_PARSER is None:_DEFAULT_PARSER = ASTParser()return _DEFAULT_PARSER.parse_file(file_info, source)-diff --git a/packages/core/src/repowise/core/ingestion/parser/grammars.py b/packages/core/src/repowise/core/ingestion/parser/grammars.pyindex a72a9a9a6..c9091abd3 100644--- a/packages/core/src/repowise/core/ingestion/parser/grammars.py+++ b/packages/core/src/repowise/core/ingestion/parser/grammars.py@@ -2,6 +2,7 @@from __future__ import annotations+import refrom collections.abc import Iterablefrom functools import cachefrom pathlib import Path@@ -73,7 +74,6 @@ def _load_compiled_query(lang: str, grammar_tag: str | None = None) -> object |return query-# Languages that intentionally have no AST parser. Derived from the# centralised LanguageRegistry — only non-code passthrough languages are# included (not the extra git-blame-only languages).@@ -267,3 +267,110 @@ def _parse_tree(grammar_tag = "tsx"language = tsx_languagereturn root, parse_errors, grammar_tag, language+++# Kotlin modifier words a declaration may carry between its annotations and its+# keyword. tree-sitter-kotlin also accepts most of them as identifiers, which is+# what lets ``private fun f() {}`` read as an infix call.+_KOTLIN_MODIFIER_WORDS = (+ "public|private|internal|protected|abstract|open|final|sealed|data|enum|annotation|"+ "inner|value|inline|noinline|crossinline|tailrec|operator|infix|external|suspend|"+ "override|lateinit|const|vararg|expect|actual|companion|fun"+)+_KOTLIN_DECLARATION_HEAD = re.compile(+ rb"(?:(?:" + _KOTLIN_MODIFIER_WORDS.encode() + rb")\s+)*"+ rb"(?:fun|class|interface|object|val|var)\b"+)+++def _kotlin_leading_annotations(top: Node) -> tuple[list[Node], Node | None]:+ """Return annotations leading a top-level expression and the node after them."""+ node = top+ if (+ node.type == "assignment"+ and node.children+ and node.children[0].type == "annotated_expression"+ ):+ node = node.children[0]++ annotations: list[Node] = []+ while node is not None and node.type == "annotated_expression":+ annotations.extend(c for c in node.children if c.type == "annotation")+ node = next(+ (+ c+ for c in node.children+ if c.is_named and c.type not in ("annotation", "line_comment", "block_comment")+ ),+ None,+ )++ return (annotations, node) if annotations and node is not None else ([], None)+++def _blank_kotlin_annotations(source: bytes, annotations: list[Node]) -> bytes:+ """Blank annotation bytes while preserving newlines and byte offsets."""+ blanked = bytearray(source)+ for node in annotations:+ for i in range(node.start_byte, node.end_byte):+ if blanked[i] not in (0x0A, 0x0D):+ blanked[i] = 0x20+ return bytes(blanked)+++def _recover_kotlin_annotated_declarations(+ language: Language,+ root: Node,+ source: bytes,+) -> tuple[Node, bytes, dict[int, tuple[int, str]]]:+ """Re-parse top-level Kotlin declarations hidden by multi-annotation expressions."""+ found: list[tuple[list[Node], Node]] = []++ for top in root.children:+ if top.type not in ("annotated_expression", "assignment"):+ continue++ annotations, rest = _kotlin_leading_annotations(top)+ if rest is None:+ continue++ head = source[rest.start_byte : rest.start_byte + 160]+ if _KOTLIN_DECLARATION_HEAD.match(head):+ found.append((annotations, rest))++ if not found:+ return root, source, {}++ blanked = _blank_kotlin_annotations(+ source,+ [annotation for annotations, _ in found for annotation in annotations],+ )+ new_root = Parser(language).parse(blanked).root_node++ # Keep the original parse if blanking makes it worse.+ if len(_collect_error_nodes(new_root)) > len(_collect_error_nodes(root)):+ return root, source, {}++ by_start = {child.start_byte: child for child in new_root.children}+ recovered: dict[int, tuple[int, str]] = {}++ for annotations, rest in found:+ first = min(annotations, key=lambda annotation: annotation.start_byte)+ end = max(annotation.end_byte for annotation in annotations)++ decl = by_start.get(rest.start_byte)+ modifiers = (+ next((child for child in decl.children if child.type == "modifiers"), None)+ if decl+ else None+ )+ if modifiers is not None:+ end = max(end, modifiers.end_byte)++ text = source[first.start_byte : end].decode("utf-8", errors="replace")+ recovered[rest.start_point[0] + 1] = (+ first.start_point[0] + 1,+ text,+ )++ return new_root, blanked, recovereddiff --git a/packages/core/src/repowise/core/ingestion/parser/symbol_rules.py b/packages/core/src/repowise/core/ingestion/parser/symbol_rules.pyindex 8e516fda2..926da41e3 100644--- a/packages/core/src/repowise/core/ingestion/parser/symbol_rules.py+++ b/packages/core/src/repowise/core/ingestion/parser/symbol_rules.py@@ -54,7 +54,9 @@ def _keep_ts_overloads_beside_a_body(symbols: list[Symbol], node_types: list[strThat is an ambient declaration (``declare function``, a ``.d.ts``), not anoverload, and it never was a symbol."""- bodied = {s.id for s, t in zip(symbols, node_types, strict=True) if t not in _TS_OVERLOAD_SIGNATURES}+ bodied = {+ s.id for s, t in zip(symbols, node_types, strict=True) if t not in _TS_OVERLOAD_SIGNATURES+ }return [sfor s, t in zip(symbols, node_types, strict=True)@@ -89,7 +91,6 @@ def _is_declaration(return _is_bodiless_cpp_type(language, node_type, def_node)-_FSHARP_BINDING_NODE_TYPES = ("function_declaration_left", "value_declaration_left")_DART_FUNCTION_NODE_TYPES = ("function_signature", "getter_signature", "setter_signature")@@ -277,6 +278,18 @@ def _is_split_annotation_chain(node: Node | None) -> bool:return False+def _restore_kotlin_annotations(+ symbols: list[Symbol],+ recovered: dict[int, tuple[int, str]],+) -> None:+ """Restore annotation lines and decorator text on recovered Kotlin symbols."""+ for symbol in symbols:+ entry = recovered.get(symbol.start_line)+ if entry is not None and symbol.parent_name is None:+ symbol.start_line, text = entry+ symbol.decorators = [text, *symbol.decorators]++def _declared_modifiers(def_node: Node, node_types: frozenset[str], src: str) -> tuple[str, ...]:"""Lowercased keyword modifiers written in *def_node*'s modifier children.diff --git a/packages/core/src/repowise/core/upgrade/version.py b/packages/core/src/repowise/core/upgrade/version.pyindex c03e3116e..cd6d15340 100644--- a/packages/core/src/repowise/core/upgrade/version.py+++ b/packages/core/src/repowise/core/upgrade/version.py@@ -99,6 +99,9 @@ STORE_FORMAT_VERSION: int = 2#: DefaultRepository``), so a call through the alias reaches the repo class.#: A top-level Kotlin declaration keeps the annotations the grammar splits off#: it when the last one takes one argument (``@Suppress("unused")``).+#: A top-level Kotlin declaration with two annotations (``@Preview @Composable+#: private fun f()``) that the grammar read as an expression is re-parsed with its+#: leading annotations blanked, so it becomes a symbol again.PARSER_SCHEMA_VERSION: int = 8#: state.json key holding the store format version that wrote the store.diff --git a/tests/unit/ingestion/test_kotlin_extractors.py b/tests/unit/ingestion/test_kotlin_extractors.pyindex 7823629d9..39d747106 100644--- a/tests/unit/ingestion/test_kotlin_extractors.py+++ b/tests/unit/ingestion/test_kotlin_extractors.py@@ -175,3 +175,113 @@ class TestKotlinDetachedAnnotation:def test_file_annotation_is_not_taken(self, parser: ASTParser) -> None:src = b'@file:Suppress("unused")\npackage x\n\nfun a() {}\n\nfun z() {}\n'assert self._decorators(parser, src)["a"] == []+++class TestKotlinMultiAnnotationDeclarations:+ """A top-level declaration with two annotations can parse as an expression."""++ _SCREEN = b"""\+package app++@Preview+@Composable+private fun TwoAnnotationsPreview() {+ helper()+}++@Composable+private fun OneAnnotation() {+ helper()+}++@Preview @Composable private fun SameLine() {+ helper()+}++@Target(AnnotationTarget.FUNCTION)+@Retention(AnnotationRetention.BINARY)+annotation class ThemePreviews++@RunWith(JUnit4::class)+@Config(sdk = [35])+class ScreenTest {+ fun check() = helper()+}++@A @B private val p = 1++@A @B private object O {}++fun helper() {}+"""++ def test_two_annotation_declarations_become_symbols(self, parser: ASTParser) -> None:+ result = parser.parse_file(_file("app/Screen.kt"), self._SCREEN)+ by_name = {s.name: s for s in result.symbols}+ for name in ("TwoAnnotationsPreview", "SameLine", "ThemePreviews", "ScreenTest", "p", "O"):+ assert name in by_name, name+ assert by_name["check"].parent_name == "ScreenTest"++ def test_recovered_symbol_starts_at_first_annotation(self, parser: ASTParser) -> None:+ result = parser.parse_file(_file("app/Screen.kt"), self._SCREEN)+ by_name = {s.name: s for s in result.symbols}+ assert by_name["TwoAnnotationsPreview"].start_line == 3+ assert by_name["SameLine"].start_line == 14+ assert by_name["ThemePreviews"].start_line == 18+ assert by_name["ScreenTest"].start_line == 22++ def test_recovered_symbol_carries_both_annotations(self, parser: ASTParser) -> None:+ result = parser.parse_file(_file("app/Screen.kt"), self._SCREEN)+ by_name = {s.name: s for s in result.symbols}+ text = " ".join(by_name["TwoAnnotationsPreview"].decorators)+ assert "@Preview" in text and "@Composable" in text+ text = " ".join(by_name["ScreenTest"].decorators)+ assert "@RunWith(JUnit4::class)" in text and "@Config(sdk = [35])" in text+ assert by_name["TwoAnnotationsPreview"].visibility == "private"++ def test_calls_attributed_to_recovered_symbol_not_module(self, parser: ASTParser) -> None:+ result = parser.parse_file(_file("app/Screen.kt"), self._SCREEN)+ callers = {c.caller_symbol_id for c in result.calls if c.target_name == "helper"}+ assert "app/Screen.kt::TwoAnnotationsPreview" in callers+ assert "app/Screen.kt::SameLine" in callers+ assert "app/Screen.kt::__module__" not in callers++ def test_shape_as_last_declaration_parses_as_today(self, parser: ASTParser) -> None:+ src = b"package a\n\nfun helper() {}\n\n@Preview\n@Composable\nprivate fun Last() {}\n"+ names = {s.name for s in parser.parse_file(_file(), src).symbols}+ assert {"helper", "Last"} <= names++ def test_shapes_that_already_parse_are_unchanged(self, parser: ASTParser) -> None:+ src = b"""\+package a++class Host {+ @A+ @B+ private fun member() {}+}++@A @B private fun withParam(x: Int) {}++@A @B suspend fun susp() {}++fun after() {}+"""+ result = parser.parse_file(_file(), src)+ by_name = {s.name: s for s in result.symbols}+ assert {"Host", "member", "withParam", "susp", "after"} <= set(by_name)+ assert by_name["member"].parent_name == "Host"+ assert by_name["withParam"].start_line == 9++ def test_real_expression_in_body_is_not_rewritten(self, parser: ASTParser) -> None:+ src = b"""\+package a++fun outer() {+ @Suppress("x") foo()+}++fun after() {}+"""+ result = parser.parse_file(_file(), src)+ assert {s.name for s in result.symbols} == {"outer", "after"}