import { describe, it, expect } from "vitest"; import { readdirSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { DiagnosticBag } from "../src/diagnostics.js"; describe("DiagnosticBag", () => { it("stores the code alongside severity/message/line/col", () => { const bag = new DiagnosticBag(); bag.error("KS4001", "Something went wrong", 3, 7); expect(bag.diagnostics).toEqual([{ code: "KS4001", severity: "error", message: "Something went wrong", line: 3, col: 7 }]); }); it("warning() stores its own code the same way", () => { const bag = new DiagnosticBag(); bag.warning("KS1001", "A non-fatal note", 1, 1); expect(bag.diagnostics[0]).toMatchObject({ code: "KS1001", severity: "warning" }); expect(bag.hasErrors).toBe(false); }); it("stores an optional fix suggestion, omitted entirely when not given", () => { const withFix = new DiagnosticBag(); withFix.error("KS4083", "Class 'Widget' has no member 'Vlaue'", 1, 1, "Did you mean 'Value'?"); expect(withFix.diagnostics[0].fix).toBe("Did you mean 'Value'?"); const withoutFix = new DiagnosticBag(); withoutFix.error("KS4001", "Something went wrong", 1, 1); expect(withoutFix.diagnostics[0]).toEqual({ code: "KS4001", severity: "error", message: "Something went wrong", line: 1, col: 1 }); expect("fix" in withoutFix.diagnostics[0]).toBe(false); }); it("format() includes a fix suggestion on its own line, when present", () => { const bag = new DiagnosticBag(); bag.error("KS4083", "Class 'Widget' has no member 'Vlaue'", 1, 1, "Did you mean 'Value'?"); const formatted = bag.format("this.Vlaue = 1;", "file.ks"); expect(formatted).toContain("fix: Did you mean 'Value'?"); }); it("toJSON() returns the file name alongside every diagnostic, fix suggestions included", () => { const bag = new DiagnosticBag(); bag.error("KS4083", "Class 'Widget' has no member 'Vlaue'", 1, 1, "Did you mean 'Value'?"); bag.warning("KS1001", "A non-fatal note", 2, 3); expect(bag.toJSON("file.ks")).toEqual({ file: "file.ks", diagnostics: [ { code: "KS4083", severity: "error", message: "Class 'Widget' has no member 'Vlaue'", line: 1, col: 1, fix: "Did you mean 'Value'?" }, { code: "KS1001", severity: "warning", message: "A non-fatal note", line: 2, col: 3 }, ], }); }); it("format() includes the code between severity and message", () => { const bag = new DiagnosticBag(); bag.error("KS4065", "Argument 2 has type 'string', expected 'number'", 1, 1); const formatted = bag.format("x", "file.ks"); expect(formatted).toContain("file.ks:1:1 - error KS4065: Argument 2 has type 'string', expected 'number'"); }); it("real compiler codes are unique across every emission site", () => { // A stable code is worthless if two different problems can share one -- // this is the actual source of truth check, not just a unit test of // DiagnosticBag's own plumbing. Scans every compiler source file (not // test fixtures) for `.error("KSxxxx", ...)` call sites. const srcDir = join(__dirname, "..", "src"); const codes: string[] = []; for (const file of readdirSync(srcDir)) { if (!file.endsWith(".ts")) continue; const text = readFileSync(join(srcDir, file), "utf-8"); for (const m of text.matchAll(/\.error\("(KS\d+)"/g)) codes.push(m[1]); } expect(codes.length).toBeGreaterThan(0); expect(new Set(codes).size).toBe(codes.length); }); });