From ad7015262b8876980c819ed20cd9dabe51facc8b Mon Sep 17 00:00:00 2001 From: Synvoya <16019863+Synvoya@users.noreply.github.com> Date: Thu, 2 Jul 2026 00:30:39 +1000 Subject: [PATCH] fix(swift): emit references for enum associated-value types The Swift `enum_entry` handler in `_swift_extra_walk` iterated the entry's children only for the `simple_identifier` case name (emitting a `case_of` edge) and never descended into the sibling `enum_type_parameters` node, where associated-value types live (`enum_type_parameters -> user_type -> type_identifier`). As a result `case started(Session)` silently dropped the `Event -> Session` type reference. Descend into each `enum_type_parameters` child after emitting `case_of`, run `_swift_collect_type_refs` over its named children, and emit a `references` edge from the enum node to each collected type (context `type`, or `generic_arg` for generic roles), guarding target != enum node. Mirrors the existing Swift property/parameter/return-type emit style. Fixture: add `case failed(Config)` to `NetworkError` in sample.swift. Test: assert (`NetworkError`, `Config`) in references(context=type). --- graphify/extract.py | 28 +++++++++++++++++++++++++--- tests/fixtures/sample.swift | 1 + tests/test_languages.py | 4 ++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/graphify/extract.py b/graphify/extract.py index 9b366ab..4a99636 100644 --- a/graphify/extract.py +++ b/graphify/extract.py @@ -2598,16 +2598,37 @@ def _csharp_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: def _swift_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) -> bool: + parent_class_nid: str | None, add_node_fn, add_edge_fn, + ensure_named_node_fn) -> bool: """Handle enum_entry for Swift. Returns True if handled.""" if node.type == "enum_entry" and parent_class_nid: + line = node.start_point[0] + 1 for child in node.children: if child.type == "simple_identifier": case_name = _read_text(child, source) case_nid = _make_id(parent_class_nid, case_name) - line = node.start_point[0] + 1 add_node_fn(case_nid, case_name, line) add_edge_fn(parent_class_nid, case_nid, "case_of", line) + # Associated-value types nest as `enum_type_parameters -> user_type -> + # type_identifier` (a sibling of the case-name simple_identifier). The + # case-name loop above never descends into them, so `case started(Session)` + # used to drop the Event -> Session reference entirely. Mirror the Swift + # property/parameter emit style: collect the type refs and emit a + # `references` edge from the ENUM node to each collected type. + for child in node.children: + if child.type != "enum_type_parameters": + continue + for grand in child.children: + if not grand.is_named: + continue + refs: list[tuple[str, str]] = [] + _swift_collect_type_refs(grand, source, False, refs) + for ref_name, role in refs: + ctx = "generic_arg" if role == "generic_arg" else "type" + target_nid = ensure_named_node_fn(ref_name, line) + if target_nid != parent_class_nid: + add_edge_fn(parent_class_nid, target_nid, "references", + line, context=ctx) return True return False @@ -4369,7 +4390,8 @@ def _extract_generic( if config.ts_module == "tree_sitter_swift": if _swift_extra_walk(node, source, file_nid, stem, str_path, nodes, edges, seen_ids, function_bodies, - parent_class_nid, add_node, add_edge): + parent_class_nid, add_node, add_edge, + ensure_named_node): return # Python's `@property` / `@staticmethod` / `@classmethod` wrap the diff --git a/tests/fixtures/sample.swift b/tests/fixtures/sample.swift index 0a51d2f..64d5a42 100644 --- a/tests/fixtures/sample.swift +++ b/tests/fixtures/sample.swift @@ -51,6 +51,7 @@ enum NetworkError { case timeout case connectionFailed case unauthorized + case failed(Config) func describe() -> String { return "error" diff --git a/tests/test_languages.py b/tests/test_languages.py index 70ae135..ca5169d 100644 --- a/tests/test_languages.py +++ b/tests/test_languages.py @@ -900,6 +900,10 @@ def test_swift_enum_cases_have_case_of_edge(): case_edges = [e for e in r["edges"] if e["relation"] == "case_of"] assert len(case_edges) >= 2 +def test_swift_enum_associated_value_type_emits_references(): + r = extract_swift(FIXTURES / "sample.swift") + assert ("NetworkError", "Config") in _edge_labels(r, "references", "type") + def test_swift_finds_deinit(): r = extract_swift(FIXTURES / "sample.swift") assert any("deinit" in l for l in _labels(r))