diff options
-rw-r--r-- | cli/flags.rs | 22 | ||||
-rw-r--r-- | cli/fmt.rs | 23 | ||||
-rw-r--r-- | cli/main.rs | 8 | ||||
-rw-r--r-- | cli/tests/integration_tests.rs | 15 |
4 files changed, 58 insertions, 10 deletions
diff --git a/cli/flags.rs b/cli/flags.rs index efd0a5c63..80eeba217 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -39,6 +39,7 @@ pub enum DenoSubcommand { Fmt { check: bool, files: Vec<String>, + ignore: Vec<String>, }, Help, Info { @@ -101,6 +102,7 @@ pub struct Flags { pub ca_file: Option<String>, pub cached_only: bool, pub config_path: Option<String>, + pub ignore: Vec<String>, pub import_map_path: Option<String>, pub inspect: Option<SocketAddr>, pub inspect_brk: Option<SocketAddr>, @@ -338,13 +340,20 @@ fn types_parse(flags: &mut Flags, matches: &clap::ArgMatches) { } fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { + // TODO(divy-work): remove `--unstable` in 1.3.0 + unstable_arg_parse(flags, matches); let files = match matches.values_of("files") { Some(f) => f.map(String::from).collect(), None => vec![], }; + let ignore = match matches.values_of("ignore") { + Some(f) => f.map(String::from).collect(), + None => vec![], + }; flags.subcommand = DenoSubcommand::Fmt { check: matches.is_present("check"), files, + ignore, } } @@ -659,6 +668,16 @@ Ignore formatting a file by adding an ignore comment at the top of the file: .takes_value(false), ) .arg( + Arg::with_name("ignore") + .long("ignore") + .requires("unstable") + .takes_value(true) + .use_delimiter(true) + .require_equals(true) + .help("Ignore formatting particular source files. Use with --unstable"), + ) + .arg(unstable_arg()) + .arg( Arg::with_name("files") .takes_value(true) .multiple(true) @@ -1665,6 +1684,7 @@ mod tests { r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt { + ignore: vec![], check: false, files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()] }, @@ -1677,6 +1697,7 @@ mod tests { r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt { + ignore: vec![], check: true, files: vec![], }, @@ -1689,6 +1710,7 @@ mod tests { r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt { + ignore: vec![], check: false, files: vec![], }, diff --git a/cli/fmt.rs b/cli/fmt.rs index 59239ec17..cac4e5c5f 100644 --- a/cli/fmt.rs +++ b/cli/fmt.rs @@ -27,15 +27,24 @@ const BOM_CHAR: char = '\u{FEFF}'; /// Format JavaScript/TypeScript files. /// -/// First argument supports globs, and if it is `None` +/// 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>, check: bool) -> Result<(), ErrBox> { +pub async fn format( + args: Vec<String>, + check: bool, + exclude: Vec<String>, +) -> Result<(), ErrBox> { if args.len() == 1 && args[0] == "-" { return format_stdin(check); } - - let target_files = collect_files(args)?; - + // collect all files provided. + let mut target_files = collect_files(args)?; + if !exclude.is_empty() { + // collect all files to be ignored + // and retain only files that should be formatted. + let ignore_files = collect_files(exclude)?; + target_files.retain(|f| !ignore_files.contains(&f)); + } let config = get_config(); if check { check_source_files(config, target_files).await @@ -217,9 +226,9 @@ pub fn collect_files(files: Vec<String>) -> Result<Vec<PathBuf>, ErrBox> { for arg in files { let p = PathBuf::from(arg); if p.is_dir() { - target_files.extend(files_in_subtree(p, is_supported)); + target_files.extend(files_in_subtree(p.canonicalize()?, is_supported)); } else { - target_files.push(p); + target_files.push(p.canonicalize()?); }; } } diff --git a/cli/main.rs b/cli/main.rs index 4a87db922..3a6000c2b 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -719,9 +719,11 @@ pub fn main() { DenoSubcommand::Cache { files } => { cache_command(flags, files).boxed_local() } - DenoSubcommand::Fmt { check, files } => { - fmt::format(files, check).boxed_local() - } + DenoSubcommand::Fmt { + check, + files, + ignore, + } => fmt::format(files, check, ignore).boxed_local(), DenoSubcommand::Info { file, json } => { info_command(flags, file, json).boxed_local() } diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index ea76fe23b..89b5cc21e 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -352,6 +352,20 @@ fn fmt_test() { let badly_formatted_str = badly_formatted.to_str().unwrap(); std::fs::copy(&badly_formatted_original, &badly_formatted) .expect("Failed to copy file"); + // First, check formatting by ignoring the badly formatted file. + let status = util::deno_cmd() + .current_dir(util::root_path()) + .arg("fmt") + .arg(format!("--ignore={}", badly_formatted_str)) + .arg("--unstable") + .arg("--check") + .arg(badly_formatted_str) + .spawn() + .expect("Failed to spawn script") + .wait() + .expect("Failed to wait for child process"); + assert!(status.success()); + // Check without ignore. let status = util::deno_cmd() .current_dir(util::root_path()) .arg("fmt") @@ -362,6 +376,7 @@ fn fmt_test() { .wait() .expect("Failed to wait for child process"); assert!(!status.success()); + // Format the source file. let status = util::deno_cmd() .current_dir(util::root_path()) .arg("fmt") |