diff --git a/cli/flags.rs b/cli/flags.rs index c58e3e3d4c..0ae8de27d7 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -44,7 +44,7 @@ pub enum DenoSubcommand { Eval { print: bool, code: String, - as_typescript: bool, + ext: String, }, Fmt { check: bool, @@ -525,7 +525,14 @@ fn eval_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.allow_write = Some(vec![]); flags.allow_plugin = true; flags.allow_hrtime = true; + // TODO(@satyarohith): remove this flag in 2.0. 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 mut code: Vec = matches .values_of("code_arg") @@ -538,11 +545,7 @@ fn eval_parse(flags: &mut Flags, matches: &clap::ArgMatches) { for v in code_args { flags.argv.push(v); } - flags.subcommand = DenoSubcommand::Eval { - print, - code, - as_typescript, - }; + flags.subcommand = DenoSubcommand::Eval { print, code, ext }; } 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')\" 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).", ) .arg( + // TODO(@satyarohith): remove this argument in 2.0. Arg::with_name("ts") .long("ts") .short("T") .help("Treat eval input as TypeScript") .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::with_name("print") @@ -2296,7 +2309,7 @@ mod tests { subcommand: DenoSubcommand::Eval { print: false, code: "'console.log(\"hello\")'".to_string(), - as_typescript: false, + ext: "js".to_string(), }, allow_net: Some(vec![]), allow_env: true, @@ -2319,7 +2332,7 @@ mod tests { subcommand: DenoSubcommand::Eval { print: true, code: "1+2".to_string(), - as_typescript: false, + ext: "js".to_string(), }, allow_net: Some(vec![]), allow_env: true, @@ -2343,7 +2356,7 @@ mod tests { subcommand: DenoSubcommand::Eval { print: false, code: "'console.log(\"hello\")'".to_string(), - as_typescript: true, + ext: "ts".to_string(), }, allow_net: Some(vec![]), allow_env: true, @@ -2367,7 +2380,7 @@ mod tests { subcommand: DenoSubcommand::Eval { print: false, code: "42".to_string(), - as_typescript: false, + ext: "js".to_string(), }, unstable: true, import_map_path: Some("import_map.json".to_string()), @@ -2410,7 +2423,7 @@ mod tests { subcommand: DenoSubcommand::Eval { print: false, code: "console.log(Deno.args)".to_string(), - as_typescript: false, + ext: "js".to_string(), }, argv: svec!["arg1", "arg2"], allow_net: Some(vec![]), diff --git a/cli/main.rs b/cli/main.rs index 779c45d53d..b9516e7040 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -472,7 +472,7 @@ async fn cache_command( async fn eval_command( flags: Flags, code: String, - as_typescript: bool, + ext: String, print: bool, ) -> Result<(), AnyError> { // Force TypeScript compile. @@ -492,10 +492,14 @@ async fn eval_command( let file = File { local: main_module.clone().to_file_path().unwrap(), maybe_types: None, - media_type: if as_typescript { + media_type: if ext.as_str() == "ts" { MediaType::TypeScript - } else { + } else if ext.as_str() == "tsx" { + MediaType::TSX + } else if ext.as_str() == "js" { MediaType::JavaScript + } else { + MediaType::JSX }, source: String::from_utf8(source_code)?, specifier: main_module.clone(), @@ -1154,11 +1158,9 @@ fn get_subcommand( filter, private, } => doc_command(flags, source_file, json, filter, private).boxed_local(), - DenoSubcommand::Eval { - print, - code, - as_typescript, - } => eval_command(flags, code, as_typescript, print).boxed_local(), + DenoSubcommand::Eval { print, code, ext } => { + eval_command(flags, code, ext, print).boxed_local() + } DenoSubcommand::Cache { files } => { cache_command(flags, files).boxed_local() } diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index bb65f24fa3..b82efa9046 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2514,7 +2514,7 @@ console.log("finish"); // Ugly parentheses due to whitespace delimiting problem. 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", });