From a9f90c7ebd6926e7385a68ab017397ec691ee31d Mon Sep 17 00:00:00 2001 From: Philipp Kretzschmar Date: Sat, 12 Aug 2017 02:37:51 +0200 Subject: [PATCH 1/4] tsc --watch should clear screen on new compilation * added optional clearScreen method to System] * implemented via `x1Bc`, reset screen * fixes 13020 --- src/compiler/sys.ts | 4 ++++ src/compiler/tsc.ts | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 8a4c4ad22cc5b..eb1a7bad7848a 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -66,6 +66,7 @@ namespace ts { /*@internal*/ debugMode?: boolean; setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any; clearTimeout?(timeoutId: any): void; + clearScreen?(): void; } export interface FileWatcher { @@ -342,6 +343,9 @@ namespace ts { const noOpFileWatcher: FileWatcher = { close: noop }; const nodeSystem: System = { + clearScreen: () => { + process.stdout.write("\x1Bc"); + }, args: process.argv.slice(2), newLine: _os.EOL, useCaseSensitiveFileNames, diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index db25afe45b62a..b5d0a779d8e70 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -184,6 +184,10 @@ namespace ts { } if (isWatchSet(commandLine.options)) { + if (sys.clearScreen) { + sys.clearScreen(); + } + if (!sys.watchFile) { reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"), /* host */ undefined); return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped); @@ -402,6 +406,9 @@ namespace ts { } function recompile() { + if (sys.clearScreen()) { + sys.clearScreen(); + } timerHandleForRecompilation = undefined; reportWatchDiagnostic(createCompilerDiagnostic(Diagnostics.File_change_detected_Starting_incremental_compilation)); performCompilation(); From e96b35a858407e804150774ca50696a05063689c Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 1 Dec 2017 14:27:14 -0800 Subject: [PATCH 2/4] Feedback on if statements; api .d.ts baseline additions --- src/compiler/tsc.ts | 4 +--- src/compiler/watch.ts | 4 ++-- tests/baselines/reference/api/tsserverlibrary.d.ts | 1 + tests/baselines/reference/api/typescript.d.ts | 1 + 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 78c1314e1f7f4..9aef6c115b8bd 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -134,9 +134,7 @@ namespace ts { } function reportWatchModeWithoutSysSupport() { - if (sys.clearScreen()) { - sys.clearScreen(); - } + sys.clearScreen(); if (!sys.watchFile || !sys.watchDirectory) { reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch")); diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index aa47c81d25b94..a9e5fab2026d3 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -493,8 +493,8 @@ namespace ts { } function updateProgram() { - if (sys.clearScreen()) { - sys.clearScreen(); + if (watchingHost.system.clearScreen) { + watchingHost.system.clearScreen(); } timerToUpdateProgram = undefined; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 30c633c36d7ea..eae76cec1cef0 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2762,6 +2762,7 @@ declare namespace ts { realpath?(path: string): string; setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any; clearTimeout?(timeoutId: any): void; + clearScreen?(): void; } interface FileWatcher { close(): void; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index fb6188c1891a7..4a7ceb8b4a090 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2762,6 +2762,7 @@ declare namespace ts { realpath?(path: string): string; setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any; clearTimeout?(timeoutId: any): void; + clearScreen?(): void; } interface FileWatcher { close(): void; From 8f2c720b7b3d70833bbc186d1330d431bda0ade5 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 1 Dec 2017 15:42:14 -0800 Subject: [PATCH 3/4] Stopped clearing screen in tsc.js's reportWatchMode --- src/compiler/tsc.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 9aef6c115b8bd..01fb45e4f7d6c 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -134,8 +134,6 @@ namespace ts { } function reportWatchModeWithoutSysSupport() { - sys.clearScreen(); - if (!sys.watchFile || !sys.watchDirectory) { reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch")); sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped); From 889d29de75c5d994e5aecbff472f977f70b952ac Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 2 Dec 2017 00:45:52 -0800 Subject: [PATCH 4/4] Added unit tests --- src/harness/unittests/tscWatchMode.ts | 33 +++++++++++++++++++++++ src/harness/virtualFileSystemWithWatch.ts | 9 +++++++ 2 files changed, 42 insertions(+) diff --git a/src/harness/unittests/tscWatchMode.ts b/src/harness/unittests/tscWatchMode.ts index 4e2d63cec90ea..8087d038b7e23 100644 --- a/src/harness/unittests/tscWatchMode.ts +++ b/src/harness/unittests/tscWatchMode.ts @@ -2027,4 +2027,37 @@ declare module "fs" { assert.equal(host.readFile(outputFile1), file1.content + host.newLine); }); }); + + describe("tsc-watch console clearing", () => { + it("doesn't clear the console when it starts", () => { + const file = { + path: "f.ts", + content: "" + }; + const host = createWatchedSystem([file]); + + createWatchModeWithoutConfigFile([file.path], host); + host.runQueuedTimeoutCallbacks(); + + host.checkScreenClears(0); + }); + + it("clears the console on recompile", () => { + const file = { + path: "f.ts", + content: "" + }; + const host = createWatchedSystem([file]); + createWatchModeWithoutConfigFile([file.path], host); + + const modifiedFile = { + ...file, + content: "//" + }; + host.reloadFS([modifiedFile]); + host.runQueuedTimeoutCallbacks(); + + host.checkScreenClears(1); + }); + }); } diff --git a/src/harness/virtualFileSystemWithWatch.ts b/src/harness/virtualFileSystemWithWatch.ts index d4d203cabbbe1..581b86c042dbb 100644 --- a/src/harness/virtualFileSystemWithWatch.ts +++ b/src/harness/virtualFileSystemWithWatch.ts @@ -251,6 +251,7 @@ interface Array {}` private toPath: (f: string) => Path; private timeoutCallbacks = new Callbacks(); private immediateCallbacks = new Callbacks(); + private screenClears = 0; readonly watchedDirectories = createMultiMap(); readonly watchedDirectoriesRecursive = createMultiMap(); @@ -604,6 +605,10 @@ interface Array {}` this.timeoutCallbacks.unregister(timeoutId); } + clearScreen(): void { + this.screenClears += 1; + } + checkTimeoutQueueLengthAndRun(expected: number) { this.checkTimeoutQueueLength(expected); this.runQueuedTimeoutCallbacks(); @@ -638,6 +643,10 @@ interface Array {}` this.immediateCallbacks.unregister(timeoutId); } + checkScreenClears(expected: number): void { + assert.equal(this.screenClears, expected); + } + createDirectory(directoryName: string): void { const folder = this.toFolder(directoryName);