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): keep each call on a shared line as its own call site (#4838)
Swati Ahuja85f3823d325a
diff --git a/packages/core/src/repowise/core/ingestion/call_receiver_typing/__init__.py b/packages/core/src/repowise/core/ingestion/call_receiver_typing/__init__.pyindex 41b56056a..418450256 100644--- a/packages/core/src/repowise/core/ingestion/call_receiver_typing/__init__.py+++ b/packages/core/src/repowise/core/ingestion/call_receiver_typing/__init__.py@@ -1626,11 +1626,12 @@ class ReceiverTypingMixin(sites = self._call_sites.get(file_path)if sites is None:parsed = self._parsed_files.get(file_path)- sites = {- (c.line, c.receiver_name, c.target_name): c- for c in (parsed.calls if parsed else ())- if c.caller_symbol_id and c.receiver_call is None- }+ sites = {}+ # The first of same-named calls on a line is the outer one, the+ # call an assignment holds: ``x = f(a, f(b))``.+ for c in parsed.calls if parsed else ():+ if c.caller_symbol_id and c.receiver_call is None:+ sites.setdefault((c.line, c.receiver_name, c.target_name), c)_store_capped(self._call_sites, file_path, sites, _SOURCE_CACHE_FILES)return sitesdiff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.pyindex 55439529d..8d27cb1d3 100644--- a/packages/core/src/repowise/core/ingestion/call_resolver.py+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py@@ -1480,18 +1480,18 @@ class CallResolver(as its own site on the chain's first line, so the hop below is readoff the site *inner* names. Apex, C#, Dart, Go, Java and Kotlin read it, so abuilder chain types each link from the one before; elsewhere an inner call- is still typed as if nothing were chained under it.+ is still typed as if nothing were chained under it. A name repeated in+ one chain (``a().b().b()``) keeps its first link, the innermost."""if language not in ("apex", "csharp", "dart", "go", "java", "kotlin"):return Nonesites = self._chained_sites.get(file_path)if sites is None:parsed = self._parsed_files.get(file_path)- sites = {- (c.line, c.receiver_name, c.target_name): c.receiver_call- for c in (parsed.calls if parsed else ())- if c.receiver_call is not None- }+ sites = {}+ for c in parsed.calls if parsed else ():+ if c.receiver_call is not None:+ sites.setdefault((c.line, c.receiver_name, c.target_name), c.receiver_call)_store_capped(self._chained_sites, file_path, sites, _SOURCE_CACHE_FILES)return sites.get((line, inner.receiver_name, inner.target_name))diff --git a/packages/core/src/repowise/core/ingestion/parser/calls.py b/packages/core/src/repowise/core/ingestion/parser/calls.pyindex 047ffdaa9..4cbc80c73 100644--- a/packages/core/src/repowise/core/ingestion/parser/calls.py+++ b/packages/core/src/repowise/core/ingestion/parser/calls.py@@ -290,7 +290,9 @@ def _jsx_supplied_props(site_node: Node, src: str) -> frozenset[str] | None:return frozenset(props_set)-def _fold_receiverless_twins(calls: list[tuple[tuple[int, int, str], CallSite]]) -> list[CallSite]:+def _fold_receiverless_twins(+ calls: list[tuple[tuple[int, int, str], CallSite]],+) -> list[tuple[tuple[int, int, str], CallSite]]:"""Fold the receiver-less copy of a member call into the copy with a receiver.A grammar's bare-call pattern that leaves the object unconstrained (Java's@@ -302,15 +304,15 @@ def _fold_receiverless_twins(calls: list[tuple[tuple[int, int, str], CallSite]])"""member_sites = {site for site, call in calls if not _is_bare(call)}bare_sites = {site for site, call in calls if site in member_sites and _is_bare(call)}- folded: list[CallSite] = []+ folded: list[tuple[tuple[int, int, str], CallSite]] = []for site, call in calls:if _is_bare(call):if site not in member_sites:- folded.append(call)+ folded.append((site, call))elif call.receiver_name and site in bare_sites:- folded.append(replace(call, bare_name_fallback=True))+ folded.append((site, replace(call, bare_name_fallback=True)))else:- folded.append(call)+ folded.append((site, call))return folded@@ -318,15 +320,18 @@ def _is_bare(call: CallSite) -> bool:return not (call.receiver_name or call.receiver_call)-def _dedupe_calls(calls: list[CallSite]) -> list[CallSite]:- """One call per ``(line, target, receiver)``, keeping the richest record.+def _dedupe_calls(calls: list[tuple[tuple[int, int, str], CallSite]]) -> list[CallSite]:+ """One call per ``(site, receiver)``, keeping the richest record.Two scoped-call patterns can match the same two-part call (one keeps thequalifier), so the richer record wins whichever order they arrive in.+ Keyed on the call node, not the line: ``f(a, f(b))`` holds two calls, and+ so does ``.add(x).add(x, y)`` even across lines, since every link of a+ chain reports the chain's first line. Each may bind a different overload."""- deduplicated: dict[tuple[int, str, str | None], CallSite] = {}- for call in calls:- key = (call.line, call.target_name, call.receiver_name)+ deduplicated: dict[tuple[tuple[int, int, str], str | None], CallSite] = {}+ for site, call in calls:+ key = (site, call.receiver_name)existing = deduplicated.get(key)if (existing is Nonediff --git a/tests/unit/ingestion/parser/golden/php.json b/tests/unit/ingestion/parser/golden/php.jsonindex 399b4dcbd..84e41c848 100644--- a/tests/unit/ingestion/parser/golden/php.json+++ b/tests/unit/ingestion/parser/golden/php.json@@ -1050,6 +1050,22 @@],"export_aliases": {},"calls": [+ {+ "target_name": "array_map",+ "receiver_name": null,+ "caller_symbol_id": "lang_samples/php/nested.php::lambda_and_arrow",+ "line": 48,+ "argument_count": 2,+ "receiver_call": null,+ "scope_name": null,+ "edge_type": "calls",+ "supplied_props": null,+ "bare_name_fallback": false,+ "spawned": false,+ "program_call": false,+ "argument_types": null,+ "argument_names": null+ },{"target_name": "array_map","receiver_name": null,diff --git a/tests/unit/ingestion/test_csharp_null_conditional_calls.py b/tests/unit/ingestion/test_csharp_null_conditional_calls.pyindex cc10d2bce..40fadf717 100644--- a/tests/unit/ingestion/test_csharp_null_conditional_calls.py+++ b/tests/unit/ingestion/test_csharp_null_conditional_calls.py@@ -122,9 +122,8 @@ class TestTheCallSiteIsCaptured:"}\n")parsed, _ = _build(tmp_path, {"src/Consumer.cs": src})- sites = [(c.line, c.target_name) for c in parsed["src/Consumer.cs"].calls]- # Same line, same target: the two spellings dedupe to one record.- assert sites == [(4, "LogInformation")]+ # Two calls, one record each, both carrying the receiver.+ assert _sites(parsed, "src/Consumer.cs") == [("LogInformation", "logger", 4)] * 2class TestTheExistingTiersResolveIt:diff --git a/tests/unit/ingestion/test_same_line_calls.py b/tests/unit/ingestion/test_same_line_calls.pynew file mode 100644index 000000000..99c39a52b--- /dev/null+++ b/tests/unit/ingestion/test_same_line_calls.py@@ -0,0 +1,149 @@+"""Calls to one name that share a line each keep their own call site.++A nested ``f(a, f(b))``, two statements on one line, and every link of a+builder chain (all of which report the chain's first line) are separate calls.+Each may bind a different overload, so the parser dedupes on the call node,+not the line, and the graph draws an edge to every overload reached.+"""++from __future__ import annotations++from datetime import datetime+from pathlib import Path++import pytest++from repowise.core.ingestion.graph.builder import GraphBuilder+from repowise.core.ingestion.models import FileInfo, ParsedFile+from repowise.core.ingestion.parser import ASTParser++_PARSER = ASTParser()+++def _parse(rel: str, language: str, text: str, root: Path | None = None) -> ParsedFile:+ """Parse *text*; with *root*, also write it there, where receiver typing reads it."""+ abs_path = rel+ if root is not None:+ (root / rel).write_text(text, encoding="utf-8")+ abs_path = str(root / rel)+ info = FileInfo(+ path=rel,+ abs_path=abs_path,+ language=language, # type: ignore[arg-type]+ size_bytes=len(text),+ git_hash="",+ last_modified=datetime.now(),+ is_test=False,+ is_config=False,+ is_api_contract=False,+ is_entry_point=False,+ )+ return _PARSER.parse_file(info, text.encode("utf-8"))+++def _call_edges(root: Path, rel: str, language: str, text: str) -> set[tuple[str, str]]:+ builder = GraphBuilder(repo_path=root)+ builder.add_file(_parse(rel, language, text, root))+ graph = builder.build()+ return {(u, v) for u, v, d in graph.edges(data=True) if d.get("edge_type") == "calls"}+++ONE_LINE = {+ "java": (+ "A.java",+ "class A {\n"+ " void f(Object a) { f(a, 1); f(a, 1, 2); }\n"+ " void f(Object a, int b) {}\n"+ " void f(Object a, int b, int c) {}\n"+ "}\n",+ ),+ "csharp": (+ "A.cs",+ "class A {\n"+ " void f(object a) { f(a, 1); f(a, 1, 2); }\n"+ " void f(object a, int b) {}\n"+ " void f(object a, int b, int c) {}\n"+ "}\n",+ ),+ "cpp": (+ "a.cpp",+ "class A {\n"+ " public:\n"+ " void f(int a) { f(a, 1); f(a, 1, 2); }\n"+ " void f(int a, int b) {}\n"+ " void f(int a, int b, int c) {}\n"+ "};\n",+ ),+ "kotlin": (+ "A.kt",+ "class A {\n"+ " fun f(a: Any) { f(a, 1); f(a, 1, 2) }\n"+ " fun f(a: Any, b: Int) {}\n"+ " fun f(a: Any, b: Int, c: Int) {}\n"+ "}\n",+ ),+}++[email protected]("language", sorted(ONE_LINE))+def test_two_overloads_called_on_one_line_each_get_an_edge(language: str, tmp_path: Path) -> None:+ rel, text = ONE_LINE[language]+ caller = f"{rel}::A::f#1"+ assert _call_edges(tmp_path, rel, language, text) == {+ (caller, f"{rel}::A::f#2"),+ (caller, f"{rel}::A::f#3"),+ }+++NESTED = {+ "java": (+ "A.java",+ "class A {\n"+ " Object f(Object a) { return a; }\n"+ " Object f(Object a, Object b) { return a; }\n"+ " void g(Object x) { f(x, f(x)); }\n"+ "}\n",+ ),+ "csharp": (+ "A.cs",+ "class A {\n"+ " object f(object a) { return a; }\n"+ " object f(object a, object b) { return a; }\n"+ " void g(object x) { f(x, f(x)); }\n"+ "}\n",+ ),+}++[email protected]("language", sorted(NESTED))+def test_a_nested_call_to_another_overload_gets_its_own_edge(language: str, tmp_path: Path) -> None:+ rel, text = NESTED[language]+ caller = f"{rel}::A::g"+ edges = _call_edges(tmp_path, rel, language, text)+ assert {(caller, f"{rel}::A::f#1"), (caller, f"{rel}::A::f#2")} <= edges+++def test_each_link_of_a_multi_line_chain_binds_its_own_overload(tmp_path: Path) -> None:+ """Both ``tag`` links report the chain's first line, ``head()``."""+ text = (+ "class B {\n"+ " B head() { return this; }\n"+ " B tag(int a) { return this; }\n"+ " B tag(int a, int b) { return this; }\n"+ " void run() {\n"+ " head()\n"+ " .tag(1)\n"+ " .tag(1, 2);\n"+ " }\n"+ "}\n"+ )+ edges = _call_edges(tmp_path, "B.java", "java", text)+ assert {("B.java::B::run", "B.java::B::tag#1"), ("B.java::B::run", "B.java::B::tag#2")} <= edges+++def test_one_call_matched_by_two_patterns_stays_one_record() -> None:+ """``S.super.m()`` and C++ ``ns::f()`` are each matched twice on one node."""+ java = _parse("A.java", "java", "class A implements I { void m() { I.super.m(); } }\n")+ cpp = _parse("a.cpp", "cpp", "void g() { ns::f(1); }\n")+ assert [(c.target_name, c.scope_name) for c in java.calls] == [("m", "I")]+ assert [(c.target_name, c.scope_name) for c in cpp.calls] == [("f", "ns")]