Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(extraction): recover C# 14 extension block members

yasmoradi7054e4ff8af7
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index b49147dc6..b00604753 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -5032,6 +5032,61 @@ static void extract_objc_impl_methods(CBMExtractCtx *ctx, TSNode impl_node, cons
}
}
+/* True for a C# 14 extension block. The vendored tree-sitter-c-sharp has no
+ * `extension` production, so `extension(string text) { ... }` parses as a
+ * constructor_declaration named `extension` and nothing flags it: has_error
+ * stays 0. A constructor carries its own type's name, so this shape can only be
+ * a genuine constructor inside a type literally called `extension` (#2071). */
+static bool csharp_is_extension_block(TSNode node, TSNode class_node, const char *source) {
+ if (strcmp(ts_node_type(node), "constructor_declaration") != 0) {
+ return false;
+ }
+ TSNode name = resolve_method_name(node, CBM_LANG_CSHARP);
+ if (ts_node_is_null(name)) {
+ return false;
+ }
+ const size_t kw_len = sizeof("extension") - 1;
+ uint32_t len = ts_node_end_byte(name) - ts_node_start_byte(name);
+ if (len != kw_len || strncmp(source + ts_node_start_byte(name), "extension", kw_len) != 0) {
+ return false;
+ }
+ TSNode type_name = ts_node_child_by_field_name(class_node, TS_FIELD("name"));
+ if (!ts_node_is_null(type_name)) {
+ uint32_t tlen = ts_node_end_byte(type_name) - ts_node_start_byte(type_name);
+ if (tlen == kw_len &&
+ strncmp(source + ts_node_start_byte(type_name), "extension", kw_len) == 0) {
+ return false;
+ }
+ }
+ return true;
+}
+
+/* Lift the members of a misparsed C# 14 extension block onto the enclosing
+ * type. They sit in the block the grammar took for a constructor body, which
+ * nothing recurses into, so every one of them was dropped. Methods land there
+ * as local_function_statement; a property in the block parses as ERROR and is
+ * beyond recovery without a grammar that knows the construct (#2071). */
+static void extract_csharp_extension_members(CBMExtractCtx *ctx, TSNode ext_node, TSNode class_node,
+ const char *class_qn, const CBMLangSpec *spec) {
+ TSNode body = cbm_find_child_by_kind(ext_node, "block");
+ if (ts_node_is_null(body)) {
+ return;
+ }
+ uint32_t count = ts_node_child_count(body);
+ for (uint32_t i = 0; i < count; i++) {
+ TSNode member = ts_node_child(body, i);
+ if (ts_node_is_null(member) ||
+ strcmp(ts_node_type(member), "local_function_statement") != 0) {
+ continue;
+ }
+ TSNode name = resolve_method_name(member, ctx->language);
+ if (ts_node_is_null(name)) {
+ continue;
+ }
+ push_method_def(ctx, member, class_node, class_qn, spec, name);
+ }
+}
+
// Extract methods inside a class body
static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
const CBMLangSpec *spec) {
@@ -5047,6 +5102,15 @@ static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const c
continue;
}
+ /* C# 14 extension block: emit the members it holds as methods of the
+ * enclosing type, rather than the block itself as a constructor named
+ * `extension` with its members buried in the body (#2071). */
+ if (ctx->language == CBM_LANG_CSHARP &&
+ csharp_is_extension_block(child, class_node, ctx->source)) {
+ extract_csharp_extension_members(ctx, child, class_node, class_qn, spec);
+ continue;
+ }
+
if (ctx->language == CBM_LANG_OBJC &&
strcmp(ts_node_type(child), "implementation_definition") == 0) {
extract_objc_impl_methods(ctx, child, class_qn, spec);
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 14d75bf8b..618561f74 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -772,6 +772,43 @@ TEST(csharp_interface) {
PASS();
}
+/* Issue #2071: a C# 14 extension block parses as a constructor_declaration
+ * named `extension` whose body holds the members, and has_error stays 0, so the
+ * members were dropped without any signal. They belong to the enclosing type. */
+TEST(csharp_extension_block_members) {
+ CBMFileResult *r = extract("namespace App;\n"
+ "public static class NumberExtensions\n"
+ "{\n"
+ " extension(IEnumerable<int> numbers)\n"
+ " {\n"
+ " public int SumPositive() => 0;\n"
+ " public static int Zero() => 0;\n"
+ " }\n"
+ "}\n",
+ CBM_LANG_CSHARP, "t", "NumberExtensions.cs");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT(has_def(r, "Class", "NumberExtensions"));
+ ASSERT(has_def(r, "Method", "SumPositive"));
+ ASSERT(has_def(r, "Method", "Zero"));
+ ASSERT_FALSE(has_def_any(r, "extension"));
+ cbm_free_result(r);
+ PASS();
+}
+
+/* The same shape is a genuine constructor inside a type actually called
+ * `extension`, and must still be emitted (#2071). */
+TEST(csharp_constructor_of_type_named_extension) {
+ CBMFileResult *r =
+ extract("namespace App { public class extension { public extension() { } } }",
+ CBM_LANG_CSHARP, "t", "extension.cs");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT(has_def(r, "Method", "extension"));
+ cbm_free_result(r);
+ PASS();
+}
+
/* --- Swift --- */
TEST(swift_class) {
CBMFileResult *r = extract("class Vehicle {\n var speed: Int = 0\n func accelerate() { "
@@ -7580,6 +7617,8 @@ SUITE(extraction) {
RUN_TEST(ruby_module);
RUN_TEST(csharp_class);
RUN_TEST(csharp_interface);
+ RUN_TEST(csharp_extension_block_members);
+ RUN_TEST(csharp_constructor_of_type_named_extension);
RUN_TEST(swift_class);
RUN_TEST(swift_protocol);
RUN_TEST(kotlin_function);