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 /// Miscellaneous
#[derive(Debug, Clone, Bpaf)] #[derive(Debug, Clone, Bpaf)]
pub struct MiscOptions { 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 /// Number of threads to use. Set to 1 for using only 1 CPU core
#[bpaf(argument("INT"), hide_usage)] #[bpaf(argument("INT"), hide_usage)]
pub threads: Option<usize>, pub threads: Option<usize>,

View file

@ -10,7 +10,7 @@ use oxc_span::VALID_EXTENSIONS;
use crate::{ use crate::{
command::{LintOptions as CliLintOptions, OutputFormat, OutputOptions, WarningOptions}, command::{LintOptions as CliLintOptions, OutputFormat, OutputOptions, WarningOptions},
walk::{Extensions, Walk}, walk::{Extensions, Walk},
CliRunResult, LintResult, Runner, CliRunResult, LintResult, MiscOptions, Runner,
}; };
pub struct LintRunner { pub struct LintRunner {
@ -40,6 +40,7 @@ impl Runner for LintRunner {
fix_options, fix_options,
enable_plugins, enable_plugins,
output_options, output_options,
misc_options,
.. ..
} = self.options; } = self.options;
@ -130,7 +131,7 @@ impl Runner for LintRunner {
let options = LintServiceOptions { cwd, paths, tsconfig }; let options = LintServiceOptions { cwd, paths, tsconfig };
let lint_service = LintService::new(linter, options); let lint_service = LintService::new(linter, options);
let mut diagnostic_service = 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. // Spawn linting in another thread so diagnostics can be printed immediately from diagnostic_service.run.
rayon::spawn({ rayon::spawn({
@ -159,9 +160,11 @@ impl LintRunner {
fn get_diagnostic_service( fn get_diagnostic_service(
warning_options: &WarningOptions, warning_options: &WarningOptions,
output_options: &OutputOptions, output_options: &OutputOptions,
misc_options: &MiscOptions,
) -> DiagnosticService { ) -> DiagnosticService {
let mut diagnostic_service = DiagnosticService::default() let mut diagnostic_service = DiagnosticService::default()
.with_quiet(warning_options.quiet) .with_quiet(warning_options.quiet)
.with_silent(misc_options.silent)
.with_max_warnings(warning_options.max_warnings); .with_max_warnings(warning_options.max_warnings);
match output_options.format { match output_options.format {
@ -181,7 +184,7 @@ mod test {
use crate::{lint_command, CliRunResult, LintResult, Runner}; use crate::{lint_command, CliRunResult, LintResult, Runner};
fn test(args: &[&str]) -> LintResult { fn test(args: &[&str]) -> LintResult {
let mut new_args = vec!["--quiet"]; let mut new_args = vec!["--silent"];
new_args.extend(args); new_args.extend(args);
let options = lint_command().run_inner(new_args.as_slice()).unwrap().lint_options; let options = lint_command().run_inner(new_args.as_slice()).unwrap().lint_options;
match LintRunner::new(options).run() { match LintRunner::new(options).run() {
@ -302,6 +305,24 @@ mod test {
assert_eq!(result.number_of_errors, 0); 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] #[test]
fn eslintrc_off() { fn eslintrc_off() {
let args = &["-c", "fixtures/eslintrc_off/eslintrc.json", "fixtures/eslintrc_off/test.js"]; 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 /// Disable reporting on warnings, only errors are reported
quiet: bool, quiet: bool,
/// Do not display any diagnostics
silent: bool,
/// Specify a warning threshold, /// 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 /// 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>, max_warnings: Option<usize>,
@ -42,6 +45,7 @@ impl Default for DiagnosticService {
Self { Self {
reporter: Box::<GraphicalReporter>::default(), reporter: Box::<GraphicalReporter>::default(),
quiet: false, quiet: false,
silent: false,
max_warnings: None, max_warnings: None,
warnings_count: Cell::new(0), warnings_count: Cell::new(0),
errors_count: Cell::new(0), errors_count: Cell::new(0),
@ -74,6 +78,12 @@ impl DiagnosticService {
self self
} }
#[must_use]
pub fn with_silent(mut self, yes: bool) -> Self {
self.silent = yes;
self
}
#[must_use] #[must_use]
pub fn with_max_warnings(mut self, max_warnings: Option<usize>) -> Self { pub fn with_max_warnings(mut self, max_warnings: Option<usize>) -> Self {
self.max_warnings = max_warnings; 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) { if let Some(mut err_str) = self.reporter.render_error(diagnostic) {
// Skip large output and print only once // Skip large output and print only once
if err_str.lines().any(|line| line.len() >= 400) { if err_str.lines().any(|line| line.len() >= 400) {