From dc0e96acebdb60d77aea8d5f6e27b4a8e058ae8d Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 13 Jul 2016 15:48:11 -0700 Subject: [PATCH 1/4] Convert getErrorBaseline to use canonical diagnostic formatting --- src/compiler/program.ts | 7 ++++++- src/harness/harness.ts | 13 +------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index c2a1e6ade66d2..e899cdf14d3a0 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -991,7 +991,12 @@ namespace ts { return sortAndDeduplicateDiagnostics(diagnostics); } - export function formatDiagnostics(diagnostics: Diagnostic[], host: CompilerHost): string { + export interface FormatDiagnosticsHost { + getCurrentDirectory(): string; + getCanonicalFileName(fileName: string): string; + } + + export function formatDiagnostics(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): string { let output = ""; for (const diagnostic of diagnostics) { diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 534778fdab8f4..057072e022b07 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1210,18 +1210,7 @@ namespace Harness { } export function minimalDiagnosticsToString(diagnostics: ts.Diagnostic[]) { - // This is basically copied from tsc.ts's reportError to replicate what tsc does - let errorOutput = ""; - ts.forEach(diagnostics, diagnostic => { - if (diagnostic.file) { - const lineAndCharacter = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); - errorOutput += diagnostic.file.fileName + "(" + (lineAndCharacter.line + 1) + "," + (lineAndCharacter.character + 1) + "): "; - } - - errorOutput += ts.DiagnosticCategory[diagnostic.category].toLowerCase() + " TS" + diagnostic.code + ": " + ts.flattenDiagnosticMessageText(diagnostic.messageText, Harness.IO.newLine()) + Harness.IO.newLine(); - }); - - return errorOutput; + return ts.formatDiagnostics(diagnostics, {getCanonicalFileName, getCurrentDirectory: () => ""}); } export function getErrorBaseline(inputFiles: TestFile[], diagnostics: ts.Diagnostic[]) { From 057612f052a63a9803efecf7a95a46033af0ede9 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 15 Jul 2016 12:32:52 -0700 Subject: [PATCH 2/4] Fix lint --- src/harness/harness.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/harness/harness.ts b/src/harness/harness.ts index e9b2e0cbb1303..f27e7e1c1749a 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1210,7 +1210,7 @@ namespace Harness { } export function minimalDiagnosticsToString(diagnostics: ts.Diagnostic[]) { - return ts.formatDiagnostics(diagnostics, {getCanonicalFileName, getCurrentDirectory: () => "", getNewLine: () => Harness.IO.newLine()}); + return ts.formatDiagnostics(diagnostics, { getCanonicalFileName, getCurrentDirectory: () => "", getNewLine: () => Harness.IO.newLine() }); } export function getErrorBaseline(inputFiles: TestFile[], diagnostics: ts.Diagnostic[]) { From d01cb066b127d4cb66f1b2b05a3de94fad68a330 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Jul 2016 10:41:08 -0700 Subject: [PATCH 3/4] Found another clone of format diagnostic - consolidate --- src/harness/unittests/moduleResolution.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/harness/unittests/moduleResolution.ts b/src/harness/unittests/moduleResolution.ts index b3f2102d903de..e71d57ac928b1 100644 --- a/src/harness/unittests/moduleResolution.ts +++ b/src/harness/unittests/moduleResolution.ts @@ -1,19 +1,9 @@ /// namespace ts { + const diagnosticHost: FormatDiagnosticsHost = {getNewLine: () => sys.newLine, getCanonicalFileName: name => name, getCurrentDirectory: () => ""}; function diagnosticToString(diagnostic: Diagnostic) { - let output = ""; - - if (diagnostic.file) { - const loc = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); - - output += `${diagnostic.file.fileName}(${loc.line + 1},${loc.character + 1}): `; - } - - const category = DiagnosticCategory[diagnostic.category].toLowerCase(); - output += `${category} TS${diagnostic.code}: ${flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine)}${sys.newLine}`; - - return output; + return formatDiagnostics([diagnostic], diagnosticHost); } interface File { From eb7355687ea2df2faeba61fa33f20d438783a36e Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Jul 2016 10:44:56 -0700 Subject: [PATCH 4/4] Fully declone --- src/harness/unittests/moduleResolution.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/harness/unittests/moduleResolution.ts b/src/harness/unittests/moduleResolution.ts index e71d57ac928b1..5f63889b08498 100644 --- a/src/harness/unittests/moduleResolution.ts +++ b/src/harness/unittests/moduleResolution.ts @@ -1,11 +1,6 @@ /// namespace ts { - const diagnosticHost: FormatDiagnosticsHost = {getNewLine: () => sys.newLine, getCanonicalFileName: name => name, getCurrentDirectory: () => ""}; - function diagnosticToString(diagnostic: Diagnostic) { - return formatDiagnostics([diagnostic], diagnosticHost); - } - interface File { name: string; content?: string; @@ -312,9 +307,9 @@ namespace ts { assert.equal(program.getSourceFiles().length, expectedFilesCount); const syntacticDiagnostics = program.getSyntacticDiagnostics(); - assert.equal(syntacticDiagnostics.length, 0, `expect no syntactic diagnostics, got: ${JSON.stringify(syntacticDiagnostics.map(diagnosticToString))}`); + assert.equal(syntacticDiagnostics.length, 0, `expect no syntactic diagnostics, got: ${JSON.stringify(Harness.Compiler.minimalDiagnosticsToString(syntacticDiagnostics))}`); const semanticDiagnostics = program.getSemanticDiagnostics(); - assert.equal(semanticDiagnostics.length, 0, `expect no semantic diagnostics, got: ${JSON.stringify(semanticDiagnostics.map(diagnosticToString))}`); + assert.equal(semanticDiagnostics.length, 0, `expect no semantic diagnostics, got: ${JSON.stringify(Harness.Compiler.minimalDiagnosticsToString(semanticDiagnostics))}`); // try to get file using a relative name for (const relativeFileName of relativeNamesToCheck) { @@ -393,7 +388,7 @@ export = C; }; const program = createProgram(rootFiles, options, host); const diagnostics = sortAndDeduplicateDiagnostics(program.getSemanticDiagnostics().concat(program.getOptionsDiagnostics())); - assert.equal(diagnostics.length, diagnosticCodes.length, `Incorrect number of expected diagnostics, expected ${diagnosticCodes.length}, got '${map(diagnostics, diagnosticToString).join("\r\n")}'`); + assert.equal(diagnostics.length, diagnosticCodes.length, `Incorrect number of expected diagnostics, expected ${diagnosticCodes.length}, got '${Harness.Compiler.minimalDiagnosticsToString(diagnostics)}'`); for (let i = 0; i < diagnosticCodes.length; i++) { assert.equal(diagnostics[i].code, diagnosticCodes[i], `Expected diagnostic code ${diagnosticCodes[i]}, got '${diagnostics[i].code}': '${diagnostics[i].messageText}'`); }