feat(cli): add warning for incorrectly ordered flags (#16734)

This code checks if permission flags are incorrectly defined after the
module name (e.g. `deno run mod.ts --allow-read` instead of the correct
`deno run --allow-read mod.ts`). If so, a simple warning is displayed.
This commit is contained in:
Asher Gomez 2022-11-24 14:00:31 +11:00 committed by GitHub
parent beaa0d8867
commit fe7e3a12ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 0 deletions

View file

@ -493,6 +493,32 @@ impl Flags {
prompt: !self.no_prompt,
}
}
pub fn has_permission(&self) -> bool {
self.allow_all
|| self.allow_hrtime
|| self.allow_env.is_some()
|| self.allow_ffi.is_some()
|| self.allow_net.is_some()
|| self.allow_read.is_some()
|| self.allow_run.is_some()
|| self.allow_sys.is_some()
|| self.allow_write.is_some()
}
pub fn has_permission_in_argv(&self) -> bool {
self.argv.iter().any(|arg| {
arg == "--allow-all"
|| arg == "--allow-hrtime"
|| arg.starts_with("--allow-env")
|| arg.starts_with("--allow-ffi")
|| arg.starts_with("--allow-net")
|| arg.starts_with("--allow-read")
|| arg.starts_with("--allow-run")
|| arg.starts_with("--allow-sys")
|| arg.starts_with("--allow-write")
})
}
}
static ENV_VARIABLES_HELP: &str = r#"ENVIRONMENT VARIABLES:
@ -3388,6 +3414,24 @@ mod tests {
);
}
#[test]
fn has_permission() {
let r = flags_from_vec(svec!["deno", "run", "--allow-read", "x.ts"]);
assert_eq!(r.unwrap().has_permission(), true);
let r = flags_from_vec(svec!["deno", "run", "x.ts"]);
assert_eq!(r.unwrap().has_permission(), false);
}
#[test]
fn has_permission_in_argv() {
let r = flags_from_vec(svec!["deno", "run", "x.ts", "--allow-read"]);
assert_eq!(r.unwrap().has_permission_in_argv(), true);
let r = flags_from_vec(svec!["deno", "run", "x.ts"]);
assert_eq!(r.unwrap().has_permission_in_argv(), false);
}
#[test]
fn script_args() {
let r = flags_from_vec(svec![