Skip to content

Commit 049610e

Browse files
authored
Merge pull request #9689 from alexeagle/formatDiags
Add formatDiagnostics utility
2 parents f9d2937 + 34e81f2 commit 049610e

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/compiler/program.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,23 @@ namespace ts {
997997
return sortAndDeduplicateDiagnostics(diagnostics);
998998
}
999999

1000+
export function formatDiagnostics(diagnostics: Diagnostic[], host: CompilerHost): string {
1001+
let output = "";
1002+
1003+
for (const diagnostic of diagnostics) {
1004+
if (diagnostic.file) {
1005+
const { line, character } = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
1006+
const fileName = diagnostic.file.fileName;
1007+
const relativeFileName = convertToRelativePath(fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName));
1008+
output += `${ relativeFileName }(${ line + 1 },${ character + 1 }): `;
1009+
}
1010+
1011+
const category = DiagnosticCategory[diagnostic.category].toLowerCase();
1012+
output += `${ category } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine) }${ sys.newLine }`;
1013+
}
1014+
return output;
1015+
}
1016+
10001017
export function flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain, newLine: string): string {
10011018
if (typeof messageText === "string") {
10021019
return messageText;

src/compiler/tsc.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,8 @@ namespace ts {
101101
return <string>diagnostic.messageText;
102102
}
103103

104-
function getRelativeFileName(fileName: string, host: CompilerHost): string {
105-
return host ? convertToRelativePath(fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName)) : fileName;
106-
}
107-
108104
function reportDiagnosticSimply(diagnostic: Diagnostic, host: CompilerHost): void {
109-
let output = "";
110-
111-
if (diagnostic.file) {
112-
const { line, character } = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
113-
const relativeFileName = getRelativeFileName(diagnostic.file.fileName, host);
114-
output += `${ relativeFileName }(${ line + 1 },${ character + 1 }): `;
115-
}
116-
117-
const category = DiagnosticCategory[diagnostic.category].toLowerCase();
118-
output += `${ category } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine) }${ sys.newLine }`;
119-
120-
sys.write(output);
105+
sys.write(ts.formatDiagnostics([diagnostic], host));
121106
}
122107

123108
const redForegroundEscapeSequence = "\u001b[91m";
@@ -145,7 +130,7 @@ namespace ts {
145130
const { line: firstLine, character: firstLineChar } = getLineAndCharacterOfPosition(file, start);
146131
const { line: lastLine, character: lastLineChar } = getLineAndCharacterOfPosition(file, start + length);
147132
const lastLineInFile = getLineAndCharacterOfPosition(file, file.text.length).line;
148-
const relativeFileName = getRelativeFileName(file.fileName, host);
133+
const relativeFileName = host ? convertToRelativePath(file.fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName)) : file.fileName;;
149134

150135
const hasMoreThanFiveLines = (lastLine - firstLine) >= 4;
151136
let gutterWidth = (lastLine + 1 + "").length;

0 commit comments

Comments
 (0)