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(csharp): bind locals created with object-initializer syntax (#3957)

Yunus Ozenc0f5a00bf9ce
diff --git a/packages/core/src/repowise/core/ingestion/languages/csharp_member_reads.py b/packages/core/src/repowise/core/ingestion/languages/csharp_member_reads.py
index e6e46661b..dc5915dac 100644
--- a/packages/core/src/repowise/core/ingestion/languages/csharp_member_reads.py
+++ b/packages/core/src/repowise/core/ingestion/languages/csharp_member_reads.py
@@ -47,13 +47,14 @@ if TYPE_CHECKING:
import networkx as nx
# ``var foo = new Bar(`` / ``var foo = new Bar<...>(`` / ``Bar foo = new(``
-# / ``Bar foo = new Bar(``. We capture the LOCAL name + the type name
-# in a single match. Generic / qualified type names are stripped to the
+# / ``Bar foo = new Bar(``, plus the object-initializer forms
+# ``var foo = new Bar {`` / ``Bar foo = new Bar {``. We capture the
+# LOCAL name + the type name in a single match. Generic / qualified type names are stripped to the
# last identifier so we resolve through ``DotNetProjectIndex.type_map``.
_LOCAL_DECL_RE = re.compile(
r"\b(?:var|(?P<type1>[A-Z][\w\.]*(?:<[^>]+>)?))\s+"
r"(?P<name>[a-z_]\w*)\s*=\s*"
- r"new(?:\s+(?P<type2>[A-Z][\w\.]*(?:<[^>]+>)?))?\s*\("
+ r"new(?:\s+(?P<type2>[A-Z][\w\.]*(?:<[^>]+>)?))?\s*[({]"
)
# ``receiver.Member`` — only PascalCase member names (properties /
diff --git a/tests/unit/ingestion/test_csharp_member_reads.py b/tests/unit/ingestion/test_csharp_member_reads.py
index f62e03b12..f3652cff3 100644
--- a/tests/unit/ingestion/test_csharp_member_reads.py
+++ b/tests/unit/ingestion/test_csharp_member_reads.py
@@ -53,6 +53,60 @@ def test_typed_local_binds_member_read() -> None:
assert graph.has_edge("Caller.cs", "Order.cs")
+def test_object_initializer_local_binds_member_read() -> None:
+ """``var x = new Order { ... }; ... x.Total`` resolves to Order.cs."""
+ graph = _seed_graph("Caller.cs", "Order.cs")
+ cs_texts = {
+ "Caller.cs": (
+ "namespace Acme;\npublic class Caller {\n"
+ " decimal Sum() { var order = new Order { Id = 42, Total = 100m };"
+ " return order.Total; }\n}"
+ ),
+ "Order.cs": "namespace Acme;\npublic class Order { public decimal Total { get; init; } }",
+ }
+ type_to_file = {"Order": "Order.cs", "Caller": "Caller.cs"}
+
+ added = resolve_csharp_member_reads(graph, cs_texts, type_to_file)
+
+ assert added == 1
+ assert graph.has_edge("Caller.cs", "Order.cs")
+
+
+def test_typed_object_initializer_local_binds_member_read() -> None:
+ """``Order o = new Order{...};`` binds even with no space before the brace."""
+ graph = _seed_graph("Caller.cs", "Order.cs")
+ cs_texts = {
+ "Caller.cs": (
+ "namespace Acme;\npublic class Caller {\n"
+ " decimal Sum() { Order o = new Order{ Total = 1m }; return o.Total; }\n}"
+ ),
+ "Order.cs": "namespace Acme;\npublic class Order { public decimal Total { get; init; } }",
+ }
+ type_to_file = {"Order": "Order.cs", "Caller": "Caller.cs"}
+
+ resolve_csharp_member_reads(graph, cs_texts, type_to_file)
+
+ assert graph.has_edge("Caller.cs", "Order.cs")
+
+
+def test_anonymous_object_no_edge() -> None:
+ """``var x = new { ... }`` is an anonymous type, so no type is bound."""
+ graph = _seed_graph("Caller.cs", "Order.cs")
+ cs_texts = {
+ "Caller.cs": (
+ "namespace Acme;\npublic class Caller {\n"
+ " decimal Sum() { var order = new { Total = 1m }; return order.Total; }\n}"
+ ),
+ "Order.cs": "namespace Acme;\npublic class Order { public decimal Total { get; init; } }",
+ }
+ type_to_file = {"Order": "Order.cs", "Caller": "Caller.cs"}
+
+ added = resolve_csharp_member_reads(graph, cs_texts, type_to_file)
+
+ assert added == 0
+ assert not graph.has_edge("Caller.cs", "Order.cs")
+
+
def test_unknown_receiver_no_edge() -> None:
"""An untyped receiver must not produce an edge — would be a guess."""
graph = _seed_graph("Caller.cs", "Order.cs")