mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
feat(unstable): add "--ignore" flag to deno fmt (#6890)
This commit is contained in:
parent
95597fc6e2
commit
0da4779b17
4 changed files with 58 additions and 10 deletions
22
cli/flags.rs
22
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,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -658,6 +667,16 @@ Ignore formatting a file by adding an ignore comment at the top of the file:
|
|||
.help("Check if the source files are formatted.")
|
||||
.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)
|
||||
|
@ -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![],
|
||||
},
|
||||
|
|
23
cli/fmt.rs
23
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()?);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Add table
Reference in a new issue