diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 7857be8a811d42..e3f442c2deba18 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -212,6 +212,7 @@ pub struct EvalFlags { pub struct FmtFlags { pub check: bool, pub files: FileFlags, + pub permit_no_files: bool, pub use_tabs: Option, pub line_width: Option, pub indent_width: Option, @@ -304,6 +305,7 @@ pub struct LintFlags { pub maybe_rules_tags: Option>, pub maybe_rules_include: Option>, pub maybe_rules_exclude: Option>, + pub permit_no_files: bool, pub json: bool, pub compact: bool, pub watch: Option, @@ -1837,12 +1839,7 @@ If you specify a directory instead of a file, the path is expanded to all contai .help("Cache bench modules, but don't run benchmarks") .action(ArgAction::SetTrue), ) - .arg( - Arg::new("permit-no-files") - .long("permit-no-files") - .help("Don't return an error code if no bench files were found") - .action(ArgAction::SetTrue) - ) + .arg(permit_no_files_arg()) .arg(watch_arg(false)) .arg(watch_exclude_arg()) .arg(no_clear_screen_arg()) @@ -2382,6 +2379,7 @@ Ignore formatting a file by adding an ignore comment at the top of the file: .action(ArgAction::Append) .value_hint(ValueHint::AnyPath), ) + .arg(permit_no_files_arg()) .arg(watch_arg(false)) .arg(watch_exclude_arg()) .arg(no_clear_screen_arg()) @@ -2950,6 +2948,7 @@ To ignore linting on an entire file, you can add an ignore comment at the top of .action(ArgAction::Append) .value_hint(ValueHint::AnyPath), ) + .arg(permit_no_files_arg()) .arg(watch_arg(false)) .arg(watch_exclude_arg()) .arg(no_clear_screen_arg()) @@ -3198,13 +3197,7 @@ or **/__tests__/**: .value_name("N") .value_parser(value_parser!(NonZeroUsize)) .help_heading(TEST_HEADING)) - .arg( - Arg::new("permit-no-files") - .long("permit-no-files") - .help("Don't return an error code if no test files were found") - .action(ArgAction::SetTrue) - .help_heading(TEST_HEADING), - ) + .arg(permit_no_files_arg().help_heading(TEST_HEADING)) .arg( Arg::new("filter") .allow_hyphen_values(true) @@ -4191,6 +4184,13 @@ fn no_code_cache_arg() -> Arg { .action(ArgAction::SetTrue) } +fn permit_no_files_arg() -> Arg { + Arg::new("permit-no-files") + .long("permit-no-files") + .help("Don't return an error code if no files were found") + .action(ArgAction::SetTrue) +} + fn watch_exclude_arg() -> Arg { Arg::new("watch-exclude") .long("watch-exclude") @@ -4620,8 +4620,6 @@ fn bench_parse( flags.permissions.no_prompt = true; let json = matches.get_flag("json"); - let permit_no_files = matches.get_flag("permit-no-files"); - let ignore = match matches.remove_many::("ignore") { Some(f) => f .flat_map(flat_escape_split_commas) @@ -4650,7 +4648,7 @@ fn bench_parse( filter, json, no_run, - permit_no_files, + permit_no_files: permit_no_files_parse(matches), watch: watch_arg_parse(matches)?, }); @@ -4930,6 +4928,7 @@ fn fmt_parse( flags.subcommand = DenoSubcommand::Fmt(FmtFlags { check: matches.get_flag("check"), files: FileFlags { include, ignore }, + permit_no_files: permit_no_files_parse(matches), use_tabs, line_width, indent_width, @@ -5224,6 +5223,7 @@ fn lint_parse( maybe_rules_tags, maybe_rules_include, maybe_rules_exclude, + permit_no_files: permit_no_files_parse(matches), json, compact, watch: watch_arg_parse(matches)?, @@ -5455,8 +5455,6 @@ fn test_parse( let no_run = matches.get_flag("no-run"); let trace_leaks = matches.get_flag("trace-leaks"); let doc = matches.get_flag("doc"); - #[allow(clippy::print_stderr)] - let permit_no_files = matches.get_flag("permit-no-files"); let filter = matches.remove_one::("filter"); let clean = matches.get_flag("clean"); @@ -5522,7 +5520,7 @@ fn test_parse( files: FileFlags { include, ignore }, filter, shuffle, - permit_no_files, + permit_no_files: permit_no_files_parse(matches), concurrent_jobs, trace_leaks, watch: watch_arg_parse_with_paths(matches)?, @@ -6024,6 +6022,10 @@ fn reload_arg_validate(urlstr: String) -> Result { } } +fn permit_no_files_parse(matches: &mut ArgMatches) -> bool { + matches.get_flag("permit-no-files") +} + fn watch_arg_parse( matches: &mut ArgMatches, ) -> clap::error::Result> { @@ -6924,6 +6926,7 @@ mod tests { include: vec!["script_1.ts".to_string(), "script_2.ts".to_string()], ignore: vec![], }, + permit_no_files: false, use_tabs: None, line_width: None, indent_width: None, @@ -6938,7 +6941,8 @@ mod tests { } ); - let r = flags_from_vec(svec!["deno", "fmt", "--check"]); + let r = + flags_from_vec(svec!["deno", "fmt", "--permit-no-files", "--check"]); assert_eq!( r.unwrap(), Flags { @@ -6948,6 +6952,7 @@ mod tests { include: vec![], ignore: vec![], }, + permit_no_files: true, use_tabs: None, line_width: None, indent_width: None, @@ -6972,6 +6977,7 @@ mod tests { include: vec![], ignore: vec![], }, + permit_no_files: false, use_tabs: None, line_width: None, indent_width: None, @@ -6996,6 +7002,7 @@ mod tests { include: vec![], ignore: vec![], }, + permit_no_files: false, use_tabs: None, line_width: None, indent_width: None, @@ -7030,6 +7037,7 @@ mod tests { include: vec![], ignore: vec![], }, + permit_no_files: false, use_tabs: None, line_width: None, indent_width: None, @@ -7065,6 +7073,7 @@ mod tests { include: vec!["foo.ts".to_string()], ignore: vec!["bar.js".to_string()], }, + permit_no_files: false, use_tabs: None, line_width: None, indent_width: None, @@ -7089,6 +7098,7 @@ mod tests { include: vec![], ignore: vec![], }, + permit_no_files: false, use_tabs: None, line_width: None, indent_width: None, @@ -7121,6 +7131,7 @@ mod tests { include: vec!["foo.ts".to_string()], ignore: vec![], }, + permit_no_files: false, use_tabs: None, line_width: None, indent_width: None, @@ -7158,6 +7169,7 @@ mod tests { include: vec![], ignore: vec![], }, + permit_no_files: false, use_tabs: Some(true), line_width: Some(NonZeroU32::new(60).unwrap()), indent_width: Some(NonZeroU8::new(4).unwrap()), @@ -7189,6 +7201,7 @@ mod tests { include: vec![], ignore: vec![], }, + permit_no_files: false, use_tabs: Some(false), line_width: None, indent_width: None, @@ -7215,6 +7228,7 @@ mod tests { include: vec!["./**".to_string()], ignore: vec![], }, + permit_no_files: false, use_tabs: None, line_width: None, indent_width: None, @@ -7247,6 +7261,7 @@ mod tests { maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, + permit_no_files: false, json: false, compact: false, watch: Default::default(), @@ -7258,6 +7273,7 @@ mod tests { let r = flags_from_vec(svec![ "deno", "lint", + "--permit-no-files", "--allow-import", "--watch", "script_1.ts", @@ -7276,6 +7292,7 @@ mod tests { maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, + permit_no_files: true, json: false, compact: false, watch: Some(Default::default()), @@ -7309,6 +7326,7 @@ mod tests { maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, + permit_no_files: false, json: false, compact: false, watch: Some(WatchFlags { @@ -7340,6 +7358,7 @@ mod tests { maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, + permit_no_files: false, json: false, compact: false, watch: Default::default(), @@ -7362,6 +7381,7 @@ mod tests { maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, + permit_no_files: false, json: false, compact: false, watch: Default::default(), @@ -7389,6 +7409,7 @@ mod tests { maybe_rules_tags: Some(svec!["recommended"]), maybe_rules_include: None, maybe_rules_exclude: None, + permit_no_files: false, json: false, compact: false, watch: Default::default(), @@ -7417,6 +7438,7 @@ mod tests { maybe_rules_tags: Some(svec![""]), maybe_rules_include: Some(svec!["ban-untagged-todo", "no-undef"]), maybe_rules_exclude: Some(svec!["no-const-assign"]), + permit_no_files: false, json: false, compact: false, watch: Default::default(), @@ -7439,6 +7461,7 @@ mod tests { maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, + permit_no_files: false, json: true, compact: false, watch: Default::default(), @@ -7468,6 +7491,7 @@ mod tests { maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, + permit_no_files: false, json: true, compact: false, watch: Default::default(), @@ -7498,6 +7522,7 @@ mod tests { maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, + permit_no_files: false, json: false, compact: true, watch: Default::default(), diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 61dcad24a5d90a..e7fef17e7c5cee 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -181,7 +181,7 @@ fn resolve_paths_with_options_batches( }); } } - if paths_with_options_batches.is_empty() { + if paths_with_options_batches.is_empty() && !fmt_flags.permit_no_files { return Err(anyhow!("No target files found.")); } Ok(paths_with_options_batches) @@ -938,7 +938,7 @@ fn format_stdin( } fn files_str(len: usize) -> &'static str { - if len <= 1 { + if len == 1 { "file" } else { "files" diff --git a/cli/tools/lint/mod.rs b/cli/tools/lint/mod.rs index 13f0526f62a1e6..39ebfd1bf69952 100644 --- a/cli/tools/lint/mod.rs +++ b/cli/tools/lint/mod.rs @@ -233,7 +233,7 @@ fn resolve_paths_with_options_batches( }); } } - if paths_with_options_batches.is_empty() { + if paths_with_options_batches.is_empty() && !lint_flags.permit_no_files { return Err(anyhow!("No target files found.")); } Ok(paths_with_options_batches) diff --git a/tests/integration/fmt_tests.rs b/tests/integration/fmt_tests.rs index 18e7b3de9d0836..a5cdf22d802ca7 100644 --- a/tests/integration/fmt_tests.rs +++ b/tests/integration/fmt_tests.rs @@ -187,19 +187,6 @@ fn fmt_stdin_syntax_error() { output.assert_exit_code(1); } -#[test] -fn fmt_ignore_unexplicit_files() { - let context = TestContext::default(); - let output = context - .new_command() - .env("NO_COLOR", "1") - .args("fmt --check --ignore=./") - .run(); - - output.assert_exit_code(1); - assert_eq!(output.combined_output(), "error: No target files found.\n"); -} - #[test] fn fmt_auto_ignore_git_and_node_modules() { fn create_bad_json(t: PathRef) { diff --git a/tests/specs/fmt/ignore_unexplicit_files/__test__.jsonc b/tests/specs/fmt/ignore_unexplicit_files/__test__.jsonc new file mode 100644 index 00000000000000..f05d479154d7c0 --- /dev/null +++ b/tests/specs/fmt/ignore_unexplicit_files/__test__.jsonc @@ -0,0 +1,14 @@ +{ + "tests": { + "default": { + "args": "fmt --check --ignore=./", + "output": "error: No target files found.\n", + "exitCode": 1 + }, + "permitted": { + "args": "fmt --permit-no-files --check --ignore=./", + "output": "Checked 0 files\n", + "exitCode": 0 + } + } +} \ No newline at end of file diff --git a/tests/specs/fmt/ignore_unexplicit_files/main.ts b/tests/specs/fmt/ignore_unexplicit_files/main.ts new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/tests/specs/lint/ignore_unexplicit_files/__test__.jsonc b/tests/specs/lint/ignore_unexplicit_files/__test__.jsonc index 7653c4e2552f6b..eaaaa5e390096a 100644 --- a/tests/specs/lint/ignore_unexplicit_files/__test__.jsonc +++ b/tests/specs/lint/ignore_unexplicit_files/__test__.jsonc @@ -1,5 +1,14 @@ { - "args": "lint --ignore=./", - "output": "lint.out", - "exitCode": 1 + "tests": { + "default": { + "args": "lint --ignore=./", + "output": "error: No target files found.\n", + "exitCode": 1 + }, + "permitted": { + "args": "lint --permit-no-files --ignore=./", + "output": "Checked 0 files\n", + "exitCode": 0 + } + } } diff --git a/tests/specs/lint/ignore_unexplicit_files/lint.out b/tests/specs/lint/ignore_unexplicit_files/lint.out deleted file mode 100644 index 33f4ddd9331403..00000000000000 --- a/tests/specs/lint/ignore_unexplicit_files/lint.out +++ /dev/null @@ -1 +0,0 @@ -error: No target files found. diff --git a/tests/specs/lint/ignore_unexplicit_files/main.ts b/tests/specs/lint/ignore_unexplicit_files/main.ts new file mode 100644 index 00000000000000..e69de29bb2d1d6