Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixes

- In Java and TypeScript, a class that implements or extends a parameterized type — `implements Store<String>`, `extends Base<Integer>`, an interface's `extends Store<T>`, `implements Store<string>` — is now linked to that type, so its methods get their interface-to-implementation links and `codegraph_explore` follows a call on the interface into the class. Before, such a supertype was read with its type arguments and matched nothing.
- In TypeScript, an awaited call with type arguments like `await fetchApi<User>("/me")` or `await api.get<T>(url)` now links to the function it calls. Before, the call was dropped, so typed fetch wrappers looked like they had no callers. (#2586)
- In React, a component rendered at module scope, in a top-level constant or inside a test's `it(…)` callback, like `createRoot(el).render(<App />)` or `render(<Card />)`, now counts as rendered there, so an app's entry reaches its root component and a component's tests show among its callers. (#2587)
- In Python, `import a.b as X` written inside a function now links its calls like `X.f()`, as the same import at module level already did. Thanks @Egrn for the report. (#2600)
Expand Down
103 changes: 103 additions & 0 deletions __tests__/generic-supertypes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* A class that implements or extends a parameterized type is linked to that
* type. The Java and TypeScript grammars wrap `Store<String>` in a
* `generic_type`, and the supertype clause reader took the whole text as the
* name — which the index holds nothing under. So `class MapStore implements
* Store<String>` had no `implements` edge, and none of the interface-impl
* links that follow from it; the same for `extends Base<Integer>`, an
* interface's `extends Store<T>`, and TypeScript's `implements Store<string>`.
* The reference now names the type before its arguments, on the wasm path
* and in the kernel alike. (A package- or namespace-qualified supertype,
* `extends app.Base`, `implements ns.Other`, stays unresolved as before,
* with or without arguments.)
*/
import { describe, it, expect, afterAll, beforeAll } from 'vitest';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { CodeGraph } from '../src';

let root = '';
let cg: CodeGraph;

beforeAll(async () => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-generic-supertypes-'));
const files: Record<string, string> = {
'src/Store.java': `package app;

public interface Store<T> {
void save(T entity);
}
`,
'src/Sub.java': `package app;

interface Sub<T> extends Store<T> {}
`,
'src/MapStore.java': `package app;

import java.io.Serializable;

public class MapStore implements Store<String>, Serializable {
public void save(String entity) {}
}
`,
'src/Base.java': `package app;

public class Base<T> {
public void init() {}
}
`,
'src/Derived.java': `package app;

public class Derived extends Base<Integer> {
public void init() {}
}
`,
'src/store.ts': `export interface Store<T> { save(entity: T): void }
export interface Named { name: string }
export class MapStore implements Store<string>, Named {
name = 'map';
save(entity: string) {}
}
`,
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
});

afterAll(() => {
cg?.close();
if (root) fs.rmSync(root, { recursive: true, force: true });
});

/** `kind -> qualified name` of the supertype edges of `name` in `file`. */
const supertypes = (file: string, name: string): string[] => {
const node = cg.getNodesInFile(file).find((n) => n.name === name && n.kind !== 'file')!;
expect(node, `${file} ${name}`).toBeDefined();
return cg.getOutgoingEdges(node.id).filter((e) => e.kind === 'extends' || e.kind === 'implements')
.map((e) => `${e.kind} -> ${cg.getNode(e.target)!.qualifiedName}`).sort();
};

describe('a parameterized supertype names its type (Java, TypeScript)', () => {
it('Java: implements Store<String>, extends Base<Integer>, interface extends Store<T>', () => {
expect(supertypes('src/MapStore.java', 'MapStore')).toEqual(['implements -> app::Store']);
expect(supertypes('src/Derived.java', 'Derived')).toEqual(['extends -> app::Base']);
expect(supertypes('src/Sub.java', 'Sub')).toEqual(['extends -> app::Store']);
});

it('Java: the interface method links its implementation, the base method its override', () => {
const save = cg.getNodesInFile('src/Store.java').find((n) => n.name === 'save')!;
expect(cg.getOutgoingEdges(save.id).filter((e) => e.kind === 'calls' && e.metadata?.synthesizedBy === 'interface-impl')
.map((e) => cg.getNode(e.target)!.qualifiedName)).toEqual(['app::MapStore::save']);
const init = cg.getNodesInFile('src/Base.java').find((n) => n.name === 'init')!;
expect(cg.getOutgoingEdges(init.id).filter((e) => e.kind === 'calls' && e.metadata?.synthesizedBy === 'interface-impl')
.map((e) => cg.getNode(e.target)!.qualifiedName)).toEqual(['app::Derived::init']);
});

it('TypeScript: implements Store<string>, beside a plain interface', () => {
expect(supertypes('src/store.ts', 'MapStore')).toEqual(['implements -> Named', 'implements -> Store']);
});
});
18 changes: 16 additions & 2 deletions codegraph-kernel/src/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ impl<'t> Walker<'t> {
None => child.named_child(0).into_iter().collect(),
};
for target in targets {
let name = self.text(target).to_string();
let name = self.supertype_name(target);
self.push_ref_at(class_row, &name, extends_kind, target);
}
}
Expand All @@ -1047,7 +1047,7 @@ impl<'t> Walker<'t> {
None => (0..child.named_child_count()).filter_map(|j| child.named_child(j)).collect(),
};
for iface in targets {
let name = self.text(iface).to_string();
let name = self.supertype_name(iface);
self.push_ref_at(class_row, &name, implements_kind, iface);
}
}
Expand All @@ -1056,6 +1056,20 @@ impl<'t> Walker<'t> {
}
}

/// supertypeName — a parameterized supertype (`generic_type`) by the name
/// before its type arguments: `Store` for `implements Store<String>`.
fn supertype_name(&self, node: Node<'t>) -> String {
if node.kind() == "generic_type" {
let named = (0..node.named_child_count())
.filter_map(|j| node.named_child(j))
.find(|c| c.kind() != "type_arguments");
if let Some(named) = named {
return self.text(named).to_string();
}
}
self.text(node).to_string()
}

/// extractDecoratorsFor — Java annotations live inside `modifiers`.
fn extract_decorators_for(&mut self, decl: Node<'t>, decorated_row: u32) {
for i in 0..decl.named_child_count() {
Expand Down
18 changes: 16 additions & 2 deletions codegraph-kernel/src/tsjs/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1851,14 +1851,14 @@ impl<'t> Walker<'t> {
// TS `extends_clause` (the other spellings are other grammars').
"extends_clause" | "superclass" | "base_clause" | "extends_interfaces" => {
if let Some(target) = child.named_child(0) {
let name = self.text(target).to_string();
let name = self.supertype_name(target);
self.push_ref(class_row, &name, extends_kind, target);
}
}
"implements_clause" | "class_interface_clause" | "super_interfaces" | "interfaces" => {
for j in 0..child.named_child_count() {
if let Some(iface) = child.named_child(j) {
let name = self.text(iface).to_string();
let name = self.supertype_name(iface);
self.push_ref(class_row, &name, implements_kind, iface);
}
}
Expand Down Expand Up @@ -1894,6 +1894,20 @@ impl<'t> Walker<'t> {
}
}

/// supertypeName — a parameterized supertype (`generic_type`) by the name
/// before its type arguments: `Store` for `implements Store<string>`.
fn supertype_name(&self, node: Node<'t>) -> String {
if node.kind() == "generic_type" {
let named = (0..node.named_child_count())
.filter_map(|j| node.named_child(j))
.find(|c| c.kind() != "type_arguments");
if let Some(named) = named {
return self.text(named).to_string();
}
}
self.text(node).to_string()
}

// --- type annotations (#381 — TS family only) ----------------------------------------------

pub(super) fn extract_type_annotations(&mut self, node: Node<'t>, from_row: u32) {
Expand Down
22 changes: 20 additions & 2 deletions src/extraction/tree-sitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ function extractNameRaw(node: SyntaxNode, source: string, extractor: LanguageExt
return '<anonymous>';
}

/**
* The type a Java or TypeScript supertype clause names, without its type
* arguments: `Store` for `implements Store<String>`, `a.Base` for `extends
* a.Base<Integer>`, `ns.Other` for TypeScript's `implements ns.Other<number>`.
* The grammars wrap a parameterized supertype in a `generic_type` whose first
* child is the name; the index holds the interface or class under that name,
* so the full text (`Store<String>`) matched nothing and a class implementing
* a generic interface had no `implements` edge, nor the interface-impl links
* that follow from it. Any other node is its own text, as before.
*/
function supertypeName(node: SyntaxNode, source: string): string {
if (node.type === 'generic_type') {
const named = node.namedChildren.find((c: SyntaxNode) => c.type !== 'type_arguments');
if (named) return getNodeText(named, source);
}
return getNodeText(node, source);
}

/**
* Resolve a Scala type node to its base type NAME for name-matching — unwrapping
* `generic_type` (`Monoid[Int]` → `Monoid`), taking the last segment of a
Expand Down Expand Up @@ -7484,7 +7502,7 @@ export class TreeSitterExtractor {
const targets = typeList ? typeList.namedChildren : [child.namedChild(0)];
for (const target of targets) {
if (target) {
const name = getNodeText(target, this.source);
const name = supertypeName(target, this.source);
this.unresolvedReferences.push({
fromNodeId: classId,
referenceName: name,
Expand Down Expand Up @@ -7551,7 +7569,7 @@ export class TreeSitterExtractor {
const targets = typeList ? typeList.namedChildren : child.namedChildren;
for (const iface of targets) {
if (iface) {
const name = getNodeText(iface, this.source);
const name = supertypeName(iface, this.source);
this.unresolvedReferences.push({
fromNodeId: classId,
referenceName: name,
Expand Down