Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/typescript/src/api/diagnosticFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ function diagnosticPrefix(diagnostic: Diagnostic): string {
return diagnostic.source || "TS";
}

function flattenDiagnosticMessage(diagnostic: Diagnostic, newLine: string, indentLevel = 0): string {
function flattenDiagnosticMessage(diagnostic: Diagnostic, newLine: string, indentLevel = 0, baseIndent = ""): string {
let result = "";
if (indentLevel) {
result += newLine + " ".repeat(indentLevel);
result += newLine + baseIndent + " ".repeat(indentLevel);
}
result += diagnostic.text;
for (const child of diagnostic.messageChain ?? []) {
result += flattenDiagnosticMessage(child, newLine, indentLevel + 1);
result += flattenDiagnosticMessage(child, newLine, indentLevel + 1, baseIndent);
}
return result;
}
Expand Down Expand Up @@ -190,12 +190,16 @@ export function formatDiagnosticsWithColorAndContext(

if (diagnostic.relatedInformation?.length) {
for (const related of diagnostic.relatedInformation) {
output += host.getNewLine();
if (related.fileName && related.startPosition) {
output += host.getNewLine();
output += halfIndent + formatLocation(related, host);
output += " - " + flattenDiagnosticMessage(related, host.getNewLine());
output += formatCodeSpan(related, indent, foregroundColorEscapeCyan, host);
output += " - ";
}
else {
output += halfIndent;
}
output += flattenDiagnosticMessage(related, host.getNewLine(), 0, halfIndent);
output += formatCodeSpan(related, indent, foregroundColorEscapeCyan, host);
output += host.getNewLine();
}
}
Expand Down
97 changes: 97 additions & 0 deletions packages/typescript/test/diagnosticFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type FormatDiagnosticsHost,
formatDiagnosticsWithColorAndContext,
} from "@typescript/typescript/async";
import { toRootedFilePath } from "@typescript/typescript/path";
import assert from "node:assert";
import {
describe,
Expand Down Expand Up @@ -121,6 +122,102 @@ describe("diagnosticFormatter", { concurrency: areTestsFiltered() }, () => {
}
});

test("formats related information with or without source locations", () => {
const host: FormatDiagnosticsHost = {
getCurrentDirectory: () => "/project",
getCanonicalFileName: fileName => fileName,
getNewLine: () => "\r\n",
};
const diagnostic = {
category: 1,
code: 2322,
pos: 0,
end: 1,
text: "Primary error.",
};
const located = {
category: 3,
code: 9998,
pos: 0,
end: 1,
fileName: toRootedFilePath("/project/related.ts", undefined),
startPosition: { line: 0, character: 0 },
endPosition: { line: 0, character: 1 },
sourceLines: [{ line: 0, text: "x" }],
text: "Located explanation.",
};
const locationless = {
category: 3,
code: 9999,
pos: 0,
end: 0,
text: "This related explanation must remain visible.",
messageChain: [{
category: 3,
code: 9999,
pos: 0,
end: 0,
text: "Nested explanation.",
messageChain: [{
category: 3,
code: 9999,
pos: 0,
end: 0,
text: "Most specific explanation.",
}],
}],
};
const stripColors = (text: string) => text.replace(/\x1b\[[0-9;]*m/g, "");

const output = stripColors(formatDiagnosticsWithColorAndContext([{
...diagnostic,
relatedInformation: [located, locationless],
}], host));
assert.ok(output.includes(" - Located explanation."), output);
assert.ok(output.includes("related.ts:1:1"), output);
assert.ok(output.includes("1 x\r\n ~"), output);
assert.ok(output.includes(" This related explanation must remain visible.\r\n Nested explanation.\r\n Most specific explanation."), output);

const locationlessOnly = stripColors(formatDiagnosticsWithColorAndContext([{
...diagnostic,
relatedInformation: [locationless],
}], host));
assert.ok(locationlessOnly.includes("Primary error."), locationlessOnly);
assert.ok(locationlessOnly.includes("This related explanation must remain visible."), locationlessOnly);
assert.ok(locationlessOnly.includes("\r\n Nested explanation.\r\n Most specific explanation."), locationlessOnly);
const lfHost = { ...host, getNewLine: () => "\n" };
const lfOutput = stripColors(formatDiagnosticsWithColorAndContext([{
...diagnostic,
relatedInformation: [locationless],
}], lfHost));
assert.ok(lfOutput.includes("\n Nested explanation.\n Most specific explanation."), lfOutput);

const partialLocations = stripColors(formatDiagnosticsWithColorAndContext([{
...diagnostic,
relatedInformation: [
{ ...locationless, fileName: toRootedFilePath("/project/related.ts", undefined) },
{ ...locationless, startPosition: { line: 0, character: 0 } },
{
...locationless,
startPosition: { line: 0, character: 0 },
endPosition: { line: 0, character: 1 },
sourceLines: [{ line: 0, text: "x" }],
},
],
}], host));
assert.equal((partialLocations.match(/This related explanation must remain visible\./g) ?? []).length, 3, partialLocations);
assert.ok(!partialLocations.includes("related.ts:1:1"), partialLocations);
assert.ok(partialLocations.includes("1 x\r\n ~"), partialLocations);

for (const relatedInformation of [undefined, []]) {
const withoutRelated = stripColors(formatDiagnosticsWithColorAndContext([{
...diagnostic,
relatedInformation,
}], host));
assert.equal(withoutRelated, "error TS2322: Primary error.");
}
});

test("uses the API directory and LF defaults for inferred projects", async () => {
const api = spawnAPI({
"/workspace/index.ts": `const x: number = "oops";`,
Expand Down