дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
The TypeScript server reports errors only for open files, so a change that breaks an unopened file went unnoticed until Build. Problems › ⋮ gets TypeScript Project Errors (tsc --watch), off by default (project setting languages.typescript.watchProblems). When on, the IDE runs the project's own node_modules/.bin/tsc in watch mode for each root tsconfig.json and lists its errors under source tsc, parsed by Build's tsc parser. The watcher never writes into the project: build info goes to a temporary folder, and an error that Build and the watcher both report is listed once. The language smoke workflow step from the original branch is left to a separate change. Co-authored-by: wildcatDownstairs <[email protected]>
303 рядки
12 KiB
TypeScript
303 рядки
12 KiB
TypeScript
import { beforeEach, describe, expect, it } from "vitest";
|
|
import type { Diagnostic } from "@/bindings";
|
|
import { useProject } from "@/platform/project";
|
|
import {
|
|
applyDiagnosticsEvent,
|
|
clearProblems,
|
|
countSeverities,
|
|
currentProblemsPath,
|
|
fileDiagnostics,
|
|
filePaths,
|
|
installProblemsEvents,
|
|
mergeDiagnostics,
|
|
problemsReport,
|
|
profiledProblems,
|
|
projectCounts,
|
|
useProblems,
|
|
worstSeverity,
|
|
} from "./problemsStore";
|
|
import { problemDecorations } from "./decorations";
|
|
|
|
const d = (line: number, severity: Diagnostic["severity"] = "error", source: string | null = null): Diagnostic => ({
|
|
range: { start: { line, col: 0 }, end: { line, col: 1 } },
|
|
severity,
|
|
message: `line ${line}`,
|
|
source,
|
|
code: null,
|
|
});
|
|
|
|
describe("mergeDiagnostics", () => {
|
|
it("replaces per (file, source) and keeps other sources", () => {
|
|
let map = mergeDiagnostics(new Map(), "/a", "ra", [d(3), d(1)]);
|
|
map = mergeDiagnostics(map, "/a", "clippy", [d(2, "warning")]);
|
|
expect(fileDiagnostics(map, "/a").map((x) => [x.range.start.line, x.source])).toEqual([
|
|
[1, "ra"],
|
|
[2, "clippy"],
|
|
[3, "ra"],
|
|
]);
|
|
map = mergeDiagnostics(map, "/a", "ra", [d(5, "info")]);
|
|
expect(fileDiagnostics(map, "/a").map((x) => x.range.start.line)).toEqual([2, 5]);
|
|
});
|
|
|
|
it("drops files whose sources are all empty and is a no-op for unknown clears", () => {
|
|
const a = mergeDiagnostics(new Map(), "/a", "ra", [d(1)]);
|
|
expect(mergeDiagnostics(a, "/a", "other", [])).toBe(a);
|
|
expect(mergeDiagnostics(a, "/b", "ra", [])).toBe(a);
|
|
const cleared = mergeDiagnostics(a, "/a", "ra", []);
|
|
expect(cleared.has("/a")).toBe(false);
|
|
});
|
|
|
|
it("keeps an item's own source", () => {
|
|
const map = mergeDiagnostics(new Map(), "/a", "eslint", [d(1, "error", "eslint/no-undef")]);
|
|
expect(fileDiagnostics(map, "/a")[0]!.source).toBe("eslint/no-undef");
|
|
});
|
|
});
|
|
|
|
describe("counts", () => {
|
|
it("counts per severity", () => {
|
|
let map = mergeDiagnostics(new Map(), "/a", "ra", [d(1), d(2, "warning")]);
|
|
map = mergeDiagnostics(map, "/b", "ra", [d(1, "warning"), d(2, "hint")]);
|
|
expect(projectCounts(map)).toEqual({ error: 1, warning: 2, info: 0, hint: 1 });
|
|
expect(countSeverities(fileDiagnostics(map, "/b"))).toEqual({ error: 0, warning: 1, info: 0, hint: 1 });
|
|
expect(worstSeverity(fileDiagnostics(map, "/b"))).toBe("warning");
|
|
expect(worstSeverity([])).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("a problem two sources report", () => {
|
|
beforeEach(() => clearProblems());
|
|
|
|
it("is listed and counted once (Build's tsc step and the tsc watcher)", () => {
|
|
const counts = () => useProblems.getState().counts.error;
|
|
const listed = () => fileDiagnostics(useProblems.getState().map, "/p/b.ts").map((x) => x.source);
|
|
applyDiagnosticsEvent("/p/b.ts", "tsc", [d(1, "error", "tsc"), d(2, "error", "tsc")]);
|
|
applyDiagnosticsEvent("/p/b.ts", "build", [d(1, "error", "build")]);
|
|
expect(listed()).toEqual(["build", "tsc"]);
|
|
expect(counts()).toBe(2);
|
|
expect(projectCounts(useProblems.getState().map).error).toBe(2);
|
|
// Either source alone still shows it.
|
|
applyDiagnosticsEvent("/p/b.ts", "tsc", []);
|
|
expect(listed()).toEqual(["build"]);
|
|
expect(counts()).toBe(1);
|
|
applyDiagnosticsEvent("/p/b.ts", "build", []);
|
|
expect(listed()).toEqual([]);
|
|
expect(counts()).toBe(0);
|
|
});
|
|
|
|
it("keeps other sources as they are, and different problems on one line", () => {
|
|
let map = mergeDiagnostics(mergeDiagnostics(new Map(), "/a", "ra", [d(1)]), "/a", "eslint", [d(1)]);
|
|
expect(fileDiagnostics(map, "/a")).toHaveLength(2);
|
|
map = mergeDiagnostics(map, "/a", "build", [{ ...d(1), message: "another" }]);
|
|
map = mergeDiagnostics(map, "/a", "tsc", [d(1)]);
|
|
expect(fileDiagnostics(map, "/a")).toHaveLength(4);
|
|
});
|
|
});
|
|
|
|
describe("store + decorations", () => {
|
|
beforeEach(() => clearProblems());
|
|
|
|
it("applies events and decorates only files with errors", () => {
|
|
applyDiagnosticsEvent("/p/src/a.rs", "ra", [d(1), d(2, "warning")]);
|
|
applyDiagnosticsEvent("/p/src/b.rs", "ra", [d(1, "warning")]);
|
|
const deco = problemDecorations(useProblems.getState().map);
|
|
expect([...deco.keys()]).toEqual(["/p/src/a.rs"]);
|
|
expect(deco.get("/p/src/a.rs")).toEqual({ error: true, propagate: true, tooltip: "1 error, 1 warning" });
|
|
});
|
|
});
|
|
|
|
/** Entries copied by every `new Map(...)` constructed while `run` executes. */
|
|
function entriesCopied(run: () => void): number {
|
|
const Original = globalThis.Map;
|
|
let copied = 0;
|
|
class Counting<K, V> extends Original<K, V> {
|
|
constructor(entries?: Iterable<readonly [K, V]> | null) {
|
|
super(entries);
|
|
copied += this.size;
|
|
}
|
|
}
|
|
globalThis.Map = Counting as MapConstructor;
|
|
try {
|
|
run();
|
|
} finally {
|
|
globalThis.Map = Original;
|
|
}
|
|
return copied;
|
|
}
|
|
|
|
describe("diagnostics storm", () => {
|
|
beforeEach(() => clearProblems());
|
|
const FILES = 2000;
|
|
const path = (i: number) => `/p/src/File${i}.ts`;
|
|
|
|
it("applies an event without copying the files already in the store", () => {
|
|
// Copying every file's entry per event is FILES^2 / 2 = 2,000,000 entries; the bound is
|
|
// a few entries per event (the touched file's sources).
|
|
const copied = entriesCopied(() => {
|
|
for (let i = 0; i < FILES; i++) applyDiagnosticsEvent(path(i), "lsp", [d(i % 50), d(1, "warning")]);
|
|
});
|
|
expect(copied).toBeLessThan(FILES * 4);
|
|
const { map, raw, counts } = useProblems.getState();
|
|
expect(map.size).toBe(FILES);
|
|
expect(raw.size).toBe(FILES);
|
|
expect(counts).toEqual({ error: FILES, warning: FILES, info: 0, hint: 0 });
|
|
});
|
|
|
|
it("rebuilds the profiled map without copying it per file", () => {
|
|
for (let i = 0; i < FILES; i++) applyDiagnosticsEvent(path(i), "lsp", [d(i % 50)]);
|
|
const { raw } = useProblems.getState();
|
|
let out: ReturnType<typeof profiledProblems> | undefined;
|
|
const copied = entriesCopied(() => {
|
|
out = profiledProblems(raw, { "lsp:lsp": { severity: "weakWarning" } });
|
|
});
|
|
expect(copied).toBeLessThan(FILES * 4);
|
|
expect(out!.size).toBe(FILES);
|
|
});
|
|
|
|
it("publishes a new map exactly when the content changed", () => {
|
|
applyDiagnosticsEvent(path(1), "lsp", [d(1)]);
|
|
const first = useProblems.getState();
|
|
applyDiagnosticsEvent(path(1), "other", []); // nothing to clear
|
|
expect(useProblems.getState()).toBe(first);
|
|
|
|
applyDiagnosticsEvent(path(2), "lsp", [d(1)]);
|
|
const second = useProblems.getState();
|
|
expect(second.map).not.toBe(first.map);
|
|
expect(second.raw).not.toBe(first.raw);
|
|
expect(second.map.get(path(1))).toBe(first.map.get(path(1))); // untouched file keeps its sources
|
|
|
|
applyDiagnosticsEvent(path(2), "lsp", []);
|
|
const third = useProblems.getState();
|
|
expect(third.map).not.toBe(second.map);
|
|
expect(third.map.has(path(2))).toBe(false);
|
|
expect(third.counts).toEqual({ error: 1, warning: 0, info: 0, hint: 0 });
|
|
});
|
|
});
|
|
|
|
describe("profiledProblems", () => {
|
|
it("applies the inspection profile per producer", () => {
|
|
let raw = mergeDiagnostics(new Map(), "/a", "ra", [d(1), d(2, "warning")]);
|
|
raw = mergeDiagnostics(raw, "/b", "eslint", [d(3)]);
|
|
const out = profiledProblems(raw, { "lsp:eslint": { enabled: false }, "lsp:ra": { severity: "weakWarning" } });
|
|
expect([...out.keys()]).toEqual(["/a"]);
|
|
expect(fileDiagnostics(out, "/a").map((x) => x.severity)).toEqual(["info", "info"]);
|
|
expect(fileDiagnostics(profiledProblems(raw, {}), "/b")).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe("local inspections (issue #42)", () => {
|
|
it("reach Problems and clear there too, with their inspection known", async () => {
|
|
const { installProblemsEvents, producerFor } = await import("./problemsStore");
|
|
const { setFileDiagnostics } = await import("@/editor/lsp/diagnostics");
|
|
const { diagnosticInspection } = await import("@/platform/inspections");
|
|
clearProblems();
|
|
const off = installProblemsEvents();
|
|
try {
|
|
setFileDiagnostics("/p/a.py", "spelling", [
|
|
{ ...d(0, "info"), message: "Typo: In word 'mesage'", source: "Spelling" },
|
|
]);
|
|
const [typo] = fileDiagnostics(useProblems.getState().map, "/p/a.py");
|
|
expect(typo).toMatchObject({ message: "Typo: In word 'mesage'", typo: true });
|
|
const producer = producerFor(useProblems.getState().map, "/p/a.py", typo!);
|
|
expect(producer).toBe("spelling");
|
|
expect(diagnosticInspection(producer!, typo!)?.id).toBe("builtin:spelling");
|
|
setFileDiagnostics("/p/a.py", "spelling", []);
|
|
expect(useProblems.getState().map.size).toBe(0);
|
|
} finally {
|
|
off();
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("currentProblemsPath", () => {
|
|
it("follows a non-text tab such as a notebook, else the text editor", () => {
|
|
expect(currentProblemsPath(null, { kind: "notebook", path: "/p/a.ipynb" })).toBe("/p/a.ipynb");
|
|
expect(currentProblemsPath("/p/old.py", { kind: "notebook", path: "/p/a.ipynb" })).toBe("/p/a.ipynb");
|
|
expect(currentProblemsPath("/p/a.py", { kind: "file", path: "/p/a.py" })).toBe("/p/a.py");
|
|
expect(currentProblemsPath("/p/a.py", null)).toBe("/p/a.py");
|
|
expect(currentProblemsPath(null, { kind: "welcome" })).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("filePaths", () => {
|
|
it("includes a notebook's cell documents", () => {
|
|
const map = new Map([
|
|
["/p/a.ipynb", new Map([["x", [d(0)]]])],
|
|
["vscode-notebook-cell:/p/a.ipynb#c1", new Map([["x", [d(1)]]])],
|
|
["vscode-notebook-cell:/p/my%20nb.ipynb#c2", new Map([["x", [d(2)]]])],
|
|
["vscode-notebook-cell:/C:/w/b.ipynb#c3", new Map([["x", [d(3)]]])],
|
|
["/p/a.py", new Map([["x", [d(4)]]])],
|
|
]);
|
|
expect(filePaths(map, "/p/a.ipynb")).toEqual(["/p/a.ipynb", "vscode-notebook-cell:/p/a.ipynb#c1"]);
|
|
expect(filePaths(map, "/p/my nb.ipynb")).toEqual(["vscode-notebook-cell:/p/my%20nb.ipynb#c2"]);
|
|
expect(filePaths(map, "C:/w/b.ipynb")).toEqual(["vscode-notebook-cell:/C:/w/b.ipynb#c3"]);
|
|
expect(filePaths(map, "/p/a.py")).toEqual(["/p/a.py"]);
|
|
expect(filePaths(map, "/p/none.py")).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("problemsReport", () => {
|
|
it("lists errors before warnings with their places, leaving out hints, up to a limit", () => {
|
|
let map = mergeDiagnostics(new Map(), "/p/src/a.ts", "tsc", [d(3, "warning", "ts"), d(0, "hint")]);
|
|
map = mergeDiagnostics(map, "/p/src/b.ts", "tsc", [d(9, "error"), d(1, "error")]);
|
|
expect(problemsReport(map, "/p")).toBe(
|
|
[
|
|
"src/b.ts:2:1: error: line 1 (tsc)",
|
|
"src/b.ts:10:1: error: line 9 (tsc)",
|
|
"src/a.ts:4:1: warning: line 3 (ts)",
|
|
].join("\n"),
|
|
);
|
|
expect(problemsReport(map, "/p", 1)).toBe("src/b.ts:2:1: error: line 1 (tsc)\n… 2 more");
|
|
expect(problemsReport(mergeDiagnostics(new Map(), "/p/a", "x", [d(0, "info")]), "/p")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("incremental bookkeeping", () => {
|
|
let seed = 7;
|
|
const random = (n: number) => (seed = (seed * 48271) % 2147483647) % n;
|
|
const SEVERITIES = ["error", "warning", "info", "hint"] as const;
|
|
const items = (): Diagnostic[] =>
|
|
Array.from({ length: random(5) }, () => ({
|
|
...d(random(3), SEVERITIES[random(4)]),
|
|
range: { start: { line: random(3), col: random(2) }, end: { line: 3, col: 0 } },
|
|
code: String(random(1000)),
|
|
}));
|
|
// The merge as it was before it was cached.
|
|
const fullMerge = (map: ReturnType<typeof useProblems.getState>["map"], path: string) =>
|
|
[...(map.get(path)?.values() ?? [])]
|
|
.flat()
|
|
.sort(
|
|
(a, b) =>
|
|
a.range.start.line - b.range.start.line ||
|
|
a.range.start.col - b.range.start.col ||
|
|
SEVERITIES.indexOf(a.severity) - SEVERITIES.indexOf(b.severity),
|
|
);
|
|
|
|
it("keeps the project counts and each file's merged list equal to a full rescan", () => {
|
|
clearProblems();
|
|
useProject.setState({ settings: {} as never });
|
|
const off = installProblemsEvents();
|
|
try {
|
|
for (let step = 0; step < 400; step++) {
|
|
if (step === 150)
|
|
useProject.setState({
|
|
settings: { inspections: { default: { "lsp:eslint": { enabled: false } } } } as never,
|
|
});
|
|
if (step === 250)
|
|
useProject.setState({
|
|
settings: { inspections: { default: { "lsp:ra": { severity: "weakWarning" } } } } as never,
|
|
});
|
|
if (step === 300) clearProblems();
|
|
const path = `/p/${random(12)}.rs`;
|
|
applyDiagnosticsEvent(path, ["ra", "eslint"][random(2)]!, items());
|
|
const { map, counts } = useProblems.getState();
|
|
expect(counts).toEqual(projectCounts(map));
|
|
for (const p of map.keys()) expect(fileDiagnostics(map, p)).toEqual(fullMerge(map, p));
|
|
}
|
|
} finally {
|
|
off();
|
|
useProject.setState({ settings: {} as never });
|
|
clearProblems();
|
|
}
|
|
});
|
|
});
|