Skip to content

Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.

fix: return fresh arrays from analyzeFileFull degraded paths; add tests

Lum110427ac76a95e8c
diff --git a/understand-anything-plugin/packages/core/src/__tests__/plugin-registry.test.ts b/understand-anything-plugin/packages/core/src/__tests__/plugin-registry.test.ts
index 39fec826..04c935ad 100644
--- a/understand-anything-plugin/packages/core/src/__tests__/plugin-registry.test.ts
+++ b/understand-anything-plugin/packages/core/src/__tests__/plugin-registry.test.ts
@@ -112,6 +112,38 @@ describe("PluginRegistry", () => {
expect(result).toBeNull();
});
+ it("analyzeFileFull delegates when the plugin implements it", () => {
+ const registry = new PluginRegistry();
+ const plugin = createMockPlugin("ts-plugin", ["typescript"]);
+ plugin.analyzeFileFull = () => ({
+ structure: {
+ ...emptyAnalysis,
+ functions: [{ name: "hello", lineRange: [1, 5], params: [] }],
+ },
+ callGraph: [{ caller: "hello", callee: "world", lineNumber: 2 }],
+ });
+ registry.register(plugin);
+
+ const result = registry.analyzeFileFull("src/test.ts", "const x = 1;");
+ expect(result).not.toBeNull();
+ expect(result!.structure.functions).toHaveLength(1);
+ expect(result!.callGraph).toHaveLength(1);
+ });
+
+ it("analyzeFileFull returns null when the plugin lacks the method (caller falls back)", () => {
+ const registry = new PluginRegistry();
+ registry.register(createMockPlugin("ts-plugin", ["typescript"]));
+ const result = registry.analyzeFileFull("src/test.ts", "const x = 1;");
+ expect(result).toBeNull();
+ });
+
+ it("analyzeFileFull returns null for unsupported files", () => {
+ const registry = new PluginRegistry();
+ registry.register(createMockPlugin("ts-plugin", ["typescript"]));
+ const result = registry.analyzeFileFull("main.py", "print('hello')");
+ expect(result).toBeNull();
+ });
+
it("unregister rebuilds language map correctly", () => {
const registry = new PluginRegistry();
const plugin1 = createMockPlugin("plugin1", ["typescript", "javascript"]);
diff --git a/understand-anything-plugin/packages/core/src/plugins/tree-sitter-plugin.test.ts b/understand-anything-plugin/packages/core/src/plugins/tree-sitter-plugin.test.ts
index 7595f338..73cd41b7 100644
--- a/understand-anything-plugin/packages/core/src/plugins/tree-sitter-plugin.test.ts
+++ b/understand-anything-plugin/packages/core/src/plugins/tree-sitter-plugin.test.ts
@@ -294,6 +294,72 @@ function main() {
});
});
+ describe("analyzeFileFull", () => {
+ const code = `
+import { helper } from "./helper";
+
+export function greet(name: string): string {
+ return formatMessage("Hello " + name);
+}
+
+function formatMessage(msg: string): string {
+ return msg.trim();
+}
+
+export class Greeter {
+ greet(name: string): string {
+ return greet(name);
+ }
+}
+`;
+
+ it("produces exactly the same output as analyzeFile + extractCallGraph", () => {
+ const separate = {
+ structure: plugin.analyzeFile("test.ts", code),
+ callGraph: plugin.extractCallGraph!("test.ts", code),
+ };
+ const full = plugin.analyzeFileFull("test.ts", code);
+
+ expect(full).toEqual(separate);
+ // Guard against vacuous equality — both sides must be non-trivial
+ expect(full.structure.functions.length).toBeGreaterThan(0);
+ expect(full.structure.classes.length).toBeGreaterThan(0);
+ expect(full.structure.imports.length).toBeGreaterThan(0);
+ expect(full.callGraph.length).toBeGreaterThan(0);
+ });
+
+ it("is stable across repeated calls (cached parser reuse)", () => {
+ const first = plugin.analyzeFileFull("test.ts", code);
+ const second = plugin.analyzeFileFull("test.ts", code);
+ expect(second).toEqual(first);
+ });
+
+ it("returns empty results for unsupported extensions", () => {
+ const full = plugin.analyzeFileFull("styles.xyz", "body { color: red; }");
+ expect(full.structure).toEqual({
+ functions: [],
+ classes: [],
+ imports: [],
+ exports: [],
+ });
+ expect(full.callGraph).toEqual([]);
+ });
+
+ it("returns fresh arrays per call — mutating one result cannot leak into the next", () => {
+ const first = plugin.analyzeFileFull("styles.xyz", "whatever");
+ first.structure.functions.push({
+ name: "injected",
+ lineRange: [1, 1],
+ params: [],
+ });
+ first.callGraph.push({ caller: "a", callee: "b", lineNumber: 1 });
+
+ const second = plugin.analyzeFileFull("styles.xyz", "whatever");
+ expect(second.structure.functions).toEqual([]);
+ expect(second.callGraph).toEqual([]);
+ });
+ });
+
describe("plugin metadata", () => {
it("should have correct name", () => {
expect(plugin.name).toBe("tree-sitter");
diff --git a/understand-anything-plugin/packages/core/src/plugins/tree-sitter-plugin.ts b/understand-anything-plugin/packages/core/src/plugins/tree-sitter-plugin.ts
index 4a30b944..6b453fca 100644
--- a/understand-anything-plugin/packages/core/src/plugins/tree-sitter-plugin.ts
+++ b/understand-anything-plugin/packages/core/src/plugins/tree-sitter-plugin.ts
@@ -227,12 +227,12 @@ export class TreeSitterPlugin implements AnalyzerPlugin {
return parser;
}
- private static readonly EMPTY_STRUCTURE: StructuralAnalysis = {
- functions: [],
- classes: [],
- imports: [],
- exports: [],
- };
+ // Fresh object AND fresh arrays on every call — a shared static would leak
+ // the same array instances to every caller, so one caller mutating its
+ // result would corrupt everyone else's.
+ private static emptyStructure(): StructuralAnalysis {
+ return { functions: [], classes: [], imports: [], exports: [] };
+ }
analyzeFile(
filePath: string,
@@ -278,12 +278,12 @@ export class TreeSitterPlugin implements AnalyzerPlugin {
): { structure: StructuralAnalysis; callGraph: CallGraphEntry[] } {
const parser = this.getParser(filePath);
if (!parser) {
- return { structure: { ...TreeSitterPlugin.EMPTY_STRUCTURE }, callGraph: [] };
+ return { structure: TreeSitterPlugin.emptyStructure(), callGraph: [] };
}
const tree = parser.parse(content);
if (!tree) {
- return { structure: { ...TreeSitterPlugin.EMPTY_STRUCTURE }, callGraph: [] };
+ return { structure: TreeSitterPlugin.emptyStructure(), callGraph: [] };
}
const langKey = this.languageKeyFromPath(filePath);
@@ -291,7 +291,7 @@ export class TreeSitterPlugin implements AnalyzerPlugin {
const structure = extractor
? extractor.extractStructure(tree.rootNode)
- : { ...TreeSitterPlugin.EMPTY_STRUCTURE };
+ : TreeSitterPlugin.emptyStructure();
const callGraph = extractor ? extractor.extractCallGraph(tree.rootNode) : [];
tree.delete();