Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not clear console in watch mode if --diagnostics or --extendedDiagnostics is specified #21537

Merged
merged 1 commit into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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);
};
}

/**
Expand Down Expand Up @@ -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<T extends BuilderProgram> = (rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost, oldProgram?: T) => T;
export interface WatchCompilerHost<T extends BuilderProgram> {
/**
Expand All @@ -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*/
Expand Down Expand Up @@ -725,7 +728,7 @@ namespace ts {

function reportWatchDiagnostic(message: DiagnosticMessage) {
if (host.onWatchStatusChange) {
host.onWatchStatusChange(createCompilerDiagnostic(message), newLine);
host.onWatchStatusChange(createCompilerDiagnostic(message), newLine, compilerOptions);
}
}

Expand Down
50 changes: 30 additions & 20 deletions src/harness/unittests/tscWatchMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends BuilderProgram> = (rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost, oldProgram?: T) => T;
interface WatchCompilerHost<T extends BuilderProgram> {
/**
Expand All @@ -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;
Expand Down