Skip to content

Commit 4c01d66

Browse files
committed
fix(java-enum): resolve enum methods from enum_body_declarations
Signed-off-by: sahil-mangla <manglasahil2017@gmail.com>
1 parent feadbe1 commit 4c01d66

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

internal/cbm/extract_defs.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3554,8 +3554,9 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
35543554
}
35553555

35563556
TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("name"));
3557-
// ObjC: class name is first identifier child
3558-
if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_OBJC) {
3557+
// ObjC/Java: class name is first identifier child (Java fallback for enum_declaration)
3558+
if (ts_node_is_null(name_node) &&
3559+
(ctx->language == CBM_LANG_OBJC || ctx->language == CBM_LANG_JAVA)) {
35593560
name_node = cbm_find_child_by_kind(node, "identifier");
35603561
}
35613562
// ObjectScript UDL: class name is a `class_name` child (no "name" field).
@@ -3974,6 +3975,12 @@ static TSNode find_class_body(TSNode class_node, CBMLanguage lang) {
39743975
for (const char **f = body_fields; *f; f++) {
39753976
TSNode body = ts_node_child_by_field_name(class_node, *f, (uint32_t)strlen(*f));
39763977
if (!ts_node_is_null(body)) {
3978+
if (strcmp(ts_node_type(body), "enum_body") == 0) {
3979+
TSNode decls = cbm_find_child_by_kind(body, "enum_body_declarations");
3980+
if (!ts_node_is_null(decls)) {
3981+
return decls;
3982+
}
3983+
}
39773984
return body;
39783985
}
39793986
}
@@ -4029,17 +4036,31 @@ static TSNode find_class_body(TSNode class_node, CBMLanguage lang) {
40294036
"implementation_definition",
40304037
NULL};
40314038
uint32_t count = ts_node_child_count(class_node);
4039+
TSNode result = {0};
40324040
for (uint32_t i = 0; i < count; i++) {
40334041
TSNode child = ts_node_child(class_node, i);
40344042
const char *ck = ts_node_type(child);
40354043
for (const char **t = body_types; *t; t++) {
40364044
if (strcmp(ck, *t) == 0) {
4037-
return child;
4045+
result = child;
4046+
break;
40384047
}
40394048
}
4049+
if (!ts_node_is_null(result)) {
4050+
break;
4051+
}
40404052
}
4041-
TSNode null_node = {0};
4042-
return null_node;
4053+
if (!ts_node_is_null(result) && strcmp(ts_node_type(result), "enum_body") == 0) {
4054+
TSNode decls = cbm_find_child_by_kind(result, "enum_body_declarations");
4055+
if (!ts_node_is_null(decls)) {
4056+
return decls;
4057+
}
4058+
}
4059+
if (ts_node_is_null(result)) {
4060+
TSNode null_node = {0};
4061+
return null_node;
4062+
}
4063+
return result;
40434064
}
40444065

40454066
// Dart: resolve method name from method_signature/function_signature.
@@ -6200,7 +6221,8 @@ static void push_class_body_children(TSNode node, const CBMLangSpec *spec, wd_st
62006221
const char *ck = ts_node_type(child);
62016222
if (strcmp(ck, "field_declaration_list") == 0 || strcmp(ck, "class_body") == 0 ||
62026223
strcmp(ck, "declaration_list") == 0 || strcmp(ck, "body") == 0 ||
6203-
strcmp(ck, "block") == 0 || strcmp(ck, "suite") == 0 ||
6224+
strcmp(ck, "block") == 0 || strcmp(ck, "suite") == 0 || strcmp(ck, "enum_body") == 0 ||
6225+
strcmp(ck, "enum_body_declarations") == 0 ||
62046226
// Groovy class bodies are a `closure` node; routing through the
62056227
// nested-class path keeps methods from being re-walked (and thus
62066228
// double-extracted) as top-level functions. Gated to Groovy so other

tests/test_extraction.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,23 @@ TEST(java_class_extends_and_implements) {
418418
PASS();
419419
}
420420

421+
TEST(java_enum_method) {
422+
CBMFileResult *r = extract(
423+
"enum Day {\n"
424+
" MON, TUE, WED, THU, FRI, SAT, SUN;\n\n"
425+
" public boolean isWeekend() { return this == SAT || this == SUN; }\n"
426+
" public String label() { return name().toLowerCase(); }\n"
427+
"}\n",
428+
CBM_LANG_JAVA, "t", "Day.java");
429+
ASSERT_NOT_NULL(r);
430+
ASSERT_FALSE(r->has_error);
431+
ASSERT(has_def(r, "Enum", "Day"));
432+
ASSERT(has_def(r, "Method", "isWeekend"));
433+
ASSERT(has_def(r, "Method", "label"));
434+
cbm_free_result(r);
435+
PASS();
436+
}
437+
421438
/* REPRODUCTION (RED until fixed) — Python `class Animal(Base):` must extract the
422439
* BARE base name "Base", but extract_base_classes captures the whole
423440
* `superclasses` argument_list text "(Base)" instead: collect_bases_from_field
@@ -4429,6 +4446,7 @@ SUITE(extraction) {
44294446
RUN_TEST(java_method);
44304447
RUN_TEST(java_interface);
44314448
RUN_TEST(java_class_extends_and_implements);
4449+
RUN_TEST(java_enum_method);
44324450
RUN_TEST(python_class_base_extracted_bare);
44334451
RUN_TEST(php_class);
44344452
RUN_TEST(php_function);

0 commit comments

Comments
 (0)