Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions crates/oxc_cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ 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-path")
.long("ignore-path")
.required(false)
.help("This option allows you to specify the file to use as your .eslintignore.")
)
.arg(
Arg::new("ignore-pattern")
.long("ignore-pattern")
Expand Down Expand Up @@ -65,75 +71,72 @@ impl Command {
mod test {
use std::path::PathBuf;

use clap::ArgMatches;

use super::Command;

fn get_lint_matches(arg: &str) -> ArgMatches {
let matches = Command::new().build().try_get_matches_from(arg.split(' ')).unwrap();
let matches = matches.subcommand_matches("lint");
assert!(matches.is_some());
matches.unwrap().clone()
}

#[test]
fn verify_command() {
Command::new().build().debug_assert();
}

#[test]
fn test_lint_path() {
let arg = "oxc lint .";
let matches = Command::new().build().try_get_matches_from(arg.split(' ')).unwrap();
let matches = matches.subcommand_matches("lint");
assert!(matches.is_some());
assert_eq!(matches.unwrap().get_one::<PathBuf>("path"), Some(&PathBuf::from(".")));
let matches = get_lint_matches("oxc lint .");
assert_eq!(matches.get_one::<PathBuf>("path"), Some(&PathBuf::from(".")));
}

#[test]
fn test_lint_multiple_paths() {
let arg = "oxc lint foo bar baz";
let matches = Command::new().build().try_get_matches_from(arg.split(' ')).unwrap();
let matches = matches.subcommand_matches("lint");
assert!(matches.is_some());
let matches = get_lint_matches("oxc lint foo bar baz");
assert_eq!(
matches.unwrap().get_many::<PathBuf>("path").unwrap().collect::<Vec<_>>(),
matches.get_many::<PathBuf>("path").unwrap().collect::<Vec<_>>(),
[&PathBuf::from("foo"), &PathBuf::from("bar"), &PathBuf::from("baz")]
);
}

#[test]
fn test_check_path() {
let arg = "oxc check /path/to/dir";
let matches = Command::new().build().try_get_matches_from(arg.split(' ')).unwrap();
let matches = matches.subcommand_matches("lint");
assert!(matches.is_some());
assert_eq!(
matches.unwrap().get_one::<PathBuf>("path"),
Some(&PathBuf::from("/path/to/dir"))
);
let matches = get_lint_matches("oxc check /path/to/dir");
assert_eq!(matches.get_one::<PathBuf>("path"), Some(&PathBuf::from("/path/to/dir")));
}

#[test]
fn test_quiet_true() {
let arg = "oxc lint foo.js --quiet";
let matches = Command::new().build().try_get_matches_from(arg.split(' ')).unwrap();
let matches = matches.subcommand_matches("lint");
assert!(matches.unwrap().get_flag("quiet"));
let matches = get_lint_matches("oxc lint foo.js --quiet");
assert!(matches.get_flag("quiet"));
}

#[test]
fn test_quiet_false() {
let arg = "oxc lint foo.js";
let matches = Command::new().build().try_get_matches_from(arg.split(' ')).unwrap();
let matches = matches.subcommand_matches("lint");
assert!(!matches.unwrap().get_flag("quiet"));
let matches = get_lint_matches("oxc lint foo.js");
assert!(!matches.get_flag("quiet"));
}

#[test]
fn test_ignore_path() {
let matches = get_lint_matches("oxc lint --ignore-path .gitignore foo.js");
assert_eq!(matches.get_one::<String>("ignore-path"), Some(&".gitignore".to_string()));
}

#[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();
let matches = get_lint_matches("oxc lint --ignore-pattern \"./test\" foo.js");
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 matches = get_lint_matches(
"oxc lint --ignore-pattern \"./test\" --ignore-pattern \"bar.js\" foo.js",
);
let ignore_pattern = matches.get_many::<String>("ignore-pattern").unwrap();
let mut compare = vec![];
for pattern in ignore_pattern {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Cli {
.paths
.iter()
.flat_map(|path| {
Walk::new(path)
Walk::new(path, &self.cli_options)
.iter()
.filter(|path| {
let ignore_pattern = &self.cli_options.ignore_pattern;
Expand Down
8 changes: 7 additions & 1 deletion crates/oxc_cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use glob::Pattern;
pub struct CliOptions {
pub quiet: bool,
pub paths: Vec<PathBuf>,
pub ignore_path: String,
pub ignore_pattern: Vec<Pattern>,
}

Expand All @@ -29,12 +30,17 @@ impl<'a> TryFrom<&'a ArgMatches> for CliOptions {
paths.extend(globbed);
}

let ignore_path = get_ignore_path(matches);
let ignore_pattern = get_ignore_pattern(matches);

Ok(Self { quiet: matches.get_flag("quiet"), paths, ignore_pattern })
Ok(Self { quiet: matches.get_flag("quiet"), paths, ignore_path, ignore_pattern })
}
}

fn get_ignore_path(matches: &ArgMatches) -> String {
matches.get_one::<String>("ignore-path").map_or(".eslintignore".to_string(), ToOwned::to_owned)
}

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};
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_cli/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ use std::path::Path;
use ignore::{DirEntry, WalkBuilder};
use oxc_ast::VALID_EXTENSIONS;

use crate::CliOptions;

pub struct Walk {
inner: ignore::Walk,
}

impl Walk {
pub fn new<P: AsRef<Path>>(path: P) -> Self {
pub fn new<P: AsRef<Path>>(path: P, options: &CliOptions) -> Self {
let inner = WalkBuilder::new(path)
.add_custom_ignore_filename(".eslintignore")
.add_custom_ignore_filename(&options.ignore_path)
.ignore(false)
.git_global(false)
.build();
Expand Down
2 changes: 1 addition & 1 deletion tasks/coverage/typescript
Submodule typescript updated 130 files