diff --git a/crates/oxc_cli/src/lib.rs b/crates/oxc_cli/src/lib.rs index e14580e25..091ba51c1 100644 --- a/crates/oxc_cli/src/lib.rs +++ b/crates/oxc_cli/src/lib.rs @@ -1,45 +1,41 @@ mod command; +mod result; mod walk; -use std::{fs, path::Path, path::PathBuf, rc::Rc}; +use std::{fs, path::Path, rc::Rc}; -pub use command::Command; use oxc_allocator::Allocator; use oxc_ast::SourceType; -use oxc_diagnostics::miette::Report; +use oxc_diagnostics::Error; use oxc_linter::Linter; use oxc_parser::Parser; use oxc_semantic::SemanticBuilder; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use walk::Walk; +pub use crate::{command::Command, result::CliRunResult}; + pub struct Cli; -#[derive(Debug)] -pub struct LintResult { - pub path: PathBuf, - pub diagnostics: Vec, -} - impl Cli { - pub fn lint>(path: P) -> Option> { + pub fn lint>(path: P) -> CliRunResult { let paths = Walk::new(path).iter().collect::>(); - if paths.is_empty() { - return None; - } - - let result: Vec = paths + let number_of_diagnostics = paths .par_iter() .map(|path| { let diagnostics = Self::lint_path(path); - LintResult { path: path.to_path_buf(), diagnostics } + for diagnostic in &diagnostics { + println!("{diagnostic:?}"); + } + diagnostics.len() }) - .collect(); - Some(result) + .sum(); + + CliRunResult::LintResult { number_of_files: paths.len(), number_of_diagnostics } } - fn lint_path(path: &Path) -> Vec { + fn lint_path(path: &Path) -> Vec { let source_text = fs::read_to_string(path).expect("{name} not found"); let allocator = Allocator::default(); let source_type = SourceType::from_path(path).expect("incorrect {path:?}"); diff --git a/crates/oxc_cli/src/main.rs b/crates/oxc_cli/src/main.rs index c41caf018..46e610968 100644 --- a/crates/oxc_cli/src/main.rs +++ b/crates/oxc_cli/src/main.rs @@ -9,44 +9,8 @@ static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc; static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; use std::path::PathBuf; -use std::process::{ExitCode, Termination}; -use oxc_cli::{Cli, Command, LintResult}; - -#[derive(Debug)] -pub enum CliRunResult { - None, - PathNotFound { path: PathBuf }, - LintResult(Vec), -} - -impl Termination for CliRunResult { - fn report(self) -> ExitCode { - match self { - Self::None => ExitCode::from(0), - Self::PathNotFound { path } => { - println!("Path {} does not exist.", path.to_string_lossy()); - ExitCode::from(1) - } - Self::LintResult(results) => { - println!("Checked {} files.", results.len()); - - let has_errors = results.iter().any(|result| !result.diagnostics.is_empty()); - - if has_errors { - for LintResult { path, diagnostics } in results { - println!("File: {path:?}"); - for diagnostic in diagnostics { - println!("{diagnostic:?}"); - } - } - return ExitCode::from(1); - } - ExitCode::from(0) - } - } - } -} +use oxc_cli::{Cli, CliRunResult, Command}; fn main() -> CliRunResult { match Command::new().build().get_matches().subcommand() { @@ -57,8 +21,7 @@ fn main() -> CliRunResult { return CliRunResult::PathNotFound { path: path.clone() }; } - let result = Cli::lint(path); - CliRunResult::LintResult(result.unwrap()) + Cli::lint(path) } _ => CliRunResult::None, } diff --git a/crates/oxc_cli/src/result.rs b/crates/oxc_cli/src/result.rs new file mode 100644 index 000000000..f12c5e712 --- /dev/null +++ b/crates/oxc_cli/src/result.rs @@ -0,0 +1,33 @@ +use std::{ + path::PathBuf, + process::{ExitCode, Termination}, +}; + +#[derive(Debug)] +pub enum CliRunResult { + None, + PathNotFound { path: PathBuf }, + LintResult { number_of_files: usize, number_of_diagnostics: usize }, +} + +impl Termination for CliRunResult { + fn report(self) -> ExitCode { + match self { + Self::None => ExitCode::from(0), + Self::PathNotFound { path } => { + println!("Path {} does not exist.", path.to_string_lossy()); + ExitCode::from(1) + } + Self::LintResult { number_of_files, number_of_diagnostics } => { + println!("Checked {number_of_files} files."); + + if number_of_files > 0 { + println!("Found {number_of_diagnostics} diagnostics."); + return ExitCode::from(1); + } + + ExitCode::from(0) + } + } + } +}