Skip to content

Commit 1fb3593

Browse files
authored
Merge pull request microsoft#21537 from Microsoft/donotClearScreenWithDiagnostics
Do not clear console in watch mode if --diagnostics or --extendedDiagnostics is specified
2 parents 0297289 + d4c8436 commit 1fb3593

3 files changed

Lines changed: 52 additions & 39 deletions

File tree

src/compiler/watch.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ namespace ts {
3131
};
3232
}
3333

34-
function clearScreenIfNotWatchingForFileChanges(system: System, diagnostic: Diagnostic) {
35-
if (system.clearScreen && diagnostic.code !== Diagnostics.Compilation_complete_Watching_for_file_changes.code) {
34+
function clearScreenIfNotWatchingForFileChanges(system: System, diagnostic: Diagnostic, options: CompilerOptions) {
35+
if (system.clearScreen &&
36+
diagnostic.code !== Diagnostics.Compilation_complete_Watching_for_file_changes.code &&
37+
!options.extendedDiagnostics &&
38+
!options.diagnostics) {
3639
system.clearScreen();
3740
}
3841
}
@@ -42,18 +45,18 @@ namespace ts {
4245
*/
4346
export function createWatchStatusReporter(system: System, pretty?: boolean): WatchStatusReporter {
4447
return pretty ?
45-
(diagnostic: Diagnostic, newLine: string) => {
46-
clearScreenIfNotWatchingForFileChanges(system, diagnostic);
47-
let output = `[${ formatColorAndReset(new Date().toLocaleTimeString(), ForegroundColorEscapeSequences.Grey) }] `;
48-
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${newLine + newLine + newLine}`;
49-
system.write(output);
50-
} :
51-
(diagnostic: Diagnostic, newLine: string) => {
52-
clearScreenIfNotWatchingForFileChanges(system, diagnostic);
53-
let output = new Date().toLocaleTimeString() + " - ";
54-
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${newLine + newLine + newLine}`;
55-
system.write(output);
56-
};
48+
(diagnostic, newLine, options) => {
49+
clearScreenIfNotWatchingForFileChanges(system, diagnostic, options);
50+
let output = `[${formatColorAndReset(new Date().toLocaleTimeString(), ForegroundColorEscapeSequences.Grey)}] `;
51+
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${newLine + newLine + newLine}`;
52+
system.write(output);
53+
} :
54+
(diagnostic, newLine, options) => {
55+
clearScreenIfNotWatchingForFileChanges(system, diagnostic, options);
56+
let output = new Date().toLocaleTimeString() + " - ";
57+
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${newLine + newLine + newLine}`;
58+
system.write(output);
59+
};
5760
}
5861

5962
/**
@@ -254,7 +257,7 @@ namespace ts {
254257

255258
namespace ts {
256259
export type DiagnosticReporter = (diagnostic: Diagnostic) => void;
257-
export type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string) => void;
260+
export type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions) => void;
258261
export type CreateProgram<T extends BuilderProgram> = (rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost, oldProgram?: T) => T;
259262
export interface WatchCompilerHost<T extends BuilderProgram> {
260263
/**
@@ -264,7 +267,7 @@ namespace ts {
264267
/** If provided, callback to invoke after every new program creation */
265268
afterProgramCreate?(program: T): void;
266269
/** If provided, called with Diagnostic message that informs about change in watch status */
267-
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string): void;
270+
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions): void;
268271

269272
// Only for testing
270273
/*@internal*/
@@ -725,7 +728,7 @@ namespace ts {
725728

726729
function reportWatchDiagnostic(message: DiagnosticMessage) {
727730
if (host.onWatchStatusChange) {
728-
host.onWatchStatusChange(createCompilerDiagnostic(message), newLine);
731+
host.onWatchStatusChange(createCompilerDiagnostic(message), newLine, compilerOptions);
729732
}
730733
}
731734

src/harness/unittests/tscWatchMode.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,35 +2102,45 @@ declare module "fs" {
21022102
});
21032103

21042104
describe("tsc-watch console clearing", () => {
2105-
it("clears the console when it starts", () => {
2105+
function checkConsoleClearing(diagnostics: boolean, extendedDiagnostics: boolean) {
21062106
const file = {
21072107
path: "f.ts",
21082108
content: ""
21092109
};
2110-
const host = createWatchedSystem([file]);
2110+
const files = [file];
2111+
const host = createWatchedSystem(files);
2112+
let clearCount: number | undefined;
2113+
checkConsoleClears();
21112114

2112-
createWatchOfFilesAndCompilerOptions([file.path], host);
2113-
host.runQueuedTimeoutCallbacks();
2115+
createWatchOfFilesAndCompilerOptions([file.path], host, { diagnostics, extendedDiagnostics });
2116+
checkConsoleClears();
21142117

2115-
host.checkScreenClears(1);
2116-
});
2118+
file.content = "//";
2119+
host.reloadFS(files);
2120+
host.runQueuedTimeoutCallbacks();
21172121

2118-
it("clears the console on recompile", () => {
2119-
const file = {
2120-
path: "f.ts",
2121-
content: ""
2122-
};
2123-
const host = createWatchedSystem([file]);
2124-
createWatchOfFilesAndCompilerOptions([file.path], host);
2122+
checkConsoleClears();
21252123

2126-
const modifiedFile = {
2127-
...file,
2128-
content: "//"
2129-
};
2130-
host.reloadFS([modifiedFile]);
2131-
host.runQueuedTimeoutCallbacks();
2124+
function checkConsoleClears() {
2125+
if (clearCount === undefined) {
2126+
clearCount = 0;
2127+
}
2128+
else if (!diagnostics && !extendedDiagnostics) {
2129+
clearCount++;
2130+
}
2131+
host.checkScreenClears(clearCount);
2132+
return clearCount;
2133+
}
2134+
}
21322135

2133-
host.checkScreenClears(2);
2136+
it("without --diagnostics or --extendedDiagnostics", () => {
2137+
checkConsoleClearing(/*diagnostics*/ false, /*extendedDiagnostics*/ false);
2138+
});
2139+
it("with --diagnostics", () => {
2140+
checkConsoleClearing(/*diagnostics*/ true, /*extendedDiagnostics*/ false);
2141+
});
2142+
it("with --extendedDiagnostics", () => {
2143+
checkConsoleClearing(/*diagnostics*/ false, /*extendedDiagnostics*/ true);
21342144
});
21352145
});
21362146
}

tests/baselines/reference/api/typescript.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3978,7 +3978,7 @@ declare namespace ts {
39783978
}
39793979
declare namespace ts {
39803980
type DiagnosticReporter = (diagnostic: Diagnostic) => void;
3981-
type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string) => void;
3981+
type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions) => void;
39823982
type CreateProgram<T extends BuilderProgram> = (rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost, oldProgram?: T) => T;
39833983
interface WatchCompilerHost<T extends BuilderProgram> {
39843984
/**
@@ -3988,7 +3988,7 @@ declare namespace ts {
39883988
/** If provided, callback to invoke after every new program creation */
39893989
afterProgramCreate?(program: T): void;
39903990
/** If provided, called with Diagnostic message that informs about change in watch status */
3991-
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string): void;
3991+
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions): void;
39923992
useCaseSensitiveFileNames(): boolean;
39933993
getNewLine(): string;
39943994
getCurrentDirectory(): string;

0 commit comments

Comments
 (0)