fix(cli): if format is json do not print summary information (#2899) (#2925)

this allows the ability to redirect the stdout to so can create a valid
json file

this also matches with eslint's format option

closes #2899
This commit is contained in:
Kalven Schraut 2024-04-09 01:26:10 -05:00 committed by GitHub
parent d65eab3b8b
commit 6eba02f472
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 21 deletions

View file

@ -145,6 +145,7 @@ impl Runner for LintRunner {
number_of_errors: diagnostic_service.errors_count(), number_of_errors: diagnostic_service.errors_count(),
max_warnings_exceeded: diagnostic_service.max_warnings_exceeded(), max_warnings_exceeded: diagnostic_service.max_warnings_exceeded(),
deny_warnings: warning_options.deny_warnings, deny_warnings: warning_options.deny_warnings,
print_summary: diagnostic_service.is_graphical_output(),
}) })
} }
} }

View file

@ -23,6 +23,7 @@ pub struct LintResult {
pub number_of_errors: usize, pub number_of_errors: usize,
pub max_warnings_exceeded: bool, pub max_warnings_exceeded: bool,
pub deny_warnings: bool, pub deny_warnings: bool,
pub print_summary: bool,
} }
#[derive(Debug)] #[derive(Debug)]
@ -51,31 +52,36 @@ impl Termination for CliRunResult {
number_of_errors, number_of_errors,
max_warnings_exceeded, max_warnings_exceeded,
deny_warnings, deny_warnings,
print_summary,
}) => { }) => {
let threads = rayon::current_num_threads(); if print_summary {
let number_of_diagnostics = number_of_warnings + number_of_errors; let threads = rayon::current_num_threads();
let number_of_diagnostics = number_of_warnings + number_of_errors;
if number_of_diagnostics > 0 { if number_of_diagnostics > 0 {
println!(); println!();
}
let time = Self::get_execution_time(&duration);
let s = if number_of_files == 1 { "" } else { "s" };
println!(
"Finished in {time} on {number_of_files} file{s} with {number_of_rules} rules using {threads} threads."
);
if max_warnings_exceeded {
println!(
"Exceeded maximum number of warnings. Found {number_of_warnings}."
);
return ExitCode::from(1);
}
println!(
"Found {number_of_warnings} warning{} and {number_of_errors} error{}.",
if number_of_warnings == 1 { "" } else { "s" },
if number_of_errors == 1 { "" } else { "s" }
);
} }
let time = Self::get_execution_time(&duration);
let s = if number_of_files == 1 { "" } else { "s" };
println!(
"Finished in {time} on {number_of_files} file{s} with {number_of_rules} rules using {threads} threads."
);
if max_warnings_exceeded {
println!("Exceeded maximum number of warnings. Found {number_of_warnings}.");
return ExitCode::from(1);
}
println!(
"Found {number_of_warnings} warning{} and {number_of_errors} error{}.",
if number_of_warnings == 1 { "" } else { "s" },
if number_of_errors == 1 { "" } else { "s" }
);
let exit_code = let exit_code =
u8::from((number_of_warnings > 0 && deny_warnings) || number_of_errors > 0); u8::from((number_of_warnings > 0 && deny_warnings) || number_of_errors > 0);
ExitCode::from(exit_code) ExitCode::from(exit_code)

View file

@ -52,6 +52,10 @@ impl DiagnosticService {
self.reporter = DiagnosticReporter::new_json(); self.reporter = DiagnosticReporter::new_json();
} }
pub fn is_graphical_output(&self) -> bool {
matches!(self.reporter, DiagnosticReporter::Graphical { .. })
}
#[must_use] #[must_use]
pub fn with_quiet(mut self, yes: bool) -> Self { pub fn with_quiet(mut self, yes: bool) -> Self {
self.quiet = yes; self.quiet = yes;