0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -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 {
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<String> = 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![]),

View file

@ -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()
}

View file

@ -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",
});