fix(extract): emit Java enum constants as nodes with case_of edges

This commit is contained in:
ivanzhilovich
2026-07-08 00:42:56 +01:00
committed by safishamsi
parent 53efaf89b6
commit cf36d10292
3 changed files with 52 additions and 0 deletions
+32
View File
@@ -2933,6 +2933,32 @@ def _swift_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: s
return False
# ── Java extra walk for enum constants ───────────────────────────────────────
def _java_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: str,
nodes: list, edges: list, seen_ids: set, function_bodies: list,
parent_class_nid: str | None, add_node_fn, add_edge_fn,
walk_fn) -> bool:
"""Handle enum_constant for Java. Returns True if handled."""
if node.type == "enum_constant" and parent_class_nid:
name_node = node.child_by_field_name("name")
if name_node is None:
return True
const_name = _read_text(name_node, source)
line = node.start_point[0] + 1
const_nid = _make_id(parent_class_nid, const_name)
add_node_fn(const_nid, const_name, line)
add_edge_fn(parent_class_nid, const_nid, "case_of", line)
# Anonymous-body constants (`MONDAY { void greet(){} }`): descend so the
# body's methods aren't dropped; const_nid attaches them to the constant.
for child in node.children:
if child.type == "class_body":
for member in child.children:
walk_fn(member, parent_class_nid=const_nid)
return True
return False
# ── Language configs ──────────────────────────────────────────────────────────
_PYTHON_CONFIG = LanguageConfig(
@@ -4855,6 +4881,12 @@ def _extract_generic(
ensure_named_node):
return
if config.ts_module == "tree_sitter_java":
if _java_extra_walk(node, source, file_nid, stem, str_path,
nodes, edges, seen_ids, function_bodies,
parent_class_nid, add_node, add_edge, walk):
return
if config.ts_module == "tree_sitter_ruby":
if _ruby_extra_walk(node, source, file_nid, stem, str_path,
nodes, edges, seen_ids, function_bodies,
+11
View File
@@ -39,3 +39,14 @@ public class DataProcessor extends BaseProcessor implements Processor {
interface Processor {
List<String> process();
}
enum ErrorCode {
OK(0),
GAME_DONE(1001);
private final int code;
ErrorCode(int code) {
this.code = code;
}
}
+9
View File
@@ -115,6 +115,15 @@ def test_java_no_dangling_edges():
assert e["source"] in node_ids
def test_java_enum_constants_have_case_of_edge():
r = extract_java(FIXTURES / "sample.java")
labels = _labels(r)
assert "OK" in labels
assert "GAME_DONE" in labels
assert ("ErrorCode", "OK") in _edge_labels(r, "case_of")
assert ("ErrorCode", "GAME_DONE") in _edge_labels(r, "case_of")
# ── C ────────────────────────────────────────────────────────────────────────
def test_c_no_error():