feat(cli): add --silent to disable all diagnostics printing (#3338)

This commit is contained in:
Boshen 2024-05-18 10:22:51 +00:00
parent c9d84af44e
commit 17f4b199b9
4 changed files with 47 additions and 3 deletions

View file

@ -0,0 +1,5 @@
{
"rules": {
"no-debugger": "error"
}
}

View file

@ -52,6 +52,10 @@ impl CliCommand {
/// Miscellaneous
#[derive(Debug, Clone, Bpaf)]
pub struct MiscOptions {
/// Do not display any diagnostics
#[bpaf(switch, hide_usage)]
pub silent: bool,
/// Number of threads to use. Set to 1 for using only 1 CPU core
#[bpaf(argument("INT"), hide_usage)]
pub threads: Option<usize>,

View file

@ -10,7 +10,7 @@ use oxc_span::VALID_EXTENSIONS;
use crate::{
command::{LintOptions as CliLintOptions, OutputFormat, OutputOptions, WarningOptions},
walk::{Extensions, Walk},
CliRunResult, LintResult, Runner,
CliRunResult, LintResult, MiscOptions, Runner,
};
pub struct LintRunner {
@ -40,6 +40,7 @@ impl Runner for LintRunner {
fix_options,
enable_plugins,
output_options,
misc_options,
..
} = self.options;
@ -130,7 +131,7 @@ impl Runner for LintRunner {
let options = LintServiceOptions { cwd, paths, tsconfig };
let lint_service = LintService::new(linter, options);
let mut diagnostic_service =
Self::get_diagnostic_service(&warning_options, &output_options);
Self::get_diagnostic_service(&warning_options, &output_options, &misc_options);
// Spawn linting in another thread so diagnostics can be printed immediately from diagnostic_service.run.
rayon::spawn({
@ -159,9 +160,11 @@ impl LintRunner {
fn get_diagnostic_service(
warning_options: &WarningOptions,
output_options: &OutputOptions,
misc_options: &MiscOptions,
) -> DiagnosticService {
let mut diagnostic_service = DiagnosticService::default()
.with_quiet(warning_options.quiet)
.with_silent(misc_options.silent)
.with_max_warnings(warning_options.max_warnings);
match output_options.format {
@ -181,7 +184,7 @@ mod test {
use crate::{lint_command, CliRunResult, LintResult, Runner};
fn test(args: &[&str]) -> LintResult {
let mut new_args = vec!["--quiet"];
let mut new_args = vec!["--silent"];
new_args.extend(args);
let options = lint_command().run_inner(new_args.as_slice()).unwrap().lint_options;
match LintRunner::new(options).run() {
@ -302,6 +305,24 @@ mod test {
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn filter_error() {
let args = &["-D", "correctness", "fixtures/linter/debugger.js"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 1);
}
#[test]
fn eslintrc_error() {
let args = &["-c", "fixtures/linter/eslintrc.json", "fixtures/linter/debugger.js"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 1);
}
#[test]
fn eslintrc_off() {
let args = &["-c", "fixtures/eslintrc_off/eslintrc.json", "fixtures/eslintrc_off/test.js"];

View file

@ -22,6 +22,9 @@ pub struct DiagnosticService {
/// Disable reporting on warnings, only errors are reported
quiet: bool,
/// Do not display any diagnostics
silent: bool,
/// Specify a warning threshold,
/// which can be used to force exit with an error status if there are too many warning-level rule violations in your project
max_warnings: Option<usize>,
@ -42,6 +45,7 @@ impl Default for DiagnosticService {
Self {
reporter: Box::<GraphicalReporter>::default(),
quiet: false,
silent: false,
max_warnings: None,
warnings_count: Cell::new(0),
errors_count: Cell::new(0),
@ -74,6 +78,12 @@ impl DiagnosticService {
self
}
#[must_use]
pub fn with_silent(mut self, yes: bool) -> Self {
self.silent = yes;
self
}
#[must_use]
pub fn with_max_warnings(mut self, max_warnings: Option<usize>) -> Self {
self.max_warnings = max_warnings;
@ -135,6 +145,10 @@ impl DiagnosticService {
}
}
if self.silent {
continue;
}
if let Some(mut err_str) = self.reporter.render_error(diagnostic) {
// Skip large output and print only once
if err_str.lines().any(|line| line.len() >= 400) {