feat(repl): add --eval flag for evaluating code when the repl starts (#11590)

This commit is contained in:
David Sherret 2021-08-06 17:30:28 -04:00 committed by GitHub
parent 33c8d790c3
commit 864ce6e832
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 133 additions and 15 deletions

View file

@ -91,7 +91,9 @@ pub enum DenoSubcommand {
rules: bool,
json: bool,
},
Repl,
Repl {
eval: Option<String>,
},
Run {
script: String,
},
@ -119,7 +121,7 @@ pub enum DenoSubcommand {
impl Default for DenoSubcommand {
fn default() -> DenoSubcommand {
DenoSubcommand::Repl
DenoSubcommand::Repl { eval: None }
}
}
@ -955,6 +957,13 @@ Ignore linting a file by adding an ignore comment at the top of the file:
fn repl_subcommand<'a, 'b>() -> App<'a, 'b> {
runtime_args(SubCommand::with_name("repl"), false, true)
.about("Read Eval Print Loop")
.arg(
Arg::with_name("eval")
.long("eval")
.help("Evaluates the provided code when the REPL starts.")
.takes_value(true)
.value_name("code"),
)
}
fn run_subcommand<'a, 'b>() -> App<'a, 'b> {
@ -1701,7 +1710,9 @@ fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
fn repl_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
runtime_args_parse(flags, matches, false, true);
flags.repl = true;
flags.subcommand = DenoSubcommand::Repl;
flags.subcommand = DenoSubcommand::Repl {
eval: matches.value_of("eval").map(ToOwned::to_owned),
};
flags.allow_net = Some(vec![]);
flags.allow_env = Some(vec![]);
flags.allow_run = Some(vec![]);
@ -2706,7 +2717,7 @@ mod tests {
r.unwrap(),
Flags {
repl: true,
subcommand: DenoSubcommand::Repl,
subcommand: DenoSubcommand::Repl { eval: None },
allow_net: Some(vec![]),
allow_env: Some(vec![]),
allow_run: Some(vec![]),
@ -2727,7 +2738,7 @@ mod tests {
r.unwrap(),
Flags {
repl: true,
subcommand: DenoSubcommand::Repl,
subcommand: DenoSubcommand::Repl { eval: None },
import_map_path: Some("import_map.json".to_string()),
no_remote: true,
config_path: Some("tsconfig.json".to_string()),
@ -2753,6 +2764,29 @@ mod tests {
);
}
#[test]
fn repl_with_eval_flag() {
#[rustfmt::skip]
let r = flags_from_vec(svec!["deno", "repl", "--eval", "console.log('hello');"]);
assert_eq!(
r.unwrap(),
Flags {
repl: true,
subcommand: DenoSubcommand::Repl {
eval: Some("console.log('hello');".to_string()),
},
allow_net: Some(vec![]),
allow_env: Some(vec![]),
allow_run: Some(vec![]),
allow_read: Some(vec![]),
allow_write: Some(vec![]),
allow_plugin: true,
allow_hrtime: true,
..Flags::default()
}
);
}
#[test]
fn allow_read_allowlist() {
use tempfile::TempDir;