From fe45bee0c274da067566c94dbd6e71a5359df840 Mon Sep 17 00:00:00 2001 From: Sysix <3897725+Sysix@users.noreply.github.com> Date: Thu, 30 Jan 2025 01:43:42 +0000 Subject: [PATCH] refactor(oxlint): create different `CliRunResult` instead of passing `ExitCode` to it (#8777) --- apps/oxlint/src/lint.rs | 18 ++++++++++-------- apps/oxlint/src/result.rs | 26 ++++++++++++++++---------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 0d1201e16c307..870273a43d441 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -2,7 +2,6 @@ use std::{ env, fs, io::{ErrorKind, Write}, path::{Path, PathBuf}, - process::ExitCode, time::Instant, }; @@ -79,8 +78,7 @@ impl Runner for LintRunner { // If explicit paths were provided, but all have been // filtered, return early. if provided_path_count > 0 { - // ToDo: when oxc_linter (config) validates the configuration, we can use exit_code = 1 to fail - return CliRunResult::LintResult(ExitCode::SUCCESS); + return CliRunResult::LintNoFilesFound; } paths.push(self.cwd.clone()); @@ -218,10 +216,6 @@ impl Runner for LintRunner { let diagnostic_result = diagnostic_service.run(stdout); - let diagnostic_failed = diagnostic_result.max_warnings_exceeded() - || diagnostic_result.errors_count() > 0 - || (warning_options.deny_warnings && diagnostic_result.warnings_count() > 0); - if let Some(end) = output_formatter.lint_command_info(&LintCommandInfo { number_of_files, number_of_rules: lint_service.linter().number_of_rules(), @@ -232,7 +226,15 @@ impl Runner for LintRunner { stdout.flush().unwrap(); }; - CliRunResult::LintResult(ExitCode::from(u8::from(diagnostic_failed))) + if diagnostic_result.errors_count() > 0 { + CliRunResult::LintFoundErrors + } else if warning_options.deny_warnings && diagnostic_result.warnings_count() > 0 { + CliRunResult::LintNoWarningsAllowed + } else if diagnostic_result.max_warnings_exceeded() { + CliRunResult::LintMaxWarningsExceeded + } else { + CliRunResult::LintSucceeded + } } } diff --git a/apps/oxlint/src/result.rs b/apps/oxlint/src/result.rs index 9fd26561cce66..289083c05ba78 100644 --- a/apps/oxlint/src/result.rs +++ b/apps/oxlint/src/result.rs @@ -3,11 +3,12 @@ use std::process::{ExitCode, Termination}; #[derive(Debug)] pub enum CliRunResult { None, - InvalidOptions { - message: String, - }, - /// The exit unix code for, in general 0 or 1 (from `--deny-warnings` or `--max-warnings` for example) - LintResult(ExitCode), + InvalidOptions { message: String }, + LintSucceeded, + LintFoundErrors, + LintMaxWarningsExceeded, + LintNoWarningsAllowed, + LintNoFilesFound, PrintConfigResult, ConfigFileInitFailed, ConfigFileInitSucceeded, @@ -17,15 +18,20 @@ impl Termination for CliRunResult { #[allow(clippy::print_stdout, clippy::print_stderr)] fn report(self) -> ExitCode { match self { - Self::None | Self::PrintConfigResult | Self::ConfigFileInitSucceeded => { - ExitCode::SUCCESS - } - Self::ConfigFileInitFailed => ExitCode::FAILURE, + Self::None + | Self::PrintConfigResult + | Self::ConfigFileInitSucceeded + | Self::LintSucceeded + // ToDo: when oxc_linter (config) validates the configuration, we can use exit_code = 1 to fail + | Self::LintNoFilesFound => ExitCode::SUCCESS, + Self::ConfigFileInitFailed + | Self::LintFoundErrors + | Self::LintNoWarningsAllowed + | Self::LintMaxWarningsExceeded => ExitCode::FAILURE, Self::InvalidOptions { message } => { println!("Invalid Options: {message}"); ExitCode::FAILURE } - Self::LintResult(exit_code) => exit_code, } } }