diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs index 106a15ddf0..c31f099601 100644 --- a/cli/args/config_file.rs +++ b/cli/args/config_file.rs @@ -373,13 +373,6 @@ pub enum ProseWrap { Preserve, } -#[derive(Clone, Copy, Debug, Serialize, Deserialize)] -#[serde(deny_unknown_fields, rename_all = "camelCase")] -pub enum SemiColons { - Prefer, - Asi, -} - #[derive(Clone, Debug, Default, Serialize, Deserialize)] #[serde(default, deny_unknown_fields, rename_all = "camelCase")] pub struct FmtOptionsConfig { @@ -388,7 +381,7 @@ pub struct FmtOptionsConfig { pub indent_width: Option, pub single_quote: Option, pub prose_wrap: Option, - pub semi_colons: Option, + pub semi_colons: Option, } #[derive(Clone, Debug, Default, Deserialize)] diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 9f7b9206f2..00df45274d 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -131,7 +131,7 @@ pub struct FmtFlags { pub indent_width: Option, pub single_quote: Option, pub prose_wrap: Option, - pub semi_colons: Option, + pub no_semicolons: Option, } #[derive(Clone, Debug, Eq, PartialEq)] @@ -1178,6 +1178,11 @@ Ignore formatting a file by adding an ignore comment at the top of the file: .arg( Arg::new("options-use-tabs") .long("options-use-tabs") + .takes_value(true) + .min_values(0) + .max_values(1) + .require_equals(true) + .possible_values(["true", "false"]) .help("Use tabs instead of spaces for indentation. Defaults to false."), ) .arg( @@ -1207,6 +1212,11 @@ Ignore formatting a file by adding an ignore comment at the top of the file: .arg( Arg::new("options-single-quote") .long("options-single-quote") + .min_values(0) + .max_values(1) + .takes_value(true) + .require_equals(true) + .possible_values(["true", "false"]) .help("Use single quotes. Defaults to false."), ) .arg( @@ -1217,11 +1227,14 @@ Ignore formatting a file by adding an ignore comment at the top of the file: .help("Define how prose should be wrapped. Defaults to always."), ) .arg( - Arg::new("options-semi") - .long("options-semi") + Arg::new("options-no-semicolons") + .long("options-no-semicolons") + .min_values(0) + .max_values(1) .takes_value(true) - .possible_values(["prefer", "asi"]) - .help("Use semi colons. Defaults to prefer."), + .require_equals(true) + .possible_values(["true", "false"]) + .help("Don't use semicolons except where necessary."), ) } @@ -2546,11 +2559,7 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { }; let ext = matches.value_of("ext").unwrap().to_string(); - let use_tabs = if matches.is_present("options-use-tabs") { - Some(true) - } else { - None - }; + let use_tabs = optional_bool_parse(matches, "options-use-tabs"); let line_width = if matches.is_present("options-line-width") { Some( matches @@ -2566,22 +2575,18 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { Some( matches .value_of("options-indent-width") - .unwrap() + .unwrap_or("true") .parse() .unwrap(), ) } else { None }; - let single_quote = if matches.is_present("options-single-quote") { - Some(true) - } else { - None - }; + let single_quote = optional_bool_parse(matches, "options-single-quote"); let prose_wrap = matches .value_of("options-prose-wrap") .map(ToString::to_string); - let semi_colons = matches.value_of("options-semi").map(ToString::to_string); + let no_semicolons = optional_bool_parse(matches, "options-no-semicolons"); flags.subcommand = DenoSubcommand::Fmt(FmtFlags { check: matches.is_present("check"), @@ -2592,10 +2597,18 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { indent_width, single_quote, prose_wrap, - semi_colons, + no_semicolons, }); } +fn optional_bool_parse(matches: &ArgMatches, name: &str) -> Option { + if matches.is_present(name) { + Some(matches.value_of(name).unwrap_or("true").parse().unwrap()) + } else { + None + } +} + fn init_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.subcommand = DenoSubcommand::Init(InitFlags { dir: matches.value_of("dir").map(|f| f.to_string()), @@ -3610,7 +3623,7 @@ mod tests { indent_width: None, single_quote: None, prose_wrap: None, - semi_colons: None, + no_semicolons: None, }), ..Flags::default() } @@ -3632,7 +3645,7 @@ mod tests { indent_width: None, single_quote: None, prose_wrap: None, - semi_colons: None, + no_semicolons: None, }), ..Flags::default() } @@ -3654,7 +3667,7 @@ mod tests { indent_width: None, single_quote: None, prose_wrap: None, - semi_colons: None, + no_semicolons: None, }), ..Flags::default() } @@ -3676,7 +3689,7 @@ mod tests { indent_width: None, single_quote: None, prose_wrap: None, - semi_colons: None, + no_semicolons: None, }), watch: Some(vec![]), ..Flags::default() @@ -3700,7 +3713,7 @@ mod tests { indent_width: None, single_quote: None, prose_wrap: None, - semi_colons: None, + no_semicolons: None, }), watch: Some(vec![]), no_clear_screen: true, @@ -3731,7 +3744,7 @@ mod tests { indent_width: None, single_quote: None, prose_wrap: None, - semi_colons: None, + no_semicolons: None, }), watch: Some(vec![]), ..Flags::default() @@ -3754,7 +3767,7 @@ mod tests { indent_width: None, single_quote: None, prose_wrap: None, - semi_colons: None, + no_semicolons: None, }), config_flag: ConfigFlag::Path("deno.jsonc".to_string()), ..Flags::default() @@ -3784,7 +3797,7 @@ mod tests { indent_width: None, single_quote: None, prose_wrap: None, - semi_colons: None, + no_semicolons: None, }), config_flag: ConfigFlag::Path("deno.jsonc".to_string()), watch: Some(vec![]), @@ -3803,8 +3816,7 @@ mod tests { "--options-single-quote", "--options-prose-wrap", "never", - "--options-semi", - "asi" + "--options-no-semicolons", ]); assert_eq!( r.unwrap(), @@ -3821,7 +3833,36 @@ mod tests { indent_width: Some(NonZeroU8::new(4).unwrap()), single_quote: Some(true), prose_wrap: Some("never".to_string()), - semi_colons: Some("asi".to_string()), + no_semicolons: Some(true), + }), + ..Flags::default() + } + ); + + // try providing =false to the booleans + let r = flags_from_vec(svec![ + "deno", + "fmt", + "--options-use-tabs=false", + "--options-single-quote=false", + "--options-no-semicolons=false", + ]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Fmt(FmtFlags { + check: false, + ext: "ts".to_string(), + files: FileFlags { + include: vec![], + ignore: vec![], + }, + use_tabs: Some(false), + line_width: None, + indent_width: None, + single_quote: Some(false), + prose_wrap: None, + no_semicolons: Some(false), }), ..Flags::default() } diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 6d81ae2af7..4cb960238a 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -15,7 +15,6 @@ pub use config_file::FmtOptionsConfig; pub use config_file::JsxImportSourceConfig; pub use config_file::LintRulesConfig; pub use config_file::ProseWrap; -pub use config_file::SemiColons; pub use config_file::TsConfig; pub use config_file::TsConfigForEmit; pub use config_file::TsConfigType; @@ -201,13 +200,8 @@ fn resolve_fmt_options( }); } - if let Some(semi_colons) = &fmt_flags.semi_colons { - options.semi_colons = Some(match semi_colons.as_str() { - "prefer" => SemiColons::Prefer, - "asi" => SemiColons::Asi, - // validators in `flags.rs` makes other values unreachable - _ => unreachable!(), - }); + if let Some(no_semis) = &fmt_flags.no_semicolons { + options.semi_colons = Some(!no_semis); } } diff --git a/cli/schemas/config-file.v1.json b/cli/schemas/config-file.v1.json index 96b1f202ba..675a763a4d 100644 --- a/cli/schemas/config-file.v1.json +++ b/cli/schemas/config-file.v1.json @@ -324,11 +324,8 @@ }, "semiColons": { "description": "Whether to prefer using semicolons.", - "default": "prefer", - "enum": [ - "prefer", - "asi" - ] + "type": "boolean", + "default": true } } } diff --git a/cli/tests/testdata/fmt/with_config/deno.jsonc b/cli/tests/testdata/fmt/with_config/deno.jsonc index 8901d62224..44e3f9a997 100644 --- a/cli/tests/testdata/fmt/with_config/deno.jsonc +++ b/cli/tests/testdata/fmt/with_config/deno.jsonc @@ -14,7 +14,7 @@ "indentWidth": 8, "singleQuote": true, "proseWrap": "always", - "semiColons": "asi" + "semiColons": false } } } diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index ae09239036..441d4fe080 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -12,7 +12,6 @@ use crate::args::FilesConfig; use crate::args::FmtOptions; use crate::args::FmtOptionsConfig; use crate::args::ProseWrap; -use crate::args::SemiColons; use crate::colors; use crate::util::diff::diff; use crate::util::file_watcher; @@ -514,12 +513,8 @@ fn get_resolved_typescript_config( if let Some(semi_colons) = options.semi_colons { builder.semi_colons(match semi_colons { - SemiColons::Prefer => { - dprint_plugin_typescript::configuration::SemiColons::Prefer - } - SemiColons::Asi => { - dprint_plugin_typescript::configuration::SemiColons::Asi - } + true => dprint_plugin_typescript::configuration::SemiColons::Prefer, + false => dprint_plugin_typescript::configuration::SemiColons::Asi, }); }