diff options
author | Akshat Agarwal <humancalico@disroot.org> | 2020-10-21 16:42:01 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-21 13:12:01 +0200 |
commit | d6c824a6c38e4d443a7f050e2f3b7703f98b2cdc (patch) | |
tree | 838d2bec35c12fba207ba5726cb784333d5d9493 | |
parent | 0fb39f9176c5bfb6a8cd50addd85d596fe93c459 (diff) |
refactor(cli): use PathBuf instead of String for lint and fmt subcommands (#8042)
-rw-r--r-- | cli/flags.rs | 35 | ||||
-rw-r--r-- | cli/fmt.rs | 18 | ||||
-rw-r--r-- | cli/lint.rs | 6 | ||||
-rw-r--r-- | cli/main.rs | 4 |
4 files changed, 36 insertions, 27 deletions
diff --git a/cli/flags.rs b/cli/flags.rs index 440c2aeea..07707ae6c 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -40,8 +40,8 @@ pub enum DenoSubcommand { }, Fmt { check: bool, - files: Vec<String>, - ignore: Vec<String>, + files: Vec<PathBuf>, + ignore: Vec<PathBuf>, }, Info { json: bool, @@ -55,8 +55,8 @@ pub enum DenoSubcommand { force: bool, }, Lint { - files: Vec<String>, - ignore: Vec<String>, + files: Vec<PathBuf>, + ignore: Vec<PathBuf>, rules: bool, json: bool, }, @@ -106,7 +106,7 @@ pub struct Flags { pub cached_only: bool, pub config_path: Option<String>, pub coverage: bool, - pub ignore: Vec<String>, + pub ignore: Vec<PathBuf>, pub import_map_path: Option<String>, pub inspect: Option<SocketAddr>, pub inspect_brk: Option<SocketAddr>, @@ -357,11 +357,11 @@ fn types_parse(flags: &mut Flags, _matches: &clap::ArgMatches) { fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { let files = match matches.values_of("files") { - Some(f) => f.map(String::from).collect(), + Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let ignore = match matches.values_of("ignore") { - Some(f) => f.map(String::from).collect(), + Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; flags.subcommand = DenoSubcommand::Fmt { @@ -639,11 +639,11 @@ fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) { fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) { let files = match matches.values_of("files") { - Some(f) => f.map(String::from).collect(), + Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let ignore = match matches.values_of("ignore") { - Some(f) => f.map(String::from).collect(), + Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let rules = matches.is_present("rules"); @@ -1781,7 +1781,10 @@ mod tests { subcommand: DenoSubcommand::Fmt { ignore: vec![], check: false, - files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()], + files: vec![ + PathBuf::from("script_1.ts"), + PathBuf::from("script_2.ts") + ], }, ..Flags::default() } @@ -1827,7 +1830,10 @@ mod tests { r.unwrap(), Flags { subcommand: DenoSubcommand::Lint { - files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()], + files: vec![ + PathBuf::from("script_1.ts"), + PathBuf::from("script_2.ts") + ], rules: false, json: false, ignore: vec![], @@ -1850,7 +1856,10 @@ mod tests { files: vec![], rules: false, json: false, - ignore: svec!["script_1.ts", "script_2.ts"], + ignore: vec![ + PathBuf::from("script_1.ts"), + PathBuf::from("script_2.ts") + ], }, unstable: true, ..Flags::default() @@ -1883,7 +1892,7 @@ mod tests { r.unwrap(), Flags { subcommand: DenoSubcommand::Lint { - files: vec!["script_1.ts".to_string()], + files: vec![PathBuf::from("script_1.ts")], rules: false, json: true, ignore: vec![], diff --git a/cli/fmt.rs b/cli/fmt.rs index 452ae20b5..1ebf27900 100644 --- a/cli/fmt.rs +++ b/cli/fmt.rs @@ -32,11 +32,11 @@ const BOM_CHAR: char = '\u{FEFF}'; /// First argument and ignore supports globs, and if it is `None` /// then the current directory is recursively walked. pub async fn format( - args: Vec<String>, + args: Vec<PathBuf>, check: bool, - exclude: Vec<String>, + exclude: Vec<PathBuf>, ) -> Result<(), AnyError> { - if args.len() == 1 && args[0] == "-" { + if args.len() == 1 && args[0].to_string_lossy() == "-" { return format_stdin(check); } // collect all files provided. @@ -232,7 +232,7 @@ fn is_supported(path: &Path) -> bool { } pub fn collect_files( - files: Vec<String>, + files: Vec<PathBuf>, ) -> Result<Vec<PathBuf>, std::io::Error> { let mut target_files: Vec<PathBuf> = vec![]; @@ -242,12 +242,12 @@ pub fn collect_files( is_supported, )); } else { - for arg in files { - let p = PathBuf::from(arg); - if p.is_dir() { - target_files.extend(files_in_subtree(p.canonicalize()?, is_supported)); + for file in files { + if file.is_dir() { + target_files + .extend(files_in_subtree(file.canonicalize()?, is_supported)); } else { - target_files.push(p.canonicalize()?); + target_files.push(file.canonicalize()?); }; } } diff --git a/cli/lint.rs b/cli/lint.rs index a2a1252c0..b7ab7baa4 100644 --- a/cli/lint.rs +++ b/cli/lint.rs @@ -40,11 +40,11 @@ fn create_reporter(kind: LintReporterKind) -> Box<dyn LintReporter + Send> { } pub async fn lint_files( - args: Vec<String>, - ignore: Vec<String>, + args: Vec<PathBuf>, + ignore: Vec<PathBuf>, json: bool, ) -> Result<(), AnyError> { - if args.len() == 1 && args[0] == "-" { + if args.len() == 1 && args[0].to_string_lossy() == "-" { return lint_stdin(json); } let mut target_files = collect_files(args)?; diff --git a/cli/main.rs b/cli/main.rs index 1e174abd7..9080bc6cb 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -217,9 +217,9 @@ async fn install_command( async fn lint_command( flags: Flags, - files: Vec<String>, + files: Vec<PathBuf>, list_rules: bool, - ignore: Vec<String>, + ignore: Vec<PathBuf>, json: bool, ) -> Result<(), AnyError> { if !flags.unstable { |