refactor(oxlint): create different CliRunResult instead of passing ExitCode to it (#8777)

This commit is contained in:
Sysix 2025-01-30 01:43:42 +00:00
parent 2378fef3cf
commit fe45bee0c2
2 changed files with 26 additions and 18 deletions

View file

@ -2,7 +2,6 @@ use std::{
env, fs, env, fs,
io::{ErrorKind, Write}, io::{ErrorKind, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
process::ExitCode,
time::Instant, time::Instant,
}; };
@ -79,8 +78,7 @@ impl Runner for LintRunner {
// If explicit paths were provided, but all have been // If explicit paths were provided, but all have been
// filtered, return early. // filtered, return early.
if provided_path_count > 0 { if provided_path_count > 0 {
// ToDo: when oxc_linter (config) validates the configuration, we can use exit_code = 1 to fail return CliRunResult::LintNoFilesFound;
return CliRunResult::LintResult(ExitCode::SUCCESS);
} }
paths.push(self.cwd.clone()); paths.push(self.cwd.clone());
@ -218,10 +216,6 @@ impl Runner for LintRunner {
let diagnostic_result = diagnostic_service.run(stdout); 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 { if let Some(end) = output_formatter.lint_command_info(&LintCommandInfo {
number_of_files, number_of_files,
number_of_rules: lint_service.linter().number_of_rules(), number_of_rules: lint_service.linter().number_of_rules(),
@ -232,7 +226,15 @@ impl Runner for LintRunner {
stdout.flush().unwrap(); 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
}
} }
} }

View file

@ -3,11 +3,12 @@ use std::process::{ExitCode, Termination};
#[derive(Debug)] #[derive(Debug)]
pub enum CliRunResult { pub enum CliRunResult {
None, None,
InvalidOptions { InvalidOptions { message: String },
message: String, LintSucceeded,
}, LintFoundErrors,
/// The exit unix code for, in general 0 or 1 (from `--deny-warnings` or `--max-warnings` for example) LintMaxWarningsExceeded,
LintResult(ExitCode), LintNoWarningsAllowed,
LintNoFilesFound,
PrintConfigResult, PrintConfigResult,
ConfigFileInitFailed, ConfigFileInitFailed,
ConfigFileInitSucceeded, ConfigFileInitSucceeded,
@ -17,15 +18,20 @@ impl Termination for CliRunResult {
#[allow(clippy::print_stdout, clippy::print_stderr)] #[allow(clippy::print_stdout, clippy::print_stderr)]
fn report(self) -> ExitCode { fn report(self) -> ExitCode {
match self { match self {
Self::None | Self::PrintConfigResult | Self::ConfigFileInitSucceeded => { Self::None
ExitCode::SUCCESS | Self::PrintConfigResult
} | Self::ConfigFileInitSucceeded
Self::ConfigFileInitFailed => ExitCode::FAILURE, | 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 } => { Self::InvalidOptions { message } => {
println!("Invalid Options: {message}"); println!("Invalid Options: {message}");
ExitCode::FAILURE ExitCode::FAILURE
} }
Self::LintResult(exit_code) => exit_code,
} }
} }
} }