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.

feat(core): expose strict structural parsing evidence

Lum1104a630b4219a28
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 73cd41b7..a0a324c5 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
@@ -360,6 +360,25 @@ export class Greeter {
});
});
+ describe("analyzeFileStrict", () => {
+ it("distinguishes unsupported syntax and parse errors from valid empty files", () => {
+ expect(plugin.analyzeFileStrict("test.xyz", "function f() {}").status).toBe("unsupported");
+ expect(plugin.analyzeFileStrict("test.ts", "class Broken {").status).toBe("failed");
+ const empty = plugin.analyzeFileStrict("test.ts", "// intentionally empty\n");
+ expect(empty.status).toBe("succeeded");
+ expect(empty.structure?.functions).toEqual([]);
+ });
+
+ it("uses the same extractor and preserves evidence of unextracted members", () => {
+ const code = "class A { run() {} callback = () => {}; }";
+ const strict = plugin.analyzeFileStrict("test.ts", code);
+ expect(strict.status).toBe("succeeded");
+ expect(strict.structure).toEqual(plugin.analyzeFile("test.ts", code));
+ expect(strict.structure?.classes[0].methods).toEqual(["run"]);
+ expect(strict.leafTexts).toContain("callback");
+ });
+ });
+
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 6b453fca..3c3f7c34 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
@@ -263,6 +263,52 @@ export class TreeSitterPlugin implements AnalyzerPlugin {
return result;
}
+ /**
+ * Evidence for destructive structural comparisons. Unlike analyzeFile's
+ * best-effort contract, an unavailable grammar or a recovered syntax error
+ * must never look like a successfully parsed file with no symbols.
+ * Leaf text lets callers detect names in syntax the extractor does not yet
+ * understand, without introducing a second parser or regex extractor.
+ */
+ analyzeFileStrict(
+ filePath: string,
+ content: string,
+ ): {
+ status: "succeeded" | "unsupported" | "failed";
+ structure: StructuralAnalysis | null;
+ leafTexts: string[];
+ } {
+ let tree: ReturnType<TreeSitterParser["parse"]> = null;
+ try {
+ const parser = this.getParser(filePath);
+ const langKey = this.languageKeyFromPath(filePath);
+ const extractor = langKey ? this.getExtractor(langKey) : null;
+ if (!parser || !extractor) {
+ return { status: "unsupported", structure: null, leafTexts: [] };
+ }
+ tree = parser.parse(content);
+ if (!tree || tree.rootNode.hasError) {
+ return { status: "failed", structure: null, leafTexts: [] };
+ }
+ const leafTexts = new Set<string>();
+ const pending = [tree.rootNode];
+ while (pending.length > 0) {
+ const node = pending.pop()!;
+ if (node.childCount === 0) leafTexts.add(node.text);
+ else pending.push(...node.children);
+ }
+ return {
+ status: "succeeded",
+ structure: extractor.extractStructure(tree.rootNode),
+ leafTexts: [...leafTexts],
+ };
+ } catch {
+ return { status: "failed", structure: null, leafTexts: [] };
+ } finally {
+ tree?.delete();
+ }
+ }
+
/**
* Parse the file ONCE and return both structural analysis and the call
* graph. `extract-structure.mjs` runs `analyzeFile` then `extractCallGraph`