Skip to content

Commit bcf9624

Browse files
Mpasha17Grok Bot
authored andcommitted
feat(scala): link classes to typeclasses in derives clauses (#2048)
`case class Point(x: Int) derives Show, Eq` (and the same on enums) now emits a `references` edge with the `derives` context from the type to each derived typeclass, next to the class-level context bounds it resembles. A qualified name (`derives cats.Show`) links by its last segment. Co-Authored-By: Grok Bot <[email protected]> (cherry picked from commit e6ccd59)
1 parent 1adadc7 commit bcf9624

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎graphify/extractors/engine.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5640,6 +5640,20 @@ def scala_base_name(type_node) -> str | None:
56405640
if target_nid != class_nid:
56415641
add_edge(class_nid, target_nid, "references",
56425642
line, context=ctx)
5643+
# `derives Show, cats.Eq` (on classes and enums) names typeclasses
5644+
# the type depends on, like a context bound (#2048). A qualified
5645+
# name links by its last segment.
5646+
derive = node.child_by_field_name("derive")
5647+
for d in derive.children_by_field_name("type") if derive is not None else ():
5648+
tail = d if d.type == "type_identifier" else next(
5649+
(x for x in reversed(d.children) if x.type == "type_identifier"), None)
5650+
if tail is None:
5651+
continue
5652+
d_line = d.start_point[0] + 1
5653+
target_nid = ensure_named_node(_read_text(tail, source), d_line)
5654+
if target_nid != class_nid:
5655+
add_edge(class_nid, target_nid, "references",
5656+
d_line, context="derives")
56435657

56445658
# C#: a primary constructor (`class Foo(IBar bar)`, C# 12+) declares
56455659
# its dependencies on the type declaration itself rather than in a

‎tests/test_scala_context_bounds.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,52 @@ def test_class_bound_edge_is_tagged_extracted_with_a_location(tmp_path):
228228
for e in edges:
229229
assert e.get("confidence") == "EXTRACTED", e
230230
assert e.get("source_location") == "L7", e
231+
232+
233+
# `derives` clauses (#2048) name typeclasses the type depends on, the same kind
234+
# of dependency as a context bound, so each one is a `references` edge with the
235+
# `derives` context. Grammar: a `derive` field on class_definition and
236+
# enum_definition, one `type` child per derived typeclass.
237+
DERIVES_SRC = '''\
238+
trait Show
239+
trait Eq
240+
241+
case class Point(x: Int) derives Show, Eq
242+
243+
enum Color derives Eq { case Red }
244+
245+
case class Wrapped(x: Int) derives cats.Show
246+
247+
class Bare(x: Int)
248+
'''
249+
250+
251+
def _derives_targets(tmp_path, label):
252+
(tmp_path / "Derives.scala").write_text(DERIVES_SRC)
253+
r = extract_scala(tmp_path / "Derives.scala")
254+
nid = {n["label"]: n["id"] for n in r["nodes"]}
255+
targets = {e["target"] for e in r["edges"]
256+
if e["relation"] == "references"
257+
and e["source"] == nid[label]
258+
and e.get("context") == "derives"}
259+
return targets, nid
260+
261+
262+
def test_case_class_derives_each_typeclass(tmp_path):
263+
targets, nid = _derives_targets(tmp_path, "Point")
264+
assert targets == {nid["Show"], nid["Eq"]}
265+
266+
267+
def test_enum_derives_typeclass(tmp_path):
268+
targets, nid = _derives_targets(tmp_path, "Color")
269+
assert targets == {nid["Eq"]}
270+
271+
272+
def test_qualified_derives_links_by_last_segment(tmp_path):
273+
targets, nid = _derives_targets(tmp_path, "Wrapped")
274+
assert targets == {nid["Show"]}
275+
276+
277+
def test_class_without_derives_emits_no_derives_edge(tmp_path):
278+
targets, _ = _derives_targets(tmp_path, "Bare")
279+
assert not targets

0 commit comments

Comments
 (0)