Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(graph): emit inheritance for JavaScript classes (#1636)

Raghav Chamadiya4e55753a80d1
diff --git a/packages/core/src/repowise/core/ingestion/extractors/heritage/ts_js.py b/packages/core/src/repowise/core/ingestion/extractors/heritage/ts_js.py
index 20f48ef67..8648f6e6d 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/heritage/ts_js.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/heritage/ts_js.py
@@ -27,15 +27,35 @@ def _type_clause(
out.append(HeritageRelation(child_name=name, parent_name=parent, kind=kind, line=line))
+_CLAUSE_KINDS = {"extends_clause": "extends", "implements_clause": "implements"}
+
+# What a base may be spelled as when it sits directly under the heritage node.
+# `extends` takes any expression there, but only these two can name a class;
+# anything else is the grammar reading something it does not understand, which
+# is what a type annotation in a .js file looks like to it.
+_UNWRAPPED_BASE_TYPES = frozenset({"identifier", "member_expression"})
+
+
def _class_heritage(
heritage: Node, name: str, line: int, src: str, out: list[HeritageRelation]
) -> None:
"""``class Foo extends Bar implements IFoo, IBar``."""
- for clause in heritage.children:
- if clause.type == "extends_clause":
- _type_clause(clause, name, line, src, "extends", "extends", out)
- elif clause.type == "implements_clause":
- _type_clause(clause, name, line, src, "implements", "implements", out)
+ clauses = [child for child in heritage.children if child.type in _CLAUSE_KINDS]
+ for clause in clauses:
+ kind = _CLAUSE_KINDS[clause.type]
+ _type_clause(clause, name, line, src, kind, kind, out)
+ if clauses:
+ return
+ # TypeScript's grammar wraps the base in a clause node; JavaScript's puts
+ # the keyword and the base directly under the heritage node.
+ for child in heritage.children:
+ if child.type not in _UNWRAPPED_BASE_TYPES:
+ continue
+ parent = bare_type_name(node_text(child, src))
+ if parent:
+ out.append(
+ HeritageRelation(child_name=name, parent_name=parent, kind="extends", line=line)
+ )
def _extract_ts_js_heritage(
diff --git a/tests/unit/ingestion/test_heritage_type_names.py b/tests/unit/ingestion/test_heritage_type_names.py
index 4c0aab7f8..5351812cc 100644
--- a/tests/unit/ingestion/test_heritage_type_names.py
+++ b/tests/unit/ingestion/test_heritage_type_names.py
@@ -11,9 +11,8 @@ to expose: type arguments survived into the parent name in six languages, a
qualifier was emitted as a parent in its own right in three, and two dropped a
qualified parent entirely.
-Two languages still produce no edge, and neither is a type-name defect. Pascal's
-grammar is not installed here. JavaScript's extractor looks for a clause node
-only TypeScript's grammar emits.
+Pascal still produces no edge, and that is not a type-name defect: its grammar
+is not installed here.
"""
from __future__ import annotations
@@ -96,9 +95,9 @@ CASES: list[tuple[str, str, str, str, list[tuple[str, str]]]] = [
"ns.Both<Arg>",
[("Both", "extends")],
),
- # --- javascript: a grammar-shape gap, not a type-name one --------------
- ("javascript", "js", "class Child extends {p} {{}}\n", "Bare", []),
- ("javascript", "js", "class Child extends {p} {{}}\n", "ns.Qual", []),
+ # --- javascript: no type arguments to mishandle -------------------------
+ ("javascript", "js", "class Child extends {p} {{}}\n", "Bare", [("Bare", "extends")]),
+ ("javascript", "js", "class Child extends {p} {{}}\n", "ns.Qual", [("Qual", "extends")]),
# --- swift -------------------------------------------------------------
("swift", "swift", "class Child: {p} {{}}\n", "Bare", [("Bare", "extends")]),
("swift", "swift", "class Child: {p} {{}}\n", "Gen<Arg>", [("Gen", "extends")]),