0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

feat: add --ext flag to deno eval (#9295)

This PR deprecates the "--ts"/"-T" flag of "deno eval" (which will later be removed in 2.0)
and introduces "--ext" which is used by "deno fmt" for content type selection. 
This is to ensure we have a single flag that can be used across subcommands 
to select the language (JS/TS).
This commit is contained in:
Satya Rohith 2021-02-21 22:28:32 +05:30 committed by GitHub
parent fe1b512820
commit 06fcfc5c0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 22 deletions

View file

@ -44,7 +44,7 @@ pub enum DenoSubcommand {
Eval { Eval {
print: bool, print: bool,
code: String, code: String,
as_typescript: bool, ext: String,
}, },
Fmt { Fmt {
check: bool, check: bool,
@ -525,7 +525,14 @@ fn eval_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
flags.allow_write = Some(vec![]); flags.allow_write = Some(vec![]);
flags.allow_plugin = true; flags.allow_plugin = true;
flags.allow_hrtime = true; flags.allow_hrtime = true;
// TODO(@satyarohith): remove this flag in 2.0.
let as_typescript = matches.is_present("ts"); let as_typescript = matches.is_present("ts");
let ext = if as_typescript {
"ts".to_string()
} else {
matches.value_of("ext").unwrap().to_string()
};
let print = matches.is_present("print"); let print = matches.is_present("print");
let mut code: Vec<String> = matches let mut code: Vec<String> = matches
.values_of("code_arg") .values_of("code_arg")
@ -538,11 +545,7 @@ fn eval_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
for v in code_args { for v in code_args {
flags.argv.push(v); flags.argv.push(v);
} }
flags.subcommand = DenoSubcommand::Eval { flags.subcommand = DenoSubcommand::Eval { print, code, ext };
print,
code,
as_typescript,
};
} }
fn info_parse(flags: &mut Flags, matches: &clap::ArgMatches) { fn info_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
@ -999,17 +1002,27 @@ fn eval_subcommand<'a, 'b>() -> App<'a, 'b> {
deno eval \"console.log('hello world')\" deno eval \"console.log('hello world')\"
To evaluate as TypeScript: To evaluate as TypeScript:
deno eval -T \"const v: string = 'hello'; console.log(v)\" deno eval --ext=ts \"const v: string = 'hello'; console.log(v)\"
This command has implicit access to all permissions (--allow-all).", This command has implicit access to all permissions (--allow-all).",
) )
.arg( .arg(
// TODO(@satyarohith): remove this argument in 2.0.
Arg::with_name("ts") Arg::with_name("ts")
.long("ts") .long("ts")
.short("T") .short("T")
.help("Treat eval input as TypeScript") .help("Treat eval input as TypeScript")
.takes_value(false) .takes_value(false)
.multiple(false), .multiple(false)
.hidden(true),
)
.arg(
Arg::with_name("ext")
.long("ext")
.help("Set standard input (stdin) content type")
.takes_value(true)
.default_value("js")
.possible_values(&["ts", "tsx", "js", "jsx"]),
) )
.arg( .arg(
Arg::with_name("print") Arg::with_name("print")
@ -2296,7 +2309,7 @@ mod tests {
subcommand: DenoSubcommand::Eval { subcommand: DenoSubcommand::Eval {
print: false, print: false,
code: "'console.log(\"hello\")'".to_string(), code: "'console.log(\"hello\")'".to_string(),
as_typescript: false, ext: "js".to_string(),
}, },
allow_net: Some(vec![]), allow_net: Some(vec![]),
allow_env: true, allow_env: true,
@ -2319,7 +2332,7 @@ mod tests {
subcommand: DenoSubcommand::Eval { subcommand: DenoSubcommand::Eval {
print: true, print: true,
code: "1+2".to_string(), code: "1+2".to_string(),
as_typescript: false, ext: "js".to_string(),
}, },
allow_net: Some(vec![]), allow_net: Some(vec![]),
allow_env: true, allow_env: true,
@ -2343,7 +2356,7 @@ mod tests {
subcommand: DenoSubcommand::Eval { subcommand: DenoSubcommand::Eval {
print: false, print: false,
code: "'console.log(\"hello\")'".to_string(), code: "'console.log(\"hello\")'".to_string(),
as_typescript: true, ext: "ts".to_string(),
}, },
allow_net: Some(vec![]), allow_net: Some(vec![]),
allow_env: true, allow_env: true,
@ -2367,7 +2380,7 @@ mod tests {
subcommand: DenoSubcommand::Eval { subcommand: DenoSubcommand::Eval {
print: false, print: false,
code: "42".to_string(), code: "42".to_string(),
as_typescript: false, ext: "js".to_string(),
}, },
unstable: true, unstable: true,
import_map_path: Some("import_map.json".to_string()), import_map_path: Some("import_map.json".to_string()),
@ -2410,7 +2423,7 @@ mod tests {
subcommand: DenoSubcommand::Eval { subcommand: DenoSubcommand::Eval {
print: false, print: false,
code: "console.log(Deno.args)".to_string(), code: "console.log(Deno.args)".to_string(),
as_typescript: false, ext: "js".to_string(),
}, },
argv: svec!["arg1", "arg2"], argv: svec!["arg1", "arg2"],
allow_net: Some(vec![]), allow_net: Some(vec![]),

View file

@ -472,7 +472,7 @@ async fn cache_command(
async fn eval_command( async fn eval_command(
flags: Flags, flags: Flags,
code: String, code: String,
as_typescript: bool, ext: String,
print: bool, print: bool,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
// Force TypeScript compile. // Force TypeScript compile.
@ -492,10 +492,14 @@ async fn eval_command(
let file = File { let file = File {
local: main_module.clone().to_file_path().unwrap(), local: main_module.clone().to_file_path().unwrap(),
maybe_types: None, maybe_types: None,
media_type: if as_typescript { media_type: if ext.as_str() == "ts" {
MediaType::TypeScript MediaType::TypeScript
} else { } else if ext.as_str() == "tsx" {
MediaType::TSX
} else if ext.as_str() == "js" {
MediaType::JavaScript MediaType::JavaScript
} else {
MediaType::JSX
}, },
source: String::from_utf8(source_code)?, source: String::from_utf8(source_code)?,
specifier: main_module.clone(), specifier: main_module.clone(),
@ -1154,11 +1158,9 @@ fn get_subcommand(
filter, filter,
private, private,
} => doc_command(flags, source_file, json, filter, private).boxed_local(), } => doc_command(flags, source_file, json, filter, private).boxed_local(),
DenoSubcommand::Eval { DenoSubcommand::Eval { print, code, ext } => {
print, eval_command(flags, code, ext, print).boxed_local()
code, }
as_typescript,
} => eval_command(flags, code, as_typescript, print).boxed_local(),
DenoSubcommand::Cache { files } => { DenoSubcommand::Cache { files } => {
cache_command(flags, files).boxed_local() cache_command(flags, files).boxed_local()
} }

View file

@ -2514,7 +2514,7 @@ console.log("finish");
// Ugly parentheses due to whitespace delimiting problem. // Ugly parentheses due to whitespace delimiting problem.
itest!(_030_eval_ts { itest!(_030_eval_ts {
args: "eval --quiet -T console.log((123)as(number))", // 'as' is a TS keyword only args: "eval --quiet --ext=ts console.log((123)as(number))", // 'as' is a TS keyword only
output: "030_eval_ts.out", output: "030_eval_ts.out",
}); });