graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Add C# inheritance/interface extraction to AST extractor

Open skoodster opened this issue 7 months ago • 1 comments

Problem

The C# extractor (extract.py) handles class declarations, method declarations, imports (using directives), and namespaces — but does not extract inheritance relationships from base_list nodes in the tree-sitter AST.

Python and Swift extractors already handle inheritance (lines 704-744 in v0.3.7), but C# is missing the equivalent.

This means for a C# codebase, the graph has no inherits edges — class hierarchies are invisible.

Impact

Tested on a 6,100-file decompiled C# codebase (Unity/VaM). Without the patch: 0 inheritance edges. With the patch: 3,957 inheritance edges discovered, enabling queries like "what inherits from JSONStorable" and "show the trigger class hierarchy."

Fix

Add a base_list handler for C# between the Python and Swift inheritance blocks in _extract_generic(). The tree-sitter C# AST structure is:

class_declaration
  identifier: "MyClass"
  base_list
    :: ":"
    identifier: "BaseClass"          # simple inheritance
    ,: ","
    generic_name                     # generic base type
      identifier: "IList"
      type_argument_list: ...
    ,: ","
    identifier: "IDisposable"        # interface implementation

The patch handles both identifier (simple bases) and generic_name (generic bases like List<T>) children of base_list:

# C#-specific: inheritance / interface implementation
if config.ts_module == "tree_sitter_c_sharp":
    for child in node.children:
        if child.type == "base_list":
            for sub in child.children:
                if sub.type in ("identifier", "generic_name"):
                    if sub.type == "generic_name":
                        name_child = sub.child_by_field_name("name")
                        base = _read_text(name_child, source) if name_child else _read_text(sub.children[0], source)
                    else:
                        base = _read_text(sub, source)
                    base_nid = _make_id(stem, base)
                    if base_nid not in seen_ids:
                        base_nid = _make_id(base)
                        if base_nid not in seen_ids:
                            nodes.append({
                                "id": base_nid,
                                "label": base,
                                "file_type": "code",
                                "source_file": "",
                                "source_location": "",
                            })
                            seen_ids.add(base_nid)
                    add_edge(class_nid, base_nid, "inherits", line)

This mirrors the existing Python (superclasses field) and Swift (inheritance_specifier node) patterns.

Tested on

  • graphifyy 0.3.7
  • Python 3.14
  • tree-sitter-c-sharp 0.23.1
  • 6,100 .cs files (decompiled Unity C#)
  • Produces 3,957 inheritance edges with zero false positives

Happy to submit a PR if preferred.

skoodster avatar Apr 07 '26 18:04 skoodster

Implemented in v0.3.8. Added base_list handling in _extract_generic() for C# — handles both identifier (simple base types) and generic_name (e.g. IList<T>) children. Mirrors the existing Python/Swift inheritance patterns. 2 new tests: test_csharp_inherits_edge and test_csharp_inherits_iprocessor.

safishamsi avatar Apr 07 '26 19:04 safishamsi