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(ingestion): record Kotlin calls whose only argument is a trailing lambda (#3783)

Yunus Ozen26eaa0ea97cb
diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/kotlin.py b/packages/core/src/repowise/core/ingestion/languages/specs/kotlin.py
index aefab32ec..41d257d6e 100644
--- a/packages/core/src/repowise/core/ingestion/languages/specs/kotlin.py
+++ b/packages/core/src/repowise/core/ingestion/languages/specs/kotlin.py
@@ -41,6 +41,12 @@ SPEC = LanguageSpec(
"also",
"apply",
"with",
+ "repeat",
+ "synchronized",
+ "buildList",
+ "buildString",
+ "buildMap",
+ "takeIf",
}
),
builtin_parents=frozenset(
diff --git a/packages/core/src/repowise/core/ingestion/parser_helpers.py b/packages/core/src/repowise/core/ingestion/parser_helpers.py
index 7baf309cb..ba5ef444c 100644
--- a/packages/core/src/repowise/core/ingestion/parser_helpers.py
+++ b/packages/core/src/repowise/core/ingestion/parser_helpers.py
@@ -344,6 +344,9 @@ def _count_arguments(arg_node: Node) -> int:
ways (``comment``, ``line_comment``, ``block_comment``), hence the
substring test rather than a fixed set.
"""
+ if arg_node.type == "annotated_lambda":
+ # Kotlin ``foo { .. }``: the lambda is the only argument.
+ return 1
skip_types = frozenset({"(", ")", ",", "[", "]"})
listed = sum(
1
diff --git a/packages/core/src/repowise/core/ingestion/queries/kotlin.scm b/packages/core/src/repowise/core/ingestion/queries/kotlin.scm
index d819d4e09..64a776cdb 100644
--- a/packages/core/src/repowise/core/ingestion/queries/kotlin.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/kotlin.scm
@@ -57,10 +57,14 @@
; Calls
; ---------------------------------------------------------------------------
-; Simple call: foo(args)
+; Simple call: foo(args), or foo { ... } whose only argument is a trailing
+; lambda: the grammar gives that form no ``value_arguments`` at all.
(call_expression
(identifier) @call.target
- (value_arguments) @call.arguments
+ [
+ (value_arguments)
+ (annotated_lambda)
+ ] @call.arguments
) @call.site
; Member call: obj.method(args) — and self-dispatch: this.method(args).
@@ -76,7 +80,10 @@
]
(identifier) @call.target
)
- (value_arguments) @call.arguments
+ [
+ (value_arguments)
+ (annotated_lambda)
+ ] @call.arguments
) @call.site
; ---------------------------------------------------------------------------
diff --git a/packages/core/src/repowise/core/upgrade/version.py b/packages/core/src/repowise/core/upgrade/version.py
index 38b6029d4..ce91ad826 100644
--- a/packages/core/src/repowise/core/upgrade/version.py
+++ b/packages/core/src/repowise/core/upgrade/version.py
@@ -93,6 +93,8 @@ STORE_FORMAT_VERSION: int = 2
#: the Java way, an extension's receiver leading its types
#: (``toHexString#0(Byte)``, ``g#0(Int?)``), and a Kotlin trailing lambda counts as an
#: argument.
+#: A Kotlin call whose only argument is a trailing lambda (``AppTheme { }``,
+#: ``s.forEachSegment { }``) is a call site, with one argument.
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_trailing_lambda_calls.py b/tests/unit/ingestion/test_kotlin_trailing_lambda_calls.py
new file mode 100644
index 000000000..24c240f3c
--- /dev/null
+++ b/tests/unit/ingestion/test_kotlin_trailing_lambda_calls.py
@@ -0,0 +1,127 @@
+"""A Kotlin call whose only argument is a trailing lambda is a call site (#3762).
+
+``AppTheme { ... }`` has no parentheses, so the grammar gives it no
+``value_arguments`` and the call patterns used to skip it. It now reaches its
+callee with one argument, like ``AppTheme({ ... })``, and the stdlib and
+function-typed refusals still hold.
+"""
+
+from __future__ import annotations
+
+from datetime import datetime
+from pathlib import Path
+
+import pytest
+
+from repowise.core.ingestion.call_resolver import CallResolver
+from repowise.core.ingestion.models import FileInfo
+from repowise.core.ingestion.parser import parse_file
+
+SOURCE = """\
+package app
+
+fun AppTheme(content: () -> Unit) = content()
+
+fun h2(a: Int, b: Int) {}
+
+class Segments {
+ fun forEachSegment(action: (Int) -> Unit) {}
+}
+
+class Runner {
+ fun run(block: () -> Unit) {}
+
+ fun scoped() {
+ run { println("x") }
+ }
+}
+
+fun withParentheses() {
+ AppTheme({ println("x") })
+}
+
+fun lambdaOnly() {
+ AppTheme { println("x") }
+}
+
+fun memberWithParentheses(segments: Segments) {
+ segments.forEachSegment({ })
+}
+
+fun memberLambdaOnly(segments: Segments) {
+ segments.forEachSegment { }
+}
+
+fun wrongArity() {
+ h2 { }
+}
+
+fun callback(onClick: (() -> Unit) -> Unit) {
+ onClick { }
+}
+
+fun counts(a: Int) {
+ foo { }
+ foo(a) { }
+ foo(a)
+}
+"""
+
+
+def parsed(tmp_path: Path):
+ path = tmp_path / "Use.kt"
+ path.write_text(SOURCE, encoding="utf-8")
+ info = FileInfo(
+ path="Use.kt",
+ abs_path=str(path),
+ language="kotlin",
+ size_bytes=len(SOURCE),
+ git_hash="",
+ last_modified=datetime.now(),
+ is_test=False,
+ is_config=False,
+ is_api_contract=False,
+ is_entry_point=False,
+ )
+ return {"Use.kt": parse_file(info, SOURCE.encode("utf-8"))}
+
+
+def _callees(parsed, tmp_path: Path, caller: str) -> set[str]:
+ resolver = CallResolver(parsed, {"Use.kt": set()}, repo_path=str(tmp_path))
+ return {
+ rc.callee_id
+ for rc in resolver.resolve_file("Use.kt", parsed["Use.kt"].calls)
+ if rc.caller_id == f"Use.kt::{caller.replace('.', '::')}"
+ }
+
+
[email protected]("caller", ["withParentheses", "lambdaOnly"])
+def test_a_lambda_only_call_reaches_its_function(parsed, tmp_path: Path, caller: str) -> None:
+ assert _callees(parsed, tmp_path, caller) == {"Use.kt::AppTheme"}
+
+
[email protected]("caller", ["memberWithParentheses", "memberLambdaOnly"])
+def test_a_lambda_only_member_call_reaches_its_method(parsed, tmp_path: Path, caller: str) -> None:
+ assert _callees(parsed, tmp_path, caller) == {"Use.kt::Segments::forEachSegment"}
+
+
+def test_a_stdlib_scope_function_stays_unresolved(parsed, tmp_path: Path) -> None:
+ assert _callees(parsed, tmp_path, "Runner.scoped") == set()
+
+
+def test_a_function_that_cannot_take_one_argument_is_not_called(parsed, tmp_path: Path) -> None:
+ assert "Use.kt::h2" not in _callees(parsed, tmp_path, "wrongArity")
+
+
+def test_a_function_typed_value_has_no_repo_callee(parsed, tmp_path: Path) -> None:
+ assert _callees(parsed, tmp_path, "callback") == set()
+
+
+def test_the_trailing_lambda_counts_as_an_argument(parsed) -> None:
+ counts = sorted(
+ (c.line, c.argument_count) for c in parsed["Use.kt"].calls if c.target_name == "foo"
+ )
+ lines = SOURCE.splitlines()
+ by_text = {lines[line - 1].strip(): count for line, count in counts}
+ assert by_text == {"foo { }": 1, "foo(a) { }": 2, "foo(a)": 1}