Skip to content

feat(fmt/lint): add --permit-no-files #28753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(fmt/lint): add --permit-no-files
  • Loading branch information
dsherret committed Apr 4, 2025
commit b39b3a8fca1ae1e213cd3be7a8f33024e3672172
65 changes: 45 additions & 20 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,
pub line_width: Option<NonZeroU32>,
pub indent_width: Option<NonZeroU8>,
Expand Down Expand Up @@ -304,6 +305,7 @@ pub struct LintFlags {
pub maybe_rules_tags: Option<Vec<String>>,
pub maybe_rules_include: Option<Vec<String>>,
pub maybe_rules_exclude: Option<Vec<String>>,
pub permit_no_files: bool,
pub json: bool,
pub compact: bool,
pub watch: Option<WatchFlags>,
Expand Down Expand Up @@ -1836,12 +1838,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())
Expand Down Expand Up @@ -2381,6 +2378,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())
Expand Down Expand Up @@ -2949,6 +2947,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())
Expand Down Expand Up @@ -3197,13 +3196,7 @@ or <c>**/__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)
Expand Down Expand Up @@ -4190,6 +4183,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")
Expand Down Expand Up @@ -4609,8 +4609,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::<String>("ignore") {
Some(f) => f
.flat_map(flat_escape_split_commas)
Expand Down Expand Up @@ -4639,7 +4637,7 @@ fn bench_parse(
filter,
json,
no_run,
permit_no_files,
permit_no_files: permit_no_files_parse(matches),
watch: watch_arg_parse(matches)?,
});

Expand Down Expand Up @@ -4919,6 +4917,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,
Expand Down Expand Up @@ -5213,6 +5212,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)?,
Expand Down Expand Up @@ -5444,8 +5444,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::<String>("filter");
let clean = matches.get_flag("clean");

Expand Down Expand Up @@ -5511,7 +5509,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)?,
Expand Down Expand Up @@ -6013,6 +6011,10 @@ fn reload_arg_validate(urlstr: String) -> Result<String, clap::Error> {
}
}

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<Option<WatchFlags>> {
Expand Down Expand Up @@ -6912,6 +6914,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,
Expand All @@ -6926,7 +6929,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 {
Expand All @@ -6936,6 +6940,7 @@ mod tests {
include: vec![],
ignore: vec![],
},
permit_no_files: false,
use_tabs: None,
line_width: None,
indent_width: None,
Expand All @@ -6960,6 +6965,7 @@ mod tests {
include: vec![],
ignore: vec![],
},
permit_no_files: false,
use_tabs: None,
line_width: None,
indent_width: None,
Expand All @@ -6984,6 +6990,7 @@ mod tests {
include: vec![],
ignore: vec![],
},
permit_no_files: false,
use_tabs: None,
line_width: None,
indent_width: None,
Expand Down Expand Up @@ -7018,6 +7025,7 @@ mod tests {
include: vec![],
ignore: vec![],
},
permit_no_files: false,
use_tabs: None,
line_width: None,
indent_width: None,
Expand Down Expand Up @@ -7053,6 +7061,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,
Expand All @@ -7077,6 +7086,7 @@ mod tests {
include: vec![],
ignore: vec![],
},
permit_no_files: false,
use_tabs: None,
line_width: None,
indent_width: None,
Expand Down Expand Up @@ -7109,6 +7119,7 @@ mod tests {
include: vec!["foo.ts".to_string()],
ignore: vec![],
},
permit_no_files: false,
use_tabs: None,
line_width: None,
indent_width: None,
Expand Down Expand Up @@ -7146,6 +7157,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()),
Expand Down Expand Up @@ -7177,6 +7189,7 @@ mod tests {
include: vec![],
ignore: vec![],
},
permit_no_files: false,
use_tabs: Some(false),
line_width: None,
indent_width: None,
Expand All @@ -7203,6 +7216,7 @@ mod tests {
include: vec!["./**".to_string()],
ignore: vec![],
},
permit_no_files: false,
use_tabs: None,
line_width: None,
indent_width: None,
Expand Down Expand Up @@ -7235,6 +7249,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(),
Expand All @@ -7246,6 +7261,7 @@ mod tests {
let r = flags_from_vec(svec![
"deno",
"lint",
"--permit-no-files",
"--allow-import",
"--watch",
"script_1.ts",
Expand All @@ -7264,6 +7280,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()),
Expand Down Expand Up @@ -7297,6 +7314,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 {
Expand Down Expand Up @@ -7328,6 +7346,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(),
Expand All @@ -7350,6 +7369,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(),
Expand Down Expand Up @@ -7377,6 +7397,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(),
Expand Down Expand Up @@ -7405,6 +7426,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(),
Expand All @@ -7427,6 +7449,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(),
Expand Down Expand Up @@ -7456,6 +7479,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(),
Expand Down Expand Up @@ -7486,6 +7510,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(),
Expand Down
4 changes: 2 additions & 2 deletions cli/tools/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -938,7 +938,7 @@ fn format_stdin(
}

fn files_str(len: usize) -> &'static str {
if len <= 1 {
if len == 1 {
"file"
} else {
"files"
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 0 additions & 13 deletions tests/integration/fmt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions tests/specs/fmt/ignore_unexplicit_files/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Empty file.
Loading
Loading