diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index 790295c225462..471a9323a3d97 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -31,8 +31,11 @@ namespace ts { }; } - function clearScreenIfNotWatchingForFileChanges(system: System, diagnostic: Diagnostic) { - if (system.clearScreen && diagnostic.code !== Diagnostics.Compilation_complete_Watching_for_file_changes.code) { + function clearScreenIfNotWatchingForFileChanges(system: System, diagnostic: Diagnostic, options: CompilerOptions) { + if (system.clearScreen && + diagnostic.code !== Diagnostics.Compilation_complete_Watching_for_file_changes.code && + !options.extendedDiagnostics && + !options.diagnostics) { system.clearScreen(); } } @@ -42,18 +45,18 @@ namespace ts { */ export function createWatchStatusReporter(system: System, pretty?: boolean): WatchStatusReporter { return pretty ? - (diagnostic: Diagnostic, newLine: string) => { - clearScreenIfNotWatchingForFileChanges(system, diagnostic); - let output = `[${ formatColorAndReset(new Date().toLocaleTimeString(), ForegroundColorEscapeSequences.Grey) }] `; - output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${newLine + newLine + newLine}`; - system.write(output); - } : - (diagnostic: Diagnostic, newLine: string) => { - clearScreenIfNotWatchingForFileChanges(system, diagnostic); - let output = new Date().toLocaleTimeString() + " - "; - output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${newLine + newLine + newLine}`; - system.write(output); - }; + (diagnostic, newLine, options) => { + clearScreenIfNotWatchingForFileChanges(system, diagnostic, options); + let output = `[${formatColorAndReset(new Date().toLocaleTimeString(), ForegroundColorEscapeSequences.Grey)}] `; + output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${newLine + newLine + newLine}`; + system.write(output); + } : + (diagnostic, newLine, options) => { + clearScreenIfNotWatchingForFileChanges(system, diagnostic, options); + let output = new Date().toLocaleTimeString() + " - "; + output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${newLine + newLine + newLine}`; + system.write(output); + }; } /** @@ -254,7 +257,7 @@ namespace ts { namespace ts { export type DiagnosticReporter = (diagnostic: Diagnostic) => void; - export type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string) => void; + export type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions) => void; export type CreateProgram = (rootNames: ReadonlyArray, options: CompilerOptions, host?: CompilerHost, oldProgram?: T) => T; export interface WatchCompilerHost { /** @@ -264,7 +267,7 @@ namespace ts { /** If provided, callback to invoke after every new program creation */ afterProgramCreate?(program: T): void; /** If provided, called with Diagnostic message that informs about change in watch status */ - onWatchStatusChange?(diagnostic: Diagnostic, newLine: string): void; + onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions): void; // Only for testing /*@internal*/ @@ -725,7 +728,7 @@ namespace ts { function reportWatchDiagnostic(message: DiagnosticMessage) { if (host.onWatchStatusChange) { - host.onWatchStatusChange(createCompilerDiagnostic(message), newLine); + host.onWatchStatusChange(createCompilerDiagnostic(message), newLine, compilerOptions); } } diff --git a/src/harness/unittests/tscWatchMode.ts b/src/harness/unittests/tscWatchMode.ts index 6915a6ad37d8b..b6d3099dc7768 100644 --- a/src/harness/unittests/tscWatchMode.ts +++ b/src/harness/unittests/tscWatchMode.ts @@ -2102,35 +2102,45 @@ declare module "fs" { }); describe("tsc-watch console clearing", () => { - it("clears the console when it starts", () => { + function checkConsoleClearing(diagnostics: boolean, extendedDiagnostics: boolean) { const file = { path: "f.ts", content: "" }; - const host = createWatchedSystem([file]); + const files = [file]; + const host = createWatchedSystem(files); + let clearCount: number | undefined; + checkConsoleClears(); - createWatchOfFilesAndCompilerOptions([file.path], host); - host.runQueuedTimeoutCallbacks(); + createWatchOfFilesAndCompilerOptions([file.path], host, { diagnostics, extendedDiagnostics }); + checkConsoleClears(); - host.checkScreenClears(1); - }); + file.content = "//"; + host.reloadFS(files); + host.runQueuedTimeoutCallbacks(); - it("clears the console on recompile", () => { - const file = { - path: "f.ts", - content: "" - }; - const host = createWatchedSystem([file]); - createWatchOfFilesAndCompilerOptions([file.path], host); + checkConsoleClears(); - const modifiedFile = { - ...file, - content: "//" - }; - host.reloadFS([modifiedFile]); - host.runQueuedTimeoutCallbacks(); + function checkConsoleClears() { + if (clearCount === undefined) { + clearCount = 0; + } + else if (!diagnostics && !extendedDiagnostics) { + clearCount++; + } + host.checkScreenClears(clearCount); + return clearCount; + } + } - host.checkScreenClears(2); + it("without --diagnostics or --extendedDiagnostics", () => { + checkConsoleClearing(/*diagnostics*/ false, /*extendedDiagnostics*/ false); + }); + it("with --diagnostics", () => { + checkConsoleClearing(/*diagnostics*/ true, /*extendedDiagnostics*/ false); + }); + it("with --extendedDiagnostics", () => { + checkConsoleClearing(/*diagnostics*/ false, /*extendedDiagnostics*/ true); }); }); } diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index bdcc29af76e36..6a0a701ca1aea 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -3978,7 +3978,7 @@ declare namespace ts { } declare namespace ts { type DiagnosticReporter = (diagnostic: Diagnostic) => void; - type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string) => void; + type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions) => void; type CreateProgram = (rootNames: ReadonlyArray, options: CompilerOptions, host?: CompilerHost, oldProgram?: T) => T; interface WatchCompilerHost { /** @@ -3988,7 +3988,7 @@ declare namespace ts { /** If provided, callback to invoke after every new program creation */ afterProgramCreate?(program: T): void; /** If provided, called with Diagnostic message that informs about change in watch status */ - onWatchStatusChange?(diagnostic: Diagnostic, newLine: string): void; + onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions): void; useCaseSensitiveFileNames(): boolean; getNewLine(): string; getCurrentDirectory(): string;