Skip to content

Commit 6695255

Browse files
authored
Allow trailing newline to have fake position (#18298)
* Actually support baselining pretty in the harness * Test case from 18216 * Use host newline in formatDiagnosticsWithColorAndContext * Merge statements
1 parent b29e0c9 commit 6695255

File tree

7 files changed

+39
-16
lines changed

7 files changed

+39
-16
lines changed

src/compiler/program.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ namespace ts {
268268
return s;
269269
}
270270

271-
export function formatDiagnosticsWithColorAndContext(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): string {
271+
export function formatDiagnosticsWithColorAndContext(diagnostics: ReadonlyArray<Diagnostic>, host: FormatDiagnosticsHost): string {
272272
let output = "";
273273
for (const diagnostic of diagnostics) {
274274
if (diagnostic.file) {
@@ -284,12 +284,12 @@ namespace ts {
284284
gutterWidth = Math.max(ellipsis.length, gutterWidth);
285285
}
286286

287-
output += sys.newLine;
287+
output += host.getNewLine();
288288
for (let i = firstLine; i <= lastLine; i++) {
289289
// If the error spans over 5 lines, we'll only show the first 2 and last 2 lines,
290290
// so we'll skip ahead to the second-to-last line.
291291
if (hasMoreThanFiveLines && firstLine + 1 < i && i < lastLine - 1) {
292-
output += formatAndReset(padLeft(ellipsis, gutterWidth), gutterStyleSequence) + gutterSeparator + sys.newLine;
292+
output += formatAndReset(padLeft(ellipsis, gutterWidth), gutterStyleSequence) + gutterSeparator + host.getNewLine();
293293
i = lastLine - 1;
294294
}
295295

@@ -301,7 +301,7 @@ namespace ts {
301301

302302
// Output the gutter and the actual contents of the line.
303303
output += formatAndReset(padLeft(i + 1 + "", gutterWidth), gutterStyleSequence) + gutterSeparator;
304-
output += lineContent + sys.newLine;
304+
output += lineContent + host.getNewLine();
305305

306306
// Output the gutter and the error span for the line using tildes.
307307
output += formatAndReset(padLeft("", gutterWidth), gutterStyleSequence) + gutterSeparator;
@@ -323,17 +323,17 @@ namespace ts {
323323
}
324324
output += resetEscapeSequence;
325325

326-
output += sys.newLine;
326+
output += host.getNewLine();
327327
}
328328

329-
output += sys.newLine;
329+
output += host.getNewLine();
330330
output += `${ relativeFileName }(${ firstLine + 1 },${ firstLineChar + 1 }): `;
331331
}
332332

333333
const categoryColor = getCategoryFormat(diagnostic.category);
334334
const category = DiagnosticCategory[diagnostic.category].toLowerCase();
335-
output += `${ formatAndReset(category, categoryColor) } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, sys.newLine) }`;
336-
output += sys.newLine;
335+
output += `${ formatAndReset(category, categoryColor) } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine()) }`;
336+
output += host.getNewLine();
337337
}
338338
return output;
339339
}

src/compiler/scanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ namespace ts {
337337
Debug.assert(res < lineStarts[line + 1]);
338338
}
339339
else if (debugText !== undefined) {
340-
Debug.assert(res < debugText.length);
340+
Debug.assert(res <= debugText.length); // Allow single character overflow for trailing newline
341341
}
342342
return res;
343343
}

src/harness/compilerRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class CompilerBaselineRunner extends RunnerBase {
141141

142142
// check errors
143143
it("Correct errors for " + fileName, () => {
144-
Harness.Compiler.doErrorBaseline(justName, tsConfigFiles.concat(toBeCompiled, otherFiles), result.errors);
144+
Harness.Compiler.doErrorBaseline(justName, tsConfigFiles.concat(toBeCompiled, otherFiles), result.errors, !!options.pretty);
145145
});
146146

147147
it (`Correct module resolution tracing for ${fileName}`, () => {

src/harness/harness.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,11 +1284,12 @@ namespace Harness {
12841284
return normalized;
12851285
}
12861286

1287-
export function minimalDiagnosticsToString(diagnostics: ReadonlyArray<ts.Diagnostic>) {
1288-
return ts.formatDiagnostics(diagnostics, { getCanonicalFileName, getCurrentDirectory: () => "", getNewLine: () => Harness.IO.newLine() });
1287+
export function minimalDiagnosticsToString(diagnostics: ReadonlyArray<ts.Diagnostic>, pretty?: boolean) {
1288+
const host = { getCanonicalFileName, getCurrentDirectory: () => "", getNewLine: () => Harness.IO.newLine() };
1289+
return (pretty ? ts.formatDiagnosticsWithColorAndContext : ts.formatDiagnostics)(diagnostics, host);
12891290
}
12901291

1291-
export function getErrorBaseline(inputFiles: ReadonlyArray<TestFile>, diagnostics: ReadonlyArray<ts.Diagnostic>) {
1292+
export function getErrorBaseline(inputFiles: ReadonlyArray<TestFile>, diagnostics: ReadonlyArray<ts.Diagnostic>, pretty?: boolean) {
12921293
diagnostics = diagnostics.slice().sort(ts.compareDiagnostics);
12931294
let outputLines = "";
12941295
// Count up all errors that were found in files other than lib.d.ts so we don't miss any
@@ -1408,18 +1409,18 @@ namespace Harness {
14081409
// Verify we didn't miss any errors in total
14091410
assert.equal(totalErrorsReportedInNonLibraryFiles + numLibraryDiagnostics + numTest262HarnessDiagnostics, diagnostics.length, "total number of errors");
14101411

1411-
return minimalDiagnosticsToString(diagnostics) +
1412+
return minimalDiagnosticsToString(diagnostics, pretty) +
14121413
Harness.IO.newLine() + Harness.IO.newLine() + outputLines;
14131414
}
14141415

1415-
export function doErrorBaseline(baselinePath: string, inputFiles: TestFile[], errors: ts.Diagnostic[]) {
1416+
export function doErrorBaseline(baselinePath: string, inputFiles: TestFile[], errors: ts.Diagnostic[], pretty?: boolean) {
14161417
Harness.Baseline.runBaseline(baselinePath.replace(/\.tsx?$/, ".errors.txt"), (): string => {
14171418
if (!errors || (errors.length === 0)) {
14181419
/* tslint:disable:no-null-keyword */
14191420
return null;
14201421
/* tslint:enable:no-null-keyword */
14211422
}
1422-
return getErrorBaseline(inputFiles, errors);
1423+
return getErrorBaseline(inputFiles, errors, pretty);
14231424
});
14241425
}
14251426

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
2
3+
  
4+
5+
tests/cases/compiler/index.ts(2,1): error TS1005: '}' expected.
6+
7+
8+
==== tests/cases/compiler/index.ts (1 errors) ====
9+
if (true) {
10+
11+
12+
!!! error TS1005: '}' expected.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [index.ts]
2+
if (true) {
3+
4+
5+
//// [index.js]
6+
if (true) {
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @pretty: true
2+
// @filename: index.ts
3+
if (true) {

0 commit comments

Comments
 (0)