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(dart): index extension declarations and their members (#1457)

Martin Vogelc6ccdd9395db
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 916f1136f..5b0571a46 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -7454,6 +7454,38 @@ static void extract_typescript_namespace_def(CBMExtractCtx *ctx, TSNode node,
cbm_defs_push(&ctx->result->defs, ctx->arena, def);
}
+/* Dart `extension on T { ... }` has no name: there is no container def to hang
+ * its members on, and extract_class_def would bail before extract_class_methods,
+ * dropping every member (#1457). */
+static bool is_dart_unnamed_extension(const CBMExtractCtx *ctx, TSNode node) {
+ return ctx->language == CBM_LANG_DART &&
+ strcmp(ts_node_type(node), "extension_declaration") == 0 &&
+ ts_node_is_null(ts_node_child_by_field_name(node, TS_FIELD("name")));
+}
+
+/* Walk an unnamed Dart extension's members as file-level functions. A member is
+ * `method_signature > function_signature`; the wrapper carries no name of its
+ * own, so push the inner function_signature (which does) for the generic walk. */
+static void push_dart_unnamed_extension_members(TSNode node, wd_stack_t *s,
+ const char *enclosing_qn) {
+ TSNode body = ts_node_child_by_field_name(node, TS_FIELD("body"));
+ if (ts_node_is_null(body)) {
+ return;
+ }
+ uint32_t nc = ts_node_named_child_count(body);
+ for (int i = (int)nc - 1; i >= 0; i--) {
+ TSNode child = ts_node_named_child(body, (uint32_t)i);
+ if (strcmp(ts_node_type(child), "method_signature") == 0) {
+ TSNode sig = cbm_find_child_by_kind(child, "function_signature");
+ if (!ts_node_is_null(sig)) {
+ wd_push(s, sig, enclosing_qn);
+ }
+ continue;
+ }
+ wd_push(s, child, enclosing_qn);
+ }
+}
+
// Push nested class children from a class body container onto the walk stack.
static void push_class_body_children(TSNode node, const CBMLangSpec *spec, wd_stack_t *s,
const char *new_enclosing, const CBMExtractCtx *ctx) {
@@ -8056,6 +8088,11 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
continue;
}
+ if (is_dart_unnamed_extension(ctx, node)) {
+ push_dart_unnamed_extension_members(node, &s, frame.enclosing_class_qn);
+ continue;
+ }
+
if (cbm_kind_in_set(node, spec->class_node_types)) {
extract_class_def(ctx, node, spec);
const char *new_enclosing = compute_class_qn(ctx, node, frame.enclosing_class_qn);
diff --git a/internal/cbm/lang_specs.c b/internal/cbm/lang_specs.c
index 3a5fad0d4..cfd363c71 100644
--- a/internal/cbm/lang_specs.c
+++ b/internal/cbm/lang_specs.c
@@ -594,8 +594,9 @@ static const char *swift_decorator_types[] = {"attribute", NULL};
// ==================== DART ====================
static const char *dart_func_types[] = {"function_signature", "method_signature",
"lambda_expression", NULL};
-static const char *dart_class_types[] = {"class_definition", "enum_declaration",
- "mixin_declaration", "type_alias", NULL};
+static const char *dart_class_types[] = {"class_definition", "enum_declaration",
+ "mixin_declaration", "extension_declaration",
+ "type_alias", NULL};
static const char *dart_field_types[] = {"declaration", NULL};
static const char *dart_module_types[] = {"program", NULL};
static const char *dart_call_types[] = {"selector", "new_expression", NULL};
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 1e6506ed1..fd08d3fd6 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -862,6 +862,43 @@ TEST(dart_class) {
PASS();
}
+/* --- Dart extension members (#1457) --- */
+TEST(dart_extension_members) {
+ CBMFileResult *r =
+ extract("class Holder {\n final int value;\n const Holder(this.value);\n}\n\n"
+ "extension HolderMath on Holder {\n int doubled() => value + value;\n"
+ " int tripled() => value + value + value;\n"
+ " String describeHolder() => 'holder';\n}\n\n"
+ "int topLevelHelper(int x) => x + 1;\n",
+ CBM_LANG_DART, "t", "a.dart");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT(has_def(r, "Class", "Holder"));
+ ASSERT(has_def(r, "Function", "topLevelHelper"));
+ ASSERT(has_def(r, "Class", "HolderMath"));
+ ASSERT(has_def(r, "Method", "doubled"));
+ ASSERT(has_def(r, "Method", "tripled"));
+ ASSERT(has_def(r, "Method", "describeHolder"));
+ /* Members hang off the extension (DEFINES_METHOD source), not the file. */
+ for (int i = 0; i < r->defs.count; i++) {
+ const CBMDefinition *d = &r->defs.items[i];
+ if (d->name && strcmp(d->name, "doubled") == 0) {
+ ASSERT_NOT_NULL(d->parent_class);
+ ASSERT_STR_EQ(d->parent_class, "t.a.HolderMath");
+ }
+ }
+ cbm_free_result(r);
+
+ /* Unnamed extension: its members are still indexed. */
+ r = extract("extension on String {\n bool isShout() => this == toUpperCase();\n}\n",
+ CBM_LANG_DART, "t", "b.dart");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT(has_def_any(r, "isShout"));
+ cbm_free_result(r);
+ PASS();
+}
+
/* --- Groovy --- */
TEST(groovy_class) {
CBMFileResult *r =
@@ -8356,6 +8393,7 @@ SUITE(extraction) {
RUN_TEST(scala_function);
RUN_TEST(scala_class);
RUN_TEST(dart_class);
+ RUN_TEST(dart_extension_members);
RUN_TEST(groovy_class);
/* Systems */