From 03df8fcc681e443fdf348dcefb5e73eb863def26 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 21 Aug 2023 10:44:32 +0800 Subject: [PATCH] refactor(cli): print the final result; fix plural on number of files --- crates/oxc_cli/Cargo.toml | 1 - crates/oxc_cli/src/lib.rs | 8 +--- crates/oxc_cli/src/result.rs | 88 ++++++++++++++++++++++++++++++++++ crates/oxc_cli/src/runner.rs | 91 +----------------------------------- 4 files changed, 91 insertions(+), 97 deletions(-) create mode 100644 crates/oxc_cli/src/result.rs diff --git a/crates/oxc_cli/Cargo.toml b/crates/oxc_cli/Cargo.toml index 0cfd724fb..8cc88b648 100644 --- a/crates/oxc_cli/Cargo.toml +++ b/crates/oxc_cli/Cargo.toml @@ -38,4 +38,3 @@ ignore = { workspace = true, features = ["simd-accel"] } miette = { workspace = true, features = ["fancy-no-backtrace"] } rayon = { workspace = true } bpaf = { workspace = true, features = ["derive", "autocomplete", "bright-color"] } -# git2 = { version = "0.16.1", default_features = false } diff --git a/crates/oxc_cli/src/lib.rs b/crates/oxc_cli/src/lib.rs index 0b98d26a6..edf7318f8 100644 --- a/crates/oxc_cli/src/lib.rs +++ b/crates/oxc_cli/src/lib.rs @@ -1,14 +1,10 @@ -// mod git; mod command; mod lint; +mod result; mod runner; mod type_check; mod walk; pub use crate::{ - command::*, - lint::LintRunner, - runner::{CliRunResult, Runner}, - type_check::TypeCheckRunner, - walk::Walk, + command::*, lint::LintRunner, result::CliRunResult, runner::Runner, type_check::TypeCheckRunner, }; diff --git a/crates/oxc_cli/src/result.rs b/crates/oxc_cli/src/result.rs new file mode 100644 index 000000000..0df3b413d --- /dev/null +++ b/crates/oxc_cli/src/result.rs @@ -0,0 +1,88 @@ +use std::{ + path::PathBuf, + process::{ExitCode, Termination}, + time::Duration, +}; + +#[derive(Debug)] +pub enum CliRunResult { + None, + IOError(crate::lint::Error), + PathNotFound { + paths: Vec, + }, + LintResult { + duration: Duration, + number_of_rules: usize, + number_of_files: usize, + number_of_warnings: usize, + number_of_errors: usize, + max_warnings_exceeded: bool, + }, + TypeCheckResult { + duration: Duration, + number_of_diagnostics: usize, + }, +} + +impl Termination for CliRunResult { + fn report(self) -> ExitCode { + match self { + Self::None => ExitCode::from(0), + Self::PathNotFound { paths } => { + println!("Path {paths:?} does not exist."); + ExitCode::from(1) + } + Self::IOError(e) => { + println!("IO Error: {e}"); + ExitCode::from(1) + } + Self::LintResult { + duration, + number_of_rules, + number_of_files, + number_of_warnings, + number_of_errors, + max_warnings_exceeded, + } => { + let ms = duration.as_millis(); + let threads = rayon::current_num_threads(); + let number_of_diagnostics = number_of_warnings + number_of_errors; + + if number_of_diagnostics > 0 { + println!(); + } + + let s = if number_of_files == 1 { "" } else { "s" }; + println!( + "Finished in {ms}ms on {number_of_files} file{s} with {number_of_rules} rules using {threads} threads." + ); + + if max_warnings_exceeded { + println!("Exceeded maximum number of warnings. Found {number_of_warnings}."); + return ExitCode::from(1); + } + + println!( + "Found {number_of_warnings} warning{} and {number_of_errors} error{}.", + if number_of_warnings == 1 { "" } else { "s" }, + if number_of_errors == 1 { "" } else { "s" } + ); + + let exit_code = u8::from(number_of_diagnostics > 0); + ExitCode::from(exit_code) + } + Self::TypeCheckResult { duration, number_of_diagnostics } => { + let ms = duration.as_millis(); + println!("Finished in {ms}ms."); + + if number_of_diagnostics > 0 { + println!("Found {number_of_diagnostics} errors."); + return ExitCode::from(1); + } + + ExitCode::from(0) + } + } + } +} diff --git a/crates/oxc_cli/src/runner.rs b/crates/oxc_cli/src/runner.rs index 135991937..c45035560 100644 --- a/crates/oxc_cli/src/runner.rs +++ b/crates/oxc_cli/src/runner.rs @@ -1,7 +1,4 @@ -use std::{ - path::PathBuf, - process::{ExitCode, Termination}, -}; +use crate::CliRunResult; /// A trait for exposing functionality to the CLI. pub trait Runner: Send + Sync { @@ -12,89 +9,3 @@ pub trait Runner: Send + Sync { /// Executes the runner, providing some result to the CLI. fn run(self) -> CliRunResult; } - -#[derive(Debug)] -pub enum CliRunResult { - None, - IOError(crate::lint::Error), - PathNotFound { - paths: Vec, - }, - LintResult { - duration: std::time::Duration, - number_of_rules: usize, - number_of_files: usize, - number_of_warnings: usize, - number_of_errors: usize, - max_warnings_exceeded: bool, - }, - TypeCheckResult { - duration: std::time::Duration, - number_of_diagnostics: usize, - }, -} - -impl Termination for CliRunResult { - fn report(self) -> ExitCode { - match self { - Self::None => ExitCode::from(0), - Self::PathNotFound { paths } => { - println!("Path {paths:?} does not exist."); - ExitCode::from(1) - } - Self::IOError(e) => { - println!("IO Error: {e}"); - ExitCode::from(1) - } - Self::LintResult { - duration, - number_of_rules, - number_of_files, - number_of_warnings, - number_of_errors, - max_warnings_exceeded, - } => { - let ms = duration.as_millis(); - let threads = rayon::current_num_threads(); - let number_of_diagnostics = number_of_warnings + number_of_errors; - - if number_of_diagnostics > 0 { - println!(); - } - - println!( - "Finished in {ms}ms on {number_of_files} files with {number_of_rules} rules using {threads} threads." - ); - - if max_warnings_exceeded { - println!("Exceeded maximum number of warnings. Found {number_of_warnings}."); - return ExitCode::from(1); - } - - if number_of_diagnostics > 0 { - let warnings = if number_of_warnings == 1 { "warning" } else { "warnings" }; - let errors = if number_of_errors == 1 { "error" } else { "errors" }; - println!( - "Found {number_of_warnings} {warnings} and {number_of_errors} {errors}." - ); - return ExitCode::from(1); - } - - // eslint does not print anything after success, so we do the same. - // It is also standard to not print anything after success in the *nix world. - ExitCode::from(0) - } - Self::TypeCheckResult { duration, number_of_diagnostics } => { - let ms = duration.as_millis(); - println!("Finished in {ms}ms."); - - if number_of_diagnostics > 0 { - println!("Found {number_of_diagnostics} errors."); - return ExitCode::from(1); - } - - ExitCode::from(0) - } - } - } -}