/** * Tests of the editor's diagnostics (M3.3, `lint.ts`): conversion to * CodeMirror positions (CRLF, clamping, order, source and code), the view * plugin applying a path's diagnostics when a view shows the state and on * every publication, clearing, and colours from tokens only. */ import { diagnosticCount, forEachDiagnostic, type Diagnostic } from "@codemirror/lint"; import { EditorState, Text } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import { readFileSync } from "node:fs"; import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { clearDiagnostics, diagnosticsOf, lintExtensions, publishDiagnostics, toCodeMirror, type DiagnosticItem, } from "../../src/components/editor/lint"; /** Views created by a test, destroyed after it. */ let views: EditorView[] = []; /** * A diagnostic on one line. * * @param line - 0-based line. * @param from - start column. * @param to - end column. * @param extra - fields to override. */ function item(line: number, from: number, to: number, extra: Partial = {}): DiagnosticItem { return { range: { start: { line, column: from }, end: { line, column: to } }, severity: "error", message: `problem at ${String(line)}:${String(from)}`, ...extra, }; } /** Lets the plugin's microtask run. */ async function settle(): Promise { await Promise.resolve(); await Promise.resolve(); } /** * A view showing `doc` as `path`. * * @param path - project-relative path. * @param doc - the text. */ function viewOf(path: string, doc: string): EditorView { const view = new EditorView({ state: EditorState.create({ doc, extensions: lintExtensions(path) }), parent: document.body, }); views.push(view); return view; } /** * The diagnostics in a state. * * @param state - the editor state. */ function shown(state: EditorState): Diagnostic[] { const out: Diagnostic[] = []; forEachDiagnostic(state, (d, from, to) => out.push({ ...d, from, to })); return out; } afterEach(() => { for (const view of views) { view.destroy(); } views = []; clearDiagnostics(); }); describe("toCodeMirror", () => { it("converts lines and UTF-16 columns, CRLF-safe and clamped", () => { const doc = Text.of([" [d.from, d.to, d.severity])).toEqual([ [0, 3, "error"], [14, 16, "warning"], [18, 21, "hint"], [20, 20, "info"], ]); expect(out[1]?.source).toBe("phpactor W1"); expect(out[0]?.source).toBeUndefined(); }); }); describe("lintExtensions", () => { it("shows a file's diagnostics and follows every publication", async () => { publishDiagnostics("a.php", [item(1, 0, 4)]); const view = viewOf("a.php", " [d.from, d.to, d.message])).toEqual([[6, 10, "problem at 1:0"]]); expect(view.dom.querySelector(".cm-lintRange-error")).not.toBeNull(); expect(view.dom.querySelector(".cm-gutter-lint .cm-lint-marker-error")).not.toBeNull(); publishDiagnostics("a.php", [item(0, 0, 5, { severity: "warning" }), item(1, 5, 6)]); expect(diagnosticCount(view.state)).toBe(2); expect(view.dom.querySelector(".cm-lintRange-warning")).not.toBeNull(); publishDiagnostics("other.php", [item(0, 0, 1)]); expect(diagnosticCount(view.state)).toBe(2); publishDiagnostics("a.php", []); expect(diagnosticCount(view.state)).toBe(0); expect(diagnosticsOf("a.php")).toEqual([]); }); it("brings a background state up to date when it is shown again", async () => { const view = viewOf("a.php", " { publishDiagnostics("a.php", [item(0, 6, 10)]); const view = viewOf("a.php", " [d.from, d.to])).toEqual([[8, 12]]); }); it("clears every file on clearDiagnostics", async () => { publishDiagnostics("a.php", [item(0, 0, 1)]); const view = viewOf("a.php", " { const view = viewOf("a.php", " { publishDiagnostics("a.php", [item(0, 0, 1)]); }).not.toThrow(); }); it("adds nothing without a path", () => { expect(lintExtensions("")).toEqual([]); }); it("takes its colours from tokens only", () => { const source = readFileSync(join(import.meta.dirname, "..", "..", "src", "components", "editor", "lint.ts"), "utf8"); expect(source).not.toMatch(/#[0-9a-fA-F]{3,8}\b|rgba?\(|hsla?\(/); for (const colour of source.matchAll(/var\((--[a-z0-9-]+)\)/g)) { expect(colour[1]).toMatch(/^--lt-/); } }); });