fix(extract): emit inherits edge for JavaScript class extends (#1790)
A plain JavaScript `class Dog extends Animal` produced no inherits edge — only the TypeScript extends_clause/implements_clause shapes were handled, not the bare identifier directly under class_heritage that JS emits. Fall back to reading heritage identifiers when no clause is present, so JS class inheritance is captured; a dynamic base (mixin call) yields nothing and TS is not double-emitted.
This commit is contained in:
1 parent
b27f219392
commit
6b2dfa0bf2
2 files changed
+51
No files matched your search
@@ -1397,19 +1397,34 @@ def _ts_walk_class_members(class_node, source: bytes, path: Path, class_nid: str
|
||||
line = class_node.start_point[0] + 1
|
||||
for child in class_node.children:
|
||||
if child.type == "class_heritage":
|
||||
saw_clause = False
|
||||
for clause in child.children:
|
||||
if clause.type == "extends_clause":
|
||||
saw_clause = True
|
||||
for name in _ts_heritage_clause_entries(clause, source):
|
||||
facts.uses.append(
|
||||
_SymbolUseFact(path, class_nid, name, "inherits", "type",
|
||||
clause.start_point[0] + 1)
|
||||
)
|
||||
elif clause.type == "implements_clause":
|
||||
saw_clause = True
|
||||
for name in _ts_heritage_clause_entries(clause, source):
|
||||
facts.uses.append(
|
||||
_SymbolUseFact(path, class_nid, name, "implements", "type",
|
||||
clause.start_point[0] + 1)
|
||||
)
|
||||
if not saw_clause:
|
||||
# The JavaScript grammar carries the base directly under
|
||||
# class_heritage (`extends Base` -> [extends, identifier]) with no
|
||||
# extends_clause/implements_clause wrapper like the TypeScript
|
||||
# grammar. Treat the heritage node itself as the extends clause so
|
||||
# `class Derived extends Base {}` in a .js file still emits an
|
||||
# inherits edge. Mirrors the extends_type_clause branch below.
|
||||
for name in _ts_heritage_clause_entries(child, source):
|
||||
facts.uses.append(
|
||||
_SymbolUseFact(path, class_nid, name, "inherits", "type",
|
||||
child.start_point[0] + 1)
|
||||
)
|
||||
elif child.type == "extends_type_clause":
|
||||
# Interface heritage (`interface A extends B, C`) is an
|
||||
# extends_type_clause node, NOT a class_heritage. Its base entries
|
||||
|
||||
@@ -56,6 +56,42 @@ def test_class_extends_same_file(tmp_path):
|
||||
assert _has_inherits(result, "src/a.ts", "Dog", "src/a.ts", "Animal")
|
||||
|
||||
|
||||
def test_js_class_extends_same_file(tmp_path):
|
||||
"""`class Dog extends Animal {}` in a plain .js file must emit an inherits edge.
|
||||
|
||||
The JavaScript grammar stores the base identifier directly under the
|
||||
`class_heritage` node (no `extends_clause` wrapper as in the TypeScript
|
||||
grammar), so the heritage walk dropped the edge for .js classes while the
|
||||
equivalent .ts file (test_class_extends_same_file) already worked.
|
||||
"""
|
||||
f = _write(tmp_path / "src" / "a.js",
|
||||
"class Animal {}\n"
|
||||
"class Dog extends Animal {}\n")
|
||||
result = extract([f], cache_root=tmp_path)
|
||||
assert _has_inherits(result, "src/a.js", "Dog", "src/a.js", "Animal")
|
||||
|
||||
|
||||
def test_js_class_extends_imported(tmp_path):
|
||||
"""The JavaScript grammar path must retain import-guided resolution."""
|
||||
base = _write(tmp_path / "src" / "base.js", "export class Animal {}\n")
|
||||
derived = _write(tmp_path / "src" / "derived.js",
|
||||
"import { Animal } from './base.js';\n"
|
||||
"export class Dog extends Animal {}\n")
|
||||
result = extract([derived, base], cache_root=tmp_path)
|
||||
assert _has_inherits(result, "src/derived.js", "Dog", "src/base.js", "Animal")
|
||||
|
||||
|
||||
def test_js_dynamic_class_base_does_not_fabricate_inherits(tmp_path):
|
||||
"""A runtime base expression has no statically resolvable parent symbol."""
|
||||
f = _write(tmp_path / "src" / "a.js",
|
||||
"class Animal {}\n"
|
||||
"function mixin(base) { return class extends base {}; }\n"
|
||||
"class Dog extends mixin(Animal) {}\n")
|
||||
result = extract([f], cache_root=tmp_path)
|
||||
assert not _has_inherits(result, "src/a.js", "Dog", "src/a.js", "Animal")
|
||||
assert not _has_inherits(result, "src/a.js", "Dog", "src/a.js", "mixin")
|
||||
|
||||
|
||||
def test_interface_extends_generic_base_same_file(tmp_path):
|
||||
f = _write(tmp_path / "src" / "a.ts",
|
||||
"interface Base<T> { x: T; }\n"
|
||||
|
||||
Reference in new issue
Block a user