mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
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.
This commit is contained in:
1 parent
e0d067b48f
commit
9cf5538763
3 files changed
+27
-53
No files matched your search
@@ -3472,18 +3472,14 @@
|
||||
"category": "Message",
|
||||
"code": 6190
|
||||
},
|
||||
"Found 1 error in 1 file.": {
|
||||
"Found 1 error.": {
|
||||
"category": "Message",
|
||||
"code": 6191
|
||||
},
|
||||
"Found {0} errors in 1 file.": {
|
||||
"Found {0} errors.": {
|
||||
"category": "Message",
|
||||
"code": 6192
|
||||
},
|
||||
"Found {0} errors in {1} files.": {
|
||||
"category": "Message",
|
||||
"code": 6193
|
||||
},
|
||||
"Variable '{0}' implicitly has an '{1}' type.": {
|
||||
"category": "Error",
|
||||
"code": 7005
|
||||
|
||||
+1
-2
@@ -144,8 +144,7 @@ namespace ts {
|
||||
enableStatistics(compilerOptions);
|
||||
|
||||
const program = createProgram(rootFileNames, compilerOptions, compilerHost);
|
||||
const diagnosticsAndEmit = getProgramDiagnosticsAndEmit(program);
|
||||
const exitStatus = reportDiagnosticErrors(program, diagnosticsAndEmit, reportDiagnostic, s => sys.write(s + sys.newLine));
|
||||
const exitStatus = emitFilesAndReportErrors(program, reportDiagnostic, s => sys.write(s + sys.newLine));
|
||||
reportStatistics(program);
|
||||
return sys.exit(exitStatus);
|
||||
}
|
||||
|
||||
+24
-45
@@ -33,9 +33,8 @@ namespace ts {
|
||||
|
||||
const nonClearingMessageCodes: number[] = [
|
||||
Diagnostics.Compilation_complete_Watching_for_file_changes.code,
|
||||
Diagnostics.Found_1_error_in_1_file.code,
|
||||
Diagnostics.Found_0_errors_in_1_file.code,
|
||||
Diagnostics.Found_0_errors_in_1_files.code,
|
||||
Diagnostics.Found_1_error.code,
|
||||
Diagnostics.Found_0_errors.code
|
||||
];
|
||||
|
||||
function clearScreenIfNotWatchingForFileChanges(system: System, diagnostic: Diagnostic, options: CompilerOptions) {
|
||||
@@ -135,7 +134,12 @@ namespace ts {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function getProgramDiagnosticsAndEmit(program: ProgramToEmitFilesAndReportErrors): ProgramDiagnosticsAndEmit {
|
||||
export type ReportEmitErrorSummary = (errorCount: number) => void;
|
||||
|
||||
/**
|
||||
* Helper that emit files, report diagnostics and lists emitted and/or source files depending on compiler options
|
||||
*/
|
||||
export function emitFilesAndReportErrors(program: ProgramToEmitFilesAndReportErrors, reportDiagnostic: DiagnosticReporter, reportSummary?: ReportEmitErrorSummary, writeFileName?: (s: string) => void) {
|
||||
// First get and report any syntactic errors.
|
||||
const diagnostics = program.getSyntacticDiagnostics().slice();
|
||||
let reportSemanticDiagnostics = false;
|
||||
@@ -159,15 +163,6 @@ namespace ts {
|
||||
addRange(diagnostics, program.getSemanticDiagnostics());
|
||||
}
|
||||
|
||||
return { diagnostics, emittedFiles, emitSkipped };
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper that emit files, report diagnostics and lists emitted and/or source files depending on compiler options
|
||||
*/
|
||||
export function reportDiagnosticErrors(program: ProgramToEmitFilesAndReportErrors, diagnosticsAndEmit: ProgramDiagnosticsAndEmit, reportDiagnostic: DiagnosticReporter, writeFileName?: (s: string) => void) {
|
||||
const { diagnostics, emittedFiles, emitSkipped } = diagnosticsAndEmit;
|
||||
|
||||
sortAndDeduplicateDiagnostics(diagnostics).forEach(reportDiagnostic);
|
||||
if (writeFileName) {
|
||||
const currentDir = program.getCurrentDirectory();
|
||||
@@ -183,6 +178,10 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
if (reportSummary) {
|
||||
reportSummary(diagnostics.filter(diagnostic => diagnostic.category === DiagnosticCategory.Error).length);
|
||||
}
|
||||
|
||||
if (emitSkipped && diagnostics.length > 0) {
|
||||
// If the emitter didn't emit anything, then pass that value along.
|
||||
return ExitStatus.DiagnosticsPresent_OutputsSkipped;
|
||||
@@ -195,34 +194,6 @@ namespace ts {
|
||||
return ExitStatus.Success;
|
||||
}
|
||||
|
||||
function summarizeDiagnosticsAcrossFiles(diagnostics: Diagnostic[], reporter: WatchStatusReporter, newLine: string, compilerOptions: CompilerOptions): void {
|
||||
if (diagnostics.length === 1) {
|
||||
reporter(createCompilerDiagnostic(Diagnostics.Found_1_error_in_1_file), newLine, compilerOptions);
|
||||
return;
|
||||
}
|
||||
|
||||
const uniqueFileNamesCount = countUniqueDiagnosticFileNames(diagnostics);
|
||||
|
||||
if (uniqueFileNamesCount === 1) {
|
||||
reporter(createCompilerDiagnostic(Diagnostics.Found_0_errors_in_1_file, diagnostics.length), newLine, compilerOptions);
|
||||
}
|
||||
else if (uniqueFileNamesCount !== 0) {
|
||||
reporter(createCompilerDiagnostic(Diagnostics.Found_0_errors_in_1_files, diagnostics.length, uniqueFileNamesCount), newLine, compilerOptions);
|
||||
}
|
||||
}
|
||||
|
||||
function countUniqueDiagnosticFileNames(diagnostics: Diagnostic[]): number {
|
||||
const fileNames = createMap<boolean>();
|
||||
|
||||
for (const diagnostic of diagnostics) {
|
||||
if (diagnostic.file) {
|
||||
fileNames.set(diagnostic.file.fileName, true);
|
||||
}
|
||||
}
|
||||
|
||||
return fileNames.size;
|
||||
}
|
||||
|
||||
const noopFileWatcher: FileWatcher = { close: noop };
|
||||
|
||||
/**
|
||||
@@ -269,13 +240,21 @@ namespace ts {
|
||||
}
|
||||
|
||||
function emitFilesAndReportErrorUsingBuilder(builderProgram: BuilderProgram) {
|
||||
const diagnosticsAndEmit = getProgramDiagnosticsAndEmit(builderProgram);
|
||||
reportDiagnosticErrors(builderProgram, diagnosticsAndEmit, reportDiagnostic, writeFileName);
|
||||
|
||||
let reportSummary: ReportEmitErrorSummary | undefined;
|
||||
const compilerOptions = builderProgram.getCompilerOptions();
|
||||
|
||||
if (compilerOptions.pretty) {
|
||||
summarizeDiagnosticsAcrossFiles(diagnosticsAndEmit.diagnostics, onWatchStatusChange, system.newLine, compilerOptions);
|
||||
reportSummary = (errorCount: number) => {
|
||||
if (errorCount === 1) {
|
||||
onWatchStatusChange(createCompilerDiagnostic(Diagnostics.Found_1_error, errorCount), sys.newLine, compilerOptions);
|
||||
}
|
||||
else {
|
||||
onWatchStatusChange(createCompilerDiagnostic(Diagnostics.Found_0_errors, errorCount, errorCount), sys.newLine, compilerOptions);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
emitFilesAndReportErrors(builderProgram, reportDiagnostic, reportSummary, writeFileName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user