From 0b6b0b4854e605045f887b9cf95efc6aea9f732a Mon Sep 17 00:00:00 2001 From: Shannon Rothe Date: Tue, 21 Nov 2023 13:08:10 +1100 Subject: [PATCH] feat(cli): add `--deny-warnings` flag (#1452) See #1403 Terminates linting with an exit code of 1 if the `--deny-warnings` flag is provided _and_ there is 1 or more warnings produced from the linting. --- crates/oxc_cli/src/command.rs | 4 ++++ crates/oxc_cli/src/lint/mod.rs | 1 + crates/oxc_cli/src/result.rs | 5 ++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/oxc_cli/src/command.rs b/crates/oxc_cli/src/command.rs index 06b0f1736..582252a75 100644 --- a/crates/oxc_cli/src/command.rs +++ b/crates/oxc_cli/src/command.rs @@ -235,6 +235,10 @@ pub struct WarningOptions { #[bpaf(switch, hide_usage)] pub quiet: bool, + /// Ensure warnings produce a non-zero exit code + #[bpaf(switch, hide_usage)] + pub deny_warnings: 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 #[bpaf(argument("INT"), hide_usage)] diff --git a/crates/oxc_cli/src/lint/mod.rs b/crates/oxc_cli/src/lint/mod.rs index 4bcb8d198..03910bcbc 100644 --- a/crates/oxc_cli/src/lint/mod.rs +++ b/crates/oxc_cli/src/lint/mod.rs @@ -93,6 +93,7 @@ impl Runner for LintRunner { number_of_warnings: diagnostic_service.warnings_count(), number_of_errors: diagnostic_service.errors_count(), max_warnings_exceeded: diagnostic_service.max_warnings_exceeded(), + deny_warnings: warning_options.deny_warnings, }) } } diff --git a/crates/oxc_cli/src/result.rs b/crates/oxc_cli/src/result.rs index 32e00d58a..54ae28c88 100644 --- a/crates/oxc_cli/src/result.rs +++ b/crates/oxc_cli/src/result.rs @@ -22,6 +22,7 @@ pub struct LintResult { pub number_of_warnings: usize, pub number_of_errors: usize, pub max_warnings_exceeded: bool, + pub deny_warnings: bool, } #[derive(Debug)] @@ -49,6 +50,7 @@ impl Termination for CliRunResult { number_of_warnings, number_of_errors, max_warnings_exceeded, + deny_warnings, }) => { let threads = rayon::current_num_threads(); let number_of_diagnostics = number_of_warnings + number_of_errors; @@ -74,7 +76,8 @@ impl Termination for CliRunResult { if number_of_errors == 1 { "" } else { "s" } ); - let exit_code = u8::from(number_of_errors > 0); + let exit_code = + u8::from((number_of_warnings > 0 && deny_warnings) || number_of_errors > 0); ExitCode::from(exit_code) } Self::FormatResult(FormatResult { duration, number_of_files }) => {