diff --git a/crates/oxc_cli/fixtures/linter/eslintrc.json b/crates/oxc_cli/fixtures/linter/eslintrc.json new file mode 100644 index 000000000..a63d73a14 --- /dev/null +++ b/crates/oxc_cli/fixtures/linter/eslintrc.json @@ -0,0 +1,5 @@ +{ + "rules": { + "no-debugger": "error" + } +} diff --git a/crates/oxc_cli/src/command/mod.rs b/crates/oxc_cli/src/command/mod.rs index 7ec0ceaee..d2c357621 100644 --- a/crates/oxc_cli/src/command/mod.rs +++ b/crates/oxc_cli/src/command/mod.rs @@ -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, diff --git a/crates/oxc_cli/src/lint/mod.rs b/crates/oxc_cli/src/lint/mod.rs index 5d4e0df08..4c5897213 100644 --- a/crates/oxc_cli/src/lint/mod.rs +++ b/crates/oxc_cli/src/lint/mod.rs @@ -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"]; diff --git a/crates/oxc_diagnostics/src/service.rs b/crates/oxc_diagnostics/src/service.rs index 6cef88a91..199caf5b4 100644 --- a/crates/oxc_diagnostics/src/service.rs +++ b/crates/oxc_diagnostics/src/service.rs @@ -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, @@ -42,6 +45,7 @@ impl Default for DiagnosticService { Self { reporter: Box::::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) -> 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) {