Skip to content

Commit

Permalink
refactor(oxlint): create different CliRunResult instead of passing …
Browse files Browse the repository at this point in the history
…`ExitCode` to it
  • Loading branch information
Sysix committed Jan 29, 2025
1 parent 36acc12 commit 38d7e11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
18 changes: 10 additions & 8 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{
env, fs,
io::{ErrorKind, Write},
path::{Path, PathBuf},
process::ExitCode,
time::Instant,
};

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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(),
Expand All @@ -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
}
}
}

Expand Down
21 changes: 15 additions & 6 deletions apps/oxlint/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ pub enum CliRunResult {
message: String,
},
/// The exit unix code for, in general 0 or 1 (from `--deny-warnings` or `--max-warnings` for example)
LintResult(ExitCode),
LintSucceeded,
LintFoundErrors,
LintMaxWarningsExceeded,
LintNoWarningsAllowed,
LintNoFilesFound,
PrintConfigResult,
ConfigFileInitFailed,
ConfigFileInitSucceeded,
Expand All @@ -17,15 +21,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,
}
}
}

0 comments on commit 38d7e11

Please sign in to comment.