From c2fb3613bdfaf403ed3715883a2adaa01ce68ca3 Mon Sep 17 00:00:00 2001 From: Xuan <97ssps30212@gmail.com> Date: Tue, 28 Feb 2023 13:33:56 +0800 Subject: [PATCH] feat(oxc_cli): support ignore-pattern --- crates/oxc_cli/src/command.rs | 31 ++++++++++++++++++++++++++++++- crates/oxc_cli/src/lib.rs | 16 +++++++++++++++- crates/oxc_cli/src/options.rs | 18 +++++++++++++++++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/crates/oxc_cli/src/command.rs b/crates/oxc_cli/src/command.rs index 54ea48948..badaeadd2 100644 --- a/crates/oxc_cli/src/command.rs +++ b/crates/oxc_cli/src/command.rs @@ -1,4 +1,4 @@ -use clap::{builder::ValueParser, Arg, Command as ClapCommand}; +use clap::{builder::ValueParser, Arg, ArgAction, Command as ClapCommand}; #[derive(Debug)] pub struct Command { @@ -37,6 +37,13 @@ impl Command { .action(clap::ArgAction::SetTrue) .help("This option allows you to disable reporting on warnings. If you enable this option, only errors are reported by oxc_lint.") ) + .arg( + Arg::new("ignore-pattern") + .long("ignore-pattern") + .required(false) + .action(ArgAction::Append) + .help("This option allows you to specify patterns of files to ignore (in addition to those in .eslintignore).") + ) .arg( Arg::new("path") .value_name("PATH") @@ -113,4 +120,26 @@ mod test { let matches = matches.subcommand_matches("lint"); assert!(!matches.unwrap().get_flag("quiet")); } + + #[test] + fn test_single_ignore_pattern() { + let arg = "oxc lint --ignore-pattern \"./test\" foo.js"; + let matches = Command::new().build().try_get_matches_from(arg.split(' ')).unwrap(); + let matches = matches.subcommand_matches("lint").unwrap(); + assert_eq!(matches.get_one::("ignore-pattern"), Some(&"\"./test\"".to_string())); + } + + #[test] + fn test_multiple_ignore_pattern() { + let arg = "oxc lint --ignore-pattern \"./test\" --ignore-pattern \"bar.js\" foo.js"; + let matches = Command::new().build().try_get_matches_from(arg.split(' ')).unwrap(); + let matches = matches.subcommand_matches("lint").unwrap(); + let ignore_pattern = matches.get_many::("ignore-pattern").unwrap(); + let mut compare = vec![]; + for pattern in ignore_pattern { + compare.push(pattern); + } + + assert_eq!(compare, vec!["\"./test\"", "\"bar.js\""]); + } } diff --git a/crates/oxc_cli/src/lib.rs b/crates/oxc_cli/src/lib.rs index 9d34a1bee..ce16cc954 100644 --- a/crates/oxc_cli/src/lib.rs +++ b/crates/oxc_cli/src/lib.rs @@ -38,7 +38,21 @@ impl Cli { .cli_options .paths .iter() - .flat_map(|path| Walk::new(path).iter().collect::>()) + .flat_map(|path| { + Walk::new(path) + .iter() + .filter(|path| { + let ignore_pattern = &self.cli_options.ignore_pattern; + for pattern in ignore_pattern { + if pattern.matches_path(path) { + return false; + } + } + + true + }) + .collect::>() + }) .collect::>(); let number_of_diagnostics = paths diff --git a/crates/oxc_cli/src/options.rs b/crates/oxc_cli/src/options.rs index 1b8fcf6d2..27b0b13d7 100644 --- a/crates/oxc_cli/src/options.rs +++ b/crates/oxc_cli/src/options.rs @@ -1,10 +1,12 @@ use std::path::PathBuf; use clap::ArgMatches; +use glob::Pattern; pub struct CliOptions { pub quiet: bool, pub paths: Vec, + pub ignore_pattern: Vec, } impl<'a> TryFrom<&'a ArgMatches> for CliOptions { @@ -27,6 +29,20 @@ impl<'a> TryFrom<&'a ArgMatches> for CliOptions { paths.extend(globbed); } - Ok(Self { quiet: matches.get_flag("quiet"), paths }) + let ignore_pattern = get_ignore_pattern(matches); + + Ok(Self { quiet: matches.get_flag("quiet"), paths, ignore_pattern }) } } + +fn get_ignore_pattern(matches: &ArgMatches) -> Vec { + let mut result = vec![]; + let Some(ignore_pattern) = matches.get_many::("ignore-pattern") else {return result}; + for pattern in ignore_pattern { + if let Ok(pattern) = Pattern::new(pattern) { + result.push(pattern); + } + } + + result +}