fix(scala): extract Scala 3 enum definitions, their cases and methods

`enum_definition` was missing from the Scala class_types, so a Scala 3
`enum` produced no nodes at all: the enum type, its cases, and any methods
declared in its body all vanished from the graph, and references to the
enum resolved to nothing.

Add `enum_definition` to the Scala class_types and `enum_body` to the
body fallbacks so the type gets a node and its body is walked, and add
`_scala_extra_walk` to emit a node plus a `case_of` edge per case. Cases
nest under an `enum_case_definitions` wrapper as `simple_enum_case`
(`case Red`) or `full_enum_case` (`case Add(x: Int)`); both carry the case
name as an `identifier` child. This is the Scala parity of Java's
enum_constant (#1719), Kotlin's enum_entry (#1738), and Swift.

Case constructor parameter types and per-case `extends` arguments are not
linked; the case node and its case_of edge are the fix for the dropped
members.

(cherry picked from commit 175af52419)
This commit is contained in:
rajatnagda45 authored and safishamsi committed 2026-09-30 20:10:34 +01:00
1 parent b507207a1a
commit 35207e9b4b
3 files changed
+88 -2

No files matched your search

+7 -2
View File
@@ -1440,7 +1440,10 @@ _SCALA_CONFIG = LanguageConfig(
ts_module="tree_sitter_scala",
# traits are class-like containers with their own heritage (extends / with),
# so they need a node and the heritage walk just like classes and objects.
class_types=frozenset({"class_definition", "object_definition", "trait_definition"}),
# Scala 3 `enum` is a class-like container too: it owns methods and a set of
# cases, so it needs a node and a body walk like the others (its cases are
# emitted by _scala_extra_walk, the parity of Java #1719 / Kotlin #1738).
class_types=frozenset({"class_definition", "object_definition", "trait_definition", "enum_definition"}),
function_types=frozenset({"function_definition"}),
import_types=frozenset({"import_declaration"}),
call_types=frozenset({"call_expression"}),
@@ -1448,7 +1451,9 @@ _SCALA_CONFIG = LanguageConfig(
call_accessor_node_types=frozenset({"field_expression"}),
call_accessor_field="field",
name_fallback_child_types=("identifier",),
body_fallback_child_types=("template_body",),
# an enum wraps its members in `enum_body` rather than a `template_body`,
# so the body walk needs it to reach the enum's methods and cases.
body_fallback_child_types=("template_body", "enum_body"),
function_boundary_types=frozenset({"function_definition"}),
import_handler=_import_scala,
)
+48
View File
@@ -2970,6 +2970,46 @@ def _swift_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: s
return True
return False
def _scala_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:
"""Emit a node per Scala 3 enum case with a case_of edge. Returns True if handled.
tree-sitter-scala wraps the members of an `enum` in `enum_case_definitions`,
whose children are `simple_enum_case` (`case Red`) or `full_enum_case`
(`case Add(x: Int, y: Int)`); both carry the case name as an `identifier`
child. Without this the enum type is a class-like container whose members
were never emitted, leaving the type a leaf. This is the Scala parity of
Java #1719 (enum_constant), Kotlin #1738 (enum_entry), and Swift.
Case constructor parameter types (`Add(x: Int)`) and per-case `extends`
arguments are not linked; the case node and its case_of edge are the fix
for the dropped members.
"""
if node.type == "enum_case_definitions" and parent_class_nid:
for case in node.children:
if case.type not in ("simple_enum_case", "full_enum_case"):
continue
name_node = next(
(c for c in case.children if c.type == "identifier"), None
)
if name_node is None:
continue
case_name = _read_text(name_node, source)
if not case_name:
continue
line = case.start_point[0] + 1
case_nid = _make_id(parent_class_nid, case_name)
# Scala is case-sensitive while the id recipe casefolds, so two
# members that differ only in case would collide on one id; the
# first declaration keeps the node rather than a second edge on it.
if case_nid not in seen_ids:
add_node_fn(case_nid, case_name, line)
add_edge_fn(parent_class_nid, case_nid, "case_of", line)
return True
return False
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,
@@ -5714,6 +5754,14 @@ def _extract_generic(
parent_class_nid, add_node, add_edge, walk):
return
# Scala 3 enum cases (`case Red`, `case Add(x: Int)`) nest under an
# `enum_case_definitions` wrapper; emit a node + case_of edge per case.
if config.ts_module == "tree_sitter_scala":
if _scala_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,
+33
View File
@@ -1154,6 +1154,39 @@ def test_scala_val_definition_field_context():
assert ("HttpClient", "Config") in _edge_labels(r, "references", "field")
def test_scala_enum_definition_cases_and_methods(tmp_path):
"""A Scala 3 `enum` is a class-like container that owns cases and methods.
`enum_definition` was missing from the Scala class_types, so the enum type,
all of its cases (`case Red`, `case Add(...)`), and its methods produced no
nodes at all — the type vanished from the graph. The cases must each become
a node with a `case_of` edge (parity with Java #1719 / Kotlin #1738 / Swift).
"""
f = tmp_path / "color.scala"
f.write_text(
"enum Color { case Red, Green, Blue }\n"
"enum Op[A] {\n"
" case Add(x: Int, y: Int)\n"
" case Noop\n"
" def run: A = ???\n"
"}\n"
)
r = extract_scala(f)
labels = _labels(r)
# The enum types themselves are contained by the file.
contains = _edge_labels(r, "contains")
assert ("color.scala", "Color") in contains
assert ("color.scala", "Op") in contains
# Every case is emitted with a case_of edge back to its enum.
case_of = _edge_labels(r, "case_of")
assert ("Color", "Red") in case_of
assert ("Color", "Green") in case_of
assert ("Color", "Blue") in case_of
assert ("Op", "Add") in case_of
assert ("Op", "Noop") in case_of
# A method declared in the enum body is still captured.
assert any(_normalize_symbol_label(l) == "run" for l in labels)
def test_scala_var_definition_field_context():
r = extract_scala(FIXTURES / "sample.scala")
assert ("HttpClient", "BaseClient") in _edge_labels(r, "references", "field")