Skip to content

Commit 9cf5538

Browse files
author
Josh Goldberg
committed
Simplified error message counts; internalized reporting to createWatchCompilerHost
Instead of modifying the logic in both `tsc.ts`to `watch.ts` by splitting `emitFilesAndReportErrors` in two, this adds an optional function parameter to report them. Removes file counting for diagnostic messages, as they might not be coming from files. Next commit will include tests.
1 parent e0d067b commit 9cf5538

3 files changed

Lines changed: 27 additions & 53 deletions

File tree

src/compiler/diagnosticMessages.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3472,18 +3472,14 @@
34723472
"category": "Message",
34733473
"code": 6190
34743474
},
3475-
"Found 1 error in 1 file.": {
3475+
"Found 1 error.": {
34763476
"category": "Message",
34773477
"code": 6191
34783478
},
3479-
"Found {0} errors in 1 file.": {
3479+
"Found {0} errors.": {
34803480
"category": "Message",
34813481
"code": 6192
34823482
},
3483-
"Found {0} errors in {1} files.": {
3484-
"category": "Message",
3485-
"code": 6193
3486-
},
34873483
"Variable '{0}' implicitly has an '{1}' type.": {
34883484
"category": "Error",
34893485
"code": 7005

src/compiler/tsc.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ namespace ts {
144144
enableStatistics(compilerOptions);
145145

146146
const program = createProgram(rootFileNames, compilerOptions, compilerHost);
147-
const diagnosticsAndEmit = getProgramDiagnosticsAndEmit(program);
148-
const exitStatus = reportDiagnosticErrors(program, diagnosticsAndEmit, reportDiagnostic, s => sys.write(s + sys.newLine));
147+
const exitStatus = emitFilesAndReportErrors(program, reportDiagnostic, s => sys.write(s + sys.newLine));
149148
reportStatistics(program);
150149
return sys.exit(exitStatus);
151150
}

src/compiler/watch.ts

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ namespace ts {
3333

3434
const nonClearingMessageCodes: number[] = [
3535
Diagnostics.Compilation_complete_Watching_for_file_changes.code,
36-
Diagnostics.Found_1_error_in_1_file.code,
37-
Diagnostics.Found_0_errors_in_1_file.code,
38-
Diagnostics.Found_0_errors_in_1_files.code,
36+
Diagnostics.Found_1_error.code,
37+
Diagnostics.Found_0_errors.code
3938
];
4039

4140
function clearScreenIfNotWatchingForFileChanges(system: System, diagnostic: Diagnostic, options: CompilerOptions) {
@@ -135,7 +134,12 @@ namespace ts {
135134
}
136135

137136
/** @internal */
138-
export function getProgramDiagnosticsAndEmit(program: ProgramToEmitFilesAndReportErrors): ProgramDiagnosticsAndEmit {
137+
export type ReportEmitErrorSummary = (errorCount: number) => void;
138+
139+
/**
140+
* Helper that emit files, report diagnostics and lists emitted and/or source files depending on compiler options
141+
*/
142+
export function emitFilesAndReportErrors(program: ProgramToEmitFilesAndReportErrors, reportDiagnostic: DiagnosticReporter, reportSummary?: ReportEmitErrorSummary, writeFileName?: (s: string) => void) {
139143
// First get and report any syntactic errors.
140144
const diagnostics = program.getSyntacticDiagnostics().slice();
141145
let reportSemanticDiagnostics = false;
@@ -159,15 +163,6 @@ namespace ts {
159163
addRange(diagnostics, program.getSemanticDiagnostics());
160164
}
161165

162-
return { diagnostics, emittedFiles, emitSkipped };
163-
}
164-
165-
/**
166-
* Helper that emit files, report diagnostics and lists emitted and/or source files depending on compiler options
167-
*/
168-
export function reportDiagnosticErrors(program: ProgramToEmitFilesAndReportErrors, diagnosticsAndEmit: ProgramDiagnosticsAndEmit, reportDiagnostic: DiagnosticReporter, writeFileName?: (s: string) => void) {
169-
const { diagnostics, emittedFiles, emitSkipped } = diagnosticsAndEmit;
170-
171166
sortAndDeduplicateDiagnostics(diagnostics).forEach(reportDiagnostic);
172167
if (writeFileName) {
173168
const currentDir = program.getCurrentDirectory();
@@ -183,6 +178,10 @@ namespace ts {
183178
}
184179
}
185180

181+
if (reportSummary) {
182+
reportSummary(diagnostics.filter(diagnostic => diagnostic.category === DiagnosticCategory.Error).length);
183+
}
184+
186185
if (emitSkipped && diagnostics.length > 0) {
187186
// If the emitter didn't emit anything, then pass that value along.
188187
return ExitStatus.DiagnosticsPresent_OutputsSkipped;
@@ -195,34 +194,6 @@ namespace ts {
195194
return ExitStatus.Success;
196195
}
197196

198-
function summarizeDiagnosticsAcrossFiles(diagnostics: Diagnostic[], reporter: WatchStatusReporter, newLine: string, compilerOptions: CompilerOptions): void {
199-
if (diagnostics.length === 1) {
200-
reporter(createCompilerDiagnostic(Diagnostics.Found_1_error_in_1_file), newLine, compilerOptions);
201-
return;
202-
}
203-
204-
const uniqueFileNamesCount = countUniqueDiagnosticFileNames(diagnostics);
205-
206-
if (uniqueFileNamesCount === 1) {
207-
reporter(createCompilerDiagnostic(Diagnostics.Found_0_errors_in_1_file, diagnostics.length), newLine, compilerOptions);
208-
}
209-
else if (uniqueFileNamesCount !== 0) {
210-
reporter(createCompilerDiagnostic(Diagnostics.Found_0_errors_in_1_files, diagnostics.length, uniqueFileNamesCount), newLine, compilerOptions);
211-
}
212-
}
213-
214-
function countUniqueDiagnosticFileNames(diagnostics: Diagnostic[]): number {
215-
const fileNames = createMap<boolean>();
216-
217-
for (const diagnostic of diagnostics) {
218-
if (diagnostic.file) {
219-
fileNames.set(diagnostic.file.fileName, true);
220-
}
221-
}
222-
223-
return fileNames.size;
224-
}
225-
226197
const noopFileWatcher: FileWatcher = { close: noop };
227198

228199
/**
@@ -269,13 +240,21 @@ namespace ts {
269240
}
270241

271242
function emitFilesAndReportErrorUsingBuilder(builderProgram: BuilderProgram) {
272-
const diagnosticsAndEmit = getProgramDiagnosticsAndEmit(builderProgram);
273-
reportDiagnosticErrors(builderProgram, diagnosticsAndEmit, reportDiagnostic, writeFileName);
274-
243+
let reportSummary: ReportEmitErrorSummary | undefined;
275244
const compilerOptions = builderProgram.getCompilerOptions();
245+
276246
if (compilerOptions.pretty) {
277-
summarizeDiagnosticsAcrossFiles(diagnosticsAndEmit.diagnostics, onWatchStatusChange, system.newLine, compilerOptions);
247+
reportSummary = (errorCount: number) => {
248+
if (errorCount === 1) {
249+
onWatchStatusChange(createCompilerDiagnostic(Diagnostics.Found_1_error, errorCount), sys.newLine, compilerOptions);
250+
}
251+
else {
252+
onWatchStatusChange(createCompilerDiagnostic(Diagnostics.Found_0_errors, errorCount, errorCount), sys.newLine, compilerOptions);
253+
}
254+
};
278255
}
256+
257+
emitFilesAndReportErrors(builderProgram, reportDiagnostic, reportSummary, writeFileName);
279258
}
280259
}
281260

0 commit comments

Comments
 (0)