test(graph): add Lua extractor + whitelist-discovery tests

Address maintainer review on PR #67.

- Add a dedicated `describe("Lua")` block locking in qualified
  `Table.method` / `T:m()` names, `local function`, both
  `T.f = function() … end` and `local f = function() … end` assignment
  forms, call attribution to the enclosing function, top-level calls
  falling back to `<module>`, and dotted-callee resolution.
- Move the former "handles Lua via regex fallback" test out of the
  regex-fallback block (Lua now routes to extractFromLua) and give it
  real assertions; drop "Lua" from that block's title.
- Add a whitelist `.gitignore` regression test (`/*` then `!/src/`)
  asserting files under the re-included dir are discovered; export
  getGraphableFiles so the test can exercise discovery directly.
- Match the dynamic-grammar style for the Lua parse call
  (`parse("lua" as unknown as Lang, source)`).
This commit is contained in:
Matteo Di Mattia
2026-06-09 19:56:09 +02:00
parent d4bbb6ca1c
commit 8be929a1c2
4 changed files with 150 additions and 17 deletions
+1 -1
View File
@@ -579,7 +579,7 @@ export function getAstGrepLang(ext: string): Lang | string | null {
* Get all source files in a project for graph analysis.
* Includes files with known AST grammars and any extra extensions.
*/
async function getGraphableFiles(
export async function getGraphableFiles(
projectPath: string,
extraExts?: Set<string>,
): Promise<string[]> {
+1 -1
View File
@@ -178,7 +178,7 @@ function extractFromLua(
language: string,
moduleSym: SymbolNode,
): ExtractedSymbols {
const root = parse("lua", source).root();
const root = parse("lua" as unknown as Lang, source).root();
const symbols: SymbolNode[] = [moduleSym];
const scopes: ScopeFrame[] = [];
const NAME = new Set(["dot_index_expression", "method_index_expression", "identifier"]);
+45
View File
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Giancarlo Erra - Altaire Limited
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { ensureDynamicLanguages, getGraphableFiles } from "../../src/services/code-graph.js";
// Regression for the whitelist .gitignore discovery fix: a `/*` then `!/src/`
// pattern ignores everything at the root but re-includes `src/`. The old walk
// passed `src` (no trailing slash) to shouldIgnore, which `/*` matched, so the
// walk bailed and produced an empty graph. Passing `src/` lets it descend and
// the files under the re-included directory are actually picked up.
describe("getGraphableFiles — whitelist .gitignore", () => {
let root: string;
beforeAll(() => {
ensureDynamicLanguages();
root = fs.mkdtempSync(path.join(os.tmpdir(), "socraticode-discovery-"));
fs.mkdirSync(path.join(root, "src"), { recursive: true });
fs.writeFileSync(path.join(root, ".gitignore"), "/*\n!/src/\n");
fs.writeFileSync(
path.join(root, "src", "mod.lua"),
"local function f()\n return 1\nend\nreturn f\n",
);
// A root-level file the `/*` pattern should keep ignored.
fs.writeFileSync(path.join(root, "ignored.lua"), "return 1\n");
});
afterAll(() => {
try {
fs.rmSync(root, { recursive: true, force: true });
} catch {
// ignore cleanup errors
}
});
it("descends into re-included src/ and discovers its files", async () => {
const files = await getGraphableFiles(root);
expect(files).toContain("src/mod.lua");
// The `/*` pattern still ignores top-level entries that are not re-included.
expect(files).not.toContain("ignored.lua");
});
});
+103 -15
View File
@@ -370,7 +370,109 @@ main() {
});
});
describe("Regex fallback (Dart, Lua, Svelte, Vue, unknown)", () => {
describe("Lua", () => {
it("extracts namespace-table, method, local, and assignment function forms", () => {
const src = `
local T = {}
function T.method(a)
return a
end
function T:m()
return self
end
local function helper()
return 1
end
T.f = function()
return 2
end
return T
`;
const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua");
const names = out.symbols.map((s) => s.name);
const qnames = out.symbols.map((s) => s.qualifiedName);
expect(names).toContain("<module>");
// qualified Table.method / T:m() forms resolve to precise method symbols
expect(qnames).toContain("T.method");
expect(qnames).toContain("T:m");
// `local function` keeps its bare name
expect(names).toContain("helper");
// `T.f = function() … end` assignment form
expect(qnames).toContain("T.f");
const kinds = out.symbols.filter((s) => s.qualifiedName === "T.method").map((s) => s.kind);
expect(kinds).toContain("method");
// colon-call form is also a method; the plain local function is not
expect(out.symbols.find((s) => s.qualifiedName === "T:m")?.kind).toBe("method");
expect(out.symbols.find((s) => s.qualifiedName === "helper")?.kind).toBe("function");
});
it("attributes calls to the enclosing function", () => {
const src = `
function greet(name)
return "hi " .. name
end
local function helper()
return greet("x")
end
`;
const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua");
expect(out.symbols.some((s) => s.name === "<module>")).toBe(true);
const greetCall = out.rawCalls.find((c) => c.calleeName === "greet");
expect(greetCall).toBeDefined();
expect(greetCall?.callerId).toContain("::helper#");
});
it("extracts the `local f = function() … end` assignment form", () => {
const src = `
local f = function()
return 1
end
local g = function()
return f()
end
`;
const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua");
const names = out.symbols.map((s) => s.name);
expect(names).toContain("f");
expect(names).toContain("g");
expect(out.symbols.find((s) => s.qualifiedName === "f")?.kind).toBe("function");
// call to `f` lives inside `g`, so it is attributed to `g`
const fCall = out.rawCalls.find((c) => c.calleeName === "f");
expect(fCall?.callerId).toContain("::g#");
});
it("attributes top-level calls to <module> and resolves dotted callees", () => {
const src = `
local M = require("mod")
M.setup()
local function run()
M.start()
end
`;
const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua");
// calls outside any function fall back to the synthetic <module> scope
const requireCall = out.rawCalls.find((c) => c.calleeName === "require");
expect(requireCall?.callerId).toContain("::<module>#");
// dotted callee `M.setup` resolves to the trailing identifier
const setupCall = out.rawCalls.find((c) => c.calleeName === "setup");
expect(setupCall).toBeDefined();
expect(setupCall?.callerId).toContain("::<module>#");
// `M.start()` lives inside `run`, so it is attributed there
const startCall = out.rawCalls.find((c) => c.calleeName === "start");
expect(startCall?.callerId).toContain("::run#");
});
});
describe("Regex fallback (Dart, Svelte, Vue, unknown)", () => {
it("handles Dart via regex fallback", () => {
const src = `
String greet(String name) {
@@ -389,20 +491,6 @@ class Foo {
expect(names.length).toBeGreaterThanOrEqual(1);
});
it("handles Lua via regex fallback", () => {
const src = `
function greet(name)
return "hi " .. name
end
local function helper()
return greet("x")
end
`;
const out = extractSymbolsAndCalls(src, "lua" as unknown as Lang, ".lua", "init.lua");
expect(out.symbols.some((s) => s.name === "<module>")).toBe(true);
});
it("handles unknown language without throwing", () => {
const src = "some random text\nwith no recognizable structure";
const out = extractSymbolsAndCalls(