From 297643cf58f2e3c63eabf8bf728bf2e97f968d1a Mon Sep 17 00:00:00 2001 From: TheNightCoder21 Date: Fri, 20 Nov 2026 02:02:15 +0530 Subject: [PATCH] Fix formatting of locationless related diagnostics --- .../typescript/src/api/diagnosticFormatter.ts | 16 +-- .../test/diagnosticFormatter.test.ts | 97 +++++++++++++++++++ 2 files changed, 107 insertions(+), 6 deletions(-) diff --git a/packages/typescript/src/api/diagnosticFormatter.ts b/packages/typescript/src/api/diagnosticFormatter.ts index ed5129bb215d0..b9ebc6943a9a2 100644 --- a/packages/typescript/src/api/diagnosticFormatter.ts +++ b/packages/typescript/src/api/diagnosticFormatter.ts @@ -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; } @@ -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(); } } diff --git a/packages/typescript/test/diagnosticFormatter.test.ts b/packages/typescript/test/diagnosticFormatter.test.ts index e20b5acbb371c..a20618c089f56 100644 --- a/packages/typescript/test/diagnosticFormatter.test.ts +++ b/packages/typescript/test/diagnosticFormatter.test.ts @@ -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, @@ -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";`,