refactor(cli): clean up and report total number of diagnostics

This commit is contained in:
Boshen 2023-02-26 01:48:05 +08:00
parent 915518b614
commit 78de22a3af
3 changed files with 50 additions and 58 deletions

View file

@ -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<Report>,
}
impl Cli {
pub fn lint<P: AsRef<Path>>(path: P) -> Option<Vec<LintResult>> {
pub fn lint<P: AsRef<Path>>(path: P) -> CliRunResult {
let paths = Walk::new(path).iter().collect::<Vec<_>>();
if paths.is_empty() {
return None;
}
let result: Vec<LintResult> = 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<Report> {
fn lint_path(path: &Path) -> Vec<Error> {
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:?}");

View file

@ -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<LintResult>),
}
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,
}

View file

@ -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)
}
}
}
}