feat(oxc_cli): support ignore-pattern

This commit is contained in:
Xuan 2023-02-28 13:33:56 +08:00 committed by Boshen
parent a3ffee8b21
commit c2fb3613bd
3 changed files with 62 additions and 3 deletions

View file

@ -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::<String>("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::<String>("ignore-pattern").unwrap();
let mut compare = vec![];
for pattern in ignore_pattern {
compare.push(pattern);
}
assert_eq!(compare, vec!["\"./test\"", "\"bar.js\""]);
}
}

View file

@ -38,7 +38,21 @@ impl Cli {
.cli_options
.paths
.iter()
.flat_map(|path| Walk::new(path).iter().collect::<Vec<_>>())
.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::<Vec<_>>()
})
.collect::<Vec<_>>();
let number_of_diagnostics = paths

View file

@ -1,10 +1,12 @@
use std::path::PathBuf;
use clap::ArgMatches;
use glob::Pattern;
pub struct CliOptions {
pub quiet: bool,
pub paths: Vec<PathBuf>,
pub ignore_pattern: Vec<Pattern>,
}
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<Pattern> {
let mut result = vec![];
let Some(ignore_pattern) = matches.get_many::<String>("ignore-pattern") else {return result};
for pattern in ignore_pattern {
if let Ok(pattern) = Pattern::new(pattern) {
result.push(pattern);
}
}
result
}