From 08c6dc99c62a34a6813883dfb40638df0bd93f28 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 2 Dec 2017 11:11:49 -0800 Subject: [PATCH] "tsc --watch should clear screen on new compilation" Two: Electric Boogaloo (#20389) * tsc --watch should clear screen on new compilation * added optional clearScreen method to System] * implemented via `x1Bc`, reset screen * fixes 13020 * Feedback on if statements; api .d.ts baseline additions * Stopped clearing screen in tsc.js's reportWatchMode * Added unit tests --- src/compiler/sys.ts | 4 +++ src/compiler/watch.ts | 4 +++ src/harness/unittests/tscWatchMode.ts | 33 +++++++++++++++++++ src/harness/virtualFileSystemWithWatch.ts | 9 +++++ .../reference/api/tsserverlibrary.d.ts | 1 + tests/baselines/reference/api/typescript.d.ts | 1 + 6 files changed, 52 insertions(+) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index aa928cdaac..841dba8a52 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -72,6 +72,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 { @@ -436,6 +437,9 @@ namespace ts { } const nodeSystem: System = { + clearScreen: () => { + process.stdout.write("\x1Bc"); + }, args: process.argv.slice(2), newLine: _os.EOL, useCaseSensitiveFileNames, diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index 8e49bb71a1..a9e5fab202 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -493,6 +493,10 @@ namespace ts { } function updateProgram() { + if (watchingHost.system.clearScreen) { + watchingHost.system.clearScreen(); + } + timerToUpdateProgram = undefined; reportWatchDiagnostic(createCompilerDiagnostic(Diagnostics.File_change_detected_Starting_incremental_compilation)); diff --git a/src/harness/unittests/tscWatchMode.ts b/src/harness/unittests/tscWatchMode.ts index 4e2d63cec9..8087d038b7 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 d4d203cabb..581b86c042 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); diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 309cb242c8..958637dc32 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 2b183bda0b..bc127f8ff7 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;