Skip to content

Commit 5d36aab

Browse files
fr3shw3bsheetalkamat
authored andcommitted
Added errorCount to WatchStatusReporter to detect 2 or more errors (#33082)
* Added errorCount to WatchStatusReporter discern between 0 and 2 or more errors * Added test for ensuring WatchStatusReporter receives errorCount
1 parent 13e1ccd commit 5d36aab

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

src/compiler/watch.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ namespace ts {
372372
errorCount => result.onWatchStatusChange!(
373373
createCompilerDiagnostic(getWatchErrorSummaryDiagnosticMessage(errorCount), errorCount),
374374
newLine,
375-
compilerOptions
375+
compilerOptions,
376+
errorCount
376377
)
377378
);
378379
};
@@ -480,14 +481,14 @@ namespace ts {
480481
return createProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences);
481482
}
482483

483-
export type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions) => void;
484+
export type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions, errorCount?: number) => void;
484485
/** Create the program with rootNames and options, if they are undefined, oldProgram and new configFile diagnostics create new program */
485486
export type CreateProgram<T extends BuilderProgram> = (rootNames: ReadonlyArray<string> | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: T, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>, projectReferences?: ReadonlyArray<ProjectReference> | undefined) => T;
486487

487488
/** Host that has watch functionality used in --watch mode */
488489
export interface WatchHost {
489490
/** If provided, called with Diagnostic message that informs about change in watch status */
490-
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions): void;
491+
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions, errorCount?: number): void;
491492

492493
/** Used to watch changes in source files, missing files needed to update the program or config file */
493494
watchFile(path: string, callback: FileWatcherCallback, pollingInterval?: number): FileWatcher;

src/testRunner/unittests/tscWatch/watchApi.ts

+28
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,32 @@ namespace ts.tscWatch {
3737
checkProgramActualFiles(program, [mainFile.path, libFile.path, settingsJson.path]);
3838
});
3939
});
40+
41+
describe("unittests:: tsc-watch:: watchAPI:: tsc-watch expose error count to watch status reporter", () => {
42+
const projectRoot = "/user/username/projects/project";
43+
const configFileJson: any = {
44+
compilerOptions: { module: "commonjs" },
45+
files: ["index.ts"]
46+
};
47+
const config: File = {
48+
path: `${projectRoot}/tsconfig.json`,
49+
content: JSON.stringify(configFileJson)
50+
};
51+
const mainFile: File = {
52+
path: `${projectRoot}/index.ts`,
53+
content: "let compiler = new Compiler(); for (let i = 0; j < 5; i++) {}"
54+
};
55+
56+
it("verify that the error count is correctly passed down to the watch status reporter", () => {
57+
const files = [libFile, mainFile, config];
58+
const host = createWatchedSystem(files, { currentDirectory: projectRoot });
59+
let watchedErrorCount;
60+
const reportWatchStatus: WatchStatusReporter = (_, __, ___, errorCount) => {
61+
watchedErrorCount = errorCount;
62+
};
63+
const compilerHost = createWatchCompilerHostOfConfigFile(config.path, {}, host, /*createProgram*/ undefined, /*reportDiagnostic*/ undefined, reportWatchStatus);
64+
createWatchProgram(compilerHost);
65+
assert.equal(watchedErrorCount, 2, "The error count was expected to be 2 for the file change");
66+
});
67+
});
4068
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4491,13 +4491,13 @@ declare namespace ts {
44914491
createProgram?: CreateProgram<T>;
44924492
}
44934493
function createIncrementalProgram<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>({ rootNames, options, configFileParsingDiagnostics, projectReferences, host, createProgram }: IncrementalProgramOptions<T>): T;
4494-
type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions) => void;
4494+
type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions, errorCount?: number) => void;
44954495
/** Create the program with rootNames and options, if they are undefined, oldProgram and new configFile diagnostics create new program */
44964496
type CreateProgram<T extends BuilderProgram> = (rootNames: ReadonlyArray<string> | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: T, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>, projectReferences?: ReadonlyArray<ProjectReference> | undefined) => T;
44974497
/** Host that has watch functionality used in --watch mode */
44984498
interface WatchHost {
44994499
/** If provided, called with Diagnostic message that informs about change in watch status */
4500-
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions): void;
4500+
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions, errorCount?: number): void;
45014501
/** Used to watch changes in source files, missing files needed to update the program or config file */
45024502
watchFile(path: string, callback: FileWatcherCallback, pollingInterval?: number): FileWatcher;
45034503
/** Used to watch resolved module's failed lookup locations, config file specs, type roots where auto type reference directives are added */

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4491,13 +4491,13 @@ declare namespace ts {
44914491
createProgram?: CreateProgram<T>;
44924492
}
44934493
function createIncrementalProgram<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>({ rootNames, options, configFileParsingDiagnostics, projectReferences, host, createProgram }: IncrementalProgramOptions<T>): T;
4494-
type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions) => void;
4494+
type WatchStatusReporter = (diagnostic: Diagnostic, newLine: string, options: CompilerOptions, errorCount?: number) => void;
44954495
/** Create the program with rootNames and options, if they are undefined, oldProgram and new configFile diagnostics create new program */
44964496
type CreateProgram<T extends BuilderProgram> = (rootNames: ReadonlyArray<string> | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: T, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>, projectReferences?: ReadonlyArray<ProjectReference> | undefined) => T;
44974497
/** Host that has watch functionality used in --watch mode */
44984498
interface WatchHost {
44994499
/** If provided, called with Diagnostic message that informs about change in watch status */
4500-
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions): void;
4500+
onWatchStatusChange?(diagnostic: Diagnostic, newLine: string, options: CompilerOptions, errorCount?: number): void;
45014501
/** Used to watch changes in source files, missing files needed to update the program or config file */
45024502
watchFile(path: string, callback: FileWatcherCallback, pollingInterval?: number): FileWatcher;
45034503
/** Used to watch resolved module's failed lookup locations, config file specs, type roots where auto type reference directives are added */

0 commit comments

Comments
 (0)