Skip to content

Commit 08c6dc9

Browse files
Josh Goldbergmhegazy
Josh Goldberg
authored andcommitted
"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
1 parent e90bf5e commit 08c6dc9

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

src/compiler/sys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ namespace ts {
7272
/*@internal*/ debugMode?: boolean;
7373
setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
7474
clearTimeout?(timeoutId: any): void;
75+
clearScreen?(): void;
7576
}
7677

7778
export interface FileWatcher {
@@ -436,6 +437,9 @@ namespace ts {
436437
}
437438

438439
const nodeSystem: System = {
440+
clearScreen: () => {
441+
process.stdout.write("\x1Bc");
442+
},
439443
args: process.argv.slice(2),
440444
newLine: _os.EOL,
441445
useCaseSensitiveFileNames,

src/compiler/watch.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,10 @@ namespace ts {
493493
}
494494

495495
function updateProgram() {
496+
if (watchingHost.system.clearScreen) {
497+
watchingHost.system.clearScreen();
498+
}
499+
496500
timerToUpdateProgram = undefined;
497501
reportWatchDiagnostic(createCompilerDiagnostic(Diagnostics.File_change_detected_Starting_incremental_compilation));
498502

src/harness/unittests/tscWatchMode.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,4 +2027,37 @@ declare module "fs" {
20272027
assert.equal(host.readFile(outputFile1), file1.content + host.newLine);
20282028
});
20292029
});
2030+
2031+
describe("tsc-watch console clearing", () => {
2032+
it("doesn't clear the console when it starts", () => {
2033+
const file = {
2034+
path: "f.ts",
2035+
content: ""
2036+
};
2037+
const host = createWatchedSystem([file]);
2038+
2039+
createWatchModeWithoutConfigFile([file.path], host);
2040+
host.runQueuedTimeoutCallbacks();
2041+
2042+
host.checkScreenClears(0);
2043+
});
2044+
2045+
it("clears the console on recompile", () => {
2046+
const file = {
2047+
path: "f.ts",
2048+
content: ""
2049+
};
2050+
const host = createWatchedSystem([file]);
2051+
createWatchModeWithoutConfigFile([file.path], host);
2052+
2053+
const modifiedFile = {
2054+
...file,
2055+
content: "//"
2056+
};
2057+
host.reloadFS([modifiedFile]);
2058+
host.runQueuedTimeoutCallbacks();
2059+
2060+
host.checkScreenClears(1);
2061+
});
2062+
});
20302063
}

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ interface Array<T> {}`
251251
private toPath: (f: string) => Path;
252252
private timeoutCallbacks = new Callbacks();
253253
private immediateCallbacks = new Callbacks();
254+
private screenClears = 0;
254255

255256
readonly watchedDirectories = createMultiMap<TestDirectoryWatcher>();
256257
readonly watchedDirectoriesRecursive = createMultiMap<TestDirectoryWatcher>();
@@ -604,6 +605,10 @@ interface Array<T> {}`
604605
this.timeoutCallbacks.unregister(timeoutId);
605606
}
606607

608+
clearScreen(): void {
609+
this.screenClears += 1;
610+
}
611+
607612
checkTimeoutQueueLengthAndRun(expected: number) {
608613
this.checkTimeoutQueueLength(expected);
609614
this.runQueuedTimeoutCallbacks();
@@ -638,6 +643,10 @@ interface Array<T> {}`
638643
this.immediateCallbacks.unregister(timeoutId);
639644
}
640645

646+
checkScreenClears(expected: number): void {
647+
assert.equal(this.screenClears, expected);
648+
}
649+
641650
createDirectory(directoryName: string): void {
642651
const folder = this.toFolder(directoryName);
643652

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,6 +2762,7 @@ declare namespace ts {
27622762
realpath?(path: string): string;
27632763
setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
27642764
clearTimeout?(timeoutId: any): void;
2765+
clearScreen?(): void;
27652766
}
27662767
interface FileWatcher {
27672768
close(): void;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,6 +2762,7 @@ declare namespace ts {
27622762
realpath?(path: string): string;
27632763
setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
27642764
clearTimeout?(timeoutId: any): void;
2765+
clearScreen?(): void;
27652766
}
27662767
interface FileWatcher {
27672768
close(): void;

0 commit comments

Comments
 (0)