Skip to content

TypeScript: interface extends and same-file class extends not captured as inherits edges (walker only inspects class_heritage) #1095

Closed

Description

@felipeathayde

Summary

After the TypeScript class-heritage extractor was added (class_heritage handling, present since ~v0.8.18), TS inheritance capture is still partial. Two cases produce no inherits/implements edge on v0.8.26:

  1. interface A extends B (interface-to-interface heritage) -- never captured.
  2. class X extends Y where Y is declared in the same file (only an imported superclass produces an edge).

class X extends ImportedClass and class X implements ImportedInterface work correctly.

Root cause (interface case)

_ts_walk_class_members in graphify/extract.py (the function that emits the heritage facts) only inspects the class_heritage child node:

# extract.py ~line 6686 (v0.8.26)
for child in class_node.children:
    if child.type == "class_heritage":
        for clause in child.children:
            if clause.type == "extends_clause":
                ... "inherits" ...
            elif clause.type == "implements_clause":
                ... "implements" ...

In tree-sitter-typescript, only class_declaration / abstract_class_declaration produce a class_heritage node. An interface_declaration represents its heritage with a different node (extends_type_clause), which does not appear anywhere in extract.py.

So even though interface_declaration is dispatched into the walker (the dispatch set at ~line 6899 includes class_declaration, abstract_class_declaration, interface_declaration), the walker finds no class_heritage child and emits nothing. Verified by parsing interface Derived extends Base {} with tree-sitter-typescript: the node tree contains no class_heritage child (only class X extends Y produces one).

Reproduce

// a.ts
export interface Base { x: number; }
export interface Derived extends Base { y: number; }   // -> expected inherits edge, got none

class Animal {}
class Dog extends Animal {}                            // same-file -> expected inherits edge, got none

import { Imported } from "./b";
class Cat extends Imported {}                           // imported -> edge IS emitted (works)

graphify update <dir> then inspect graphify-out/graph.json for links[].relation == "inherits".

Suggested fix

  1. Handle the interface heritage node (extends_type_clause) in _ts_walk_class_members, mirroring the extends_clause path, emitting inherits for each entry.
  2. For same-file class heritage: the superclass is not resolved unless it was already seen as a node; consider resolving same-file declarations so class X extends LocalClass also emits an edge.

Context

Inheritance support was added for C++/C#/Java when reported (#435 etc.); the TS interface/same-file gap appears to have no dedicated issue yet.

Environment

  • graphify (PyPI graphifyy) v0.8.26
  • tree-sitter-typescript via the bundled grammar

Activity

  1. safishamsi commented on May 31, 2026

    @safishamsi
    Member

    Fixed in 88a8e3b, shipping in the next release. Your root-cause analysis was exactly right on both cases @felipeathayde - confirmed the extends_type_clause node by parsing with the bundled grammar.

    Case 1: interface extends

    The walker only inspected class_heritage children, but interface heritage in tree-sitter-typescript is a separate extends_type_clause node (a direct child of interface_declaration, not wrapped in class_heritage). Added an extends_type_clause branch in _ts_walk_class_members that emits inherits facts. The existing _ts_heritage_clause_entries helper was directly reusable - the base entries under extends_type_clause are the same node types (type_identifier, generic_type, ...), so multiple extends (interface M extends A, B) and generic bases (extends Base<T>) work for free.

    Case 2: same-file class/interface heritage

    As you diagnosed, the heritage fact was emitted - the gap was in resolution. The use-fact resolver only consulted the import table (local_aliases_by_file, built solely from facts.imports), so a same-file base with no import alias was dropped while an imported one resolved. Added a same-file fallback against the file's own symbol_nodes (already keyed (path, label), so no new registration needed). Import resolution still takes precedence.

    One refinement worth noting: I scoped the same-file fallback to inherits/implements only. The facts.uses list also carries same-file calls/type-uses, and those already resolve through the dedicated call-graph pass - widening the fallback to all relations produced duplicate edges. Heritage is the only relation that genuinely lacked a same-file path.

    Tests

    Added tests/test_ts_inheritance.py: interface single/multiple extends, generic base, same-file class extends, same-file implements, imported-interface extends, plus a regression guard that the originally-working imported-class case still emits its edge. Full suite green (1502 passed).

    Verified end-to-end via the CLI:

    src_models_derived -> src_models_base    [ inherits ]
    src_models_dog     -> src_models_animal  [ inherits ]
    src_models_person  -> src_models_walker  [ implements ]
    
  2. added a commit that references this issue on May 31, 2026
    88a8e3b
  3. added a commit that references this issue on Aug 13, 2026
    7822ba8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions