1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

feat(fmt): make semi-colon option a boolean (#17527)

This commit is contained in:
David Sherret 2023-01-25 15:06:00 -05:00 committed by GitHub
parent 83642976bf
commit b5b4887c4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 58 deletions

View file

@ -373,13 +373,6 @@ pub enum ProseWrap {
Preserve, 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)] #[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")] #[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct FmtOptionsConfig { pub struct FmtOptionsConfig {
@ -388,7 +381,7 @@ pub struct FmtOptionsConfig {
pub indent_width: Option<u8>, pub indent_width: Option<u8>,
pub single_quote: Option<bool>, pub single_quote: Option<bool>,
pub prose_wrap: Option<ProseWrap>, pub prose_wrap: Option<ProseWrap>,
pub semi_colons: Option<SemiColons>, pub semi_colons: Option<bool>,
} }
#[derive(Clone, Debug, Default, Deserialize)] #[derive(Clone, Debug, Default, Deserialize)]

View file

@ -131,7 +131,7 @@ pub struct FmtFlags {
pub indent_width: Option<NonZeroU8>, pub indent_width: Option<NonZeroU8>,
pub single_quote: Option<bool>, pub single_quote: Option<bool>,
pub prose_wrap: Option<String>, pub prose_wrap: Option<String>,
pub semi_colons: Option<String>, pub no_semicolons: Option<bool>,
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[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(
Arg::new("options-use-tabs") Arg::new("options-use-tabs")
.long("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."), .help("Use tabs instead of spaces for indentation. Defaults to false."),
) )
.arg( .arg(
@ -1207,6 +1212,11 @@ Ignore formatting a file by adding an ignore comment at the top of the file:
.arg( .arg(
Arg::new("options-single-quote") Arg::new("options-single-quote")
.long("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."), .help("Use single quotes. Defaults to false."),
) )
.arg( .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."), .help("Define how prose should be wrapped. Defaults to always."),
) )
.arg( .arg(
Arg::new("options-semi") Arg::new("options-no-semicolons")
.long("options-semi") .long("options-no-semicolons")
.min_values(0)
.max_values(1)
.takes_value(true) .takes_value(true)
.possible_values(["prefer", "asi"]) .require_equals(true)
.help("Use semi colons. Defaults to prefer."), .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 ext = matches.value_of("ext").unwrap().to_string();
let use_tabs = if matches.is_present("options-use-tabs") { let use_tabs = optional_bool_parse(matches, "options-use-tabs");
Some(true)
} else {
None
};
let line_width = if matches.is_present("options-line-width") { let line_width = if matches.is_present("options-line-width") {
Some( Some(
matches matches
@ -2566,22 +2575,18 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
Some( Some(
matches matches
.value_of("options-indent-width") .value_of("options-indent-width")
.unwrap() .unwrap_or("true")
.parse() .parse()
.unwrap(), .unwrap(),
) )
} else { } else {
None None
}; };
let single_quote = if matches.is_present("options-single-quote") { let single_quote = optional_bool_parse(matches, "options-single-quote");
Some(true)
} else {
None
};
let prose_wrap = matches let prose_wrap = matches
.value_of("options-prose-wrap") .value_of("options-prose-wrap")
.map(ToString::to_string); .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 { flags.subcommand = DenoSubcommand::Fmt(FmtFlags {
check: matches.is_present("check"), check: matches.is_present("check"),
@ -2592,10 +2597,18 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
indent_width, indent_width,
single_quote, single_quote,
prose_wrap, prose_wrap,
semi_colons, no_semicolons,
}); });
} }
fn optional_bool_parse(matches: &ArgMatches, name: &str) -> Option<bool> {
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) { fn init_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
flags.subcommand = DenoSubcommand::Init(InitFlags { flags.subcommand = DenoSubcommand::Init(InitFlags {
dir: matches.value_of("dir").map(|f| f.to_string()), dir: matches.value_of("dir").map(|f| f.to_string()),
@ -3610,7 +3623,7 @@ mod tests {
indent_width: None, indent_width: None,
single_quote: None, single_quote: None,
prose_wrap: None, prose_wrap: None,
semi_colons: None, no_semicolons: None,
}), }),
..Flags::default() ..Flags::default()
} }
@ -3632,7 +3645,7 @@ mod tests {
indent_width: None, indent_width: None,
single_quote: None, single_quote: None,
prose_wrap: None, prose_wrap: None,
semi_colons: None, no_semicolons: None,
}), }),
..Flags::default() ..Flags::default()
} }
@ -3654,7 +3667,7 @@ mod tests {
indent_width: None, indent_width: None,
single_quote: None, single_quote: None,
prose_wrap: None, prose_wrap: None,
semi_colons: None, no_semicolons: None,
}), }),
..Flags::default() ..Flags::default()
} }
@ -3676,7 +3689,7 @@ mod tests {
indent_width: None, indent_width: None,
single_quote: None, single_quote: None,
prose_wrap: None, prose_wrap: None,
semi_colons: None, no_semicolons: None,
}), }),
watch: Some(vec![]), watch: Some(vec![]),
..Flags::default() ..Flags::default()
@ -3700,7 +3713,7 @@ mod tests {
indent_width: None, indent_width: None,
single_quote: None, single_quote: None,
prose_wrap: None, prose_wrap: None,
semi_colons: None, no_semicolons: None,
}), }),
watch: Some(vec![]), watch: Some(vec![]),
no_clear_screen: true, no_clear_screen: true,
@ -3731,7 +3744,7 @@ mod tests {
indent_width: None, indent_width: None,
single_quote: None, single_quote: None,
prose_wrap: None, prose_wrap: None,
semi_colons: None, no_semicolons: None,
}), }),
watch: Some(vec![]), watch: Some(vec![]),
..Flags::default() ..Flags::default()
@ -3754,7 +3767,7 @@ mod tests {
indent_width: None, indent_width: None,
single_quote: None, single_quote: None,
prose_wrap: None, prose_wrap: None,
semi_colons: None, no_semicolons: None,
}), }),
config_flag: ConfigFlag::Path("deno.jsonc".to_string()), config_flag: ConfigFlag::Path("deno.jsonc".to_string()),
..Flags::default() ..Flags::default()
@ -3784,7 +3797,7 @@ mod tests {
indent_width: None, indent_width: None,
single_quote: None, single_quote: None,
prose_wrap: None, prose_wrap: None,
semi_colons: None, no_semicolons: None,
}), }),
config_flag: ConfigFlag::Path("deno.jsonc".to_string()), config_flag: ConfigFlag::Path("deno.jsonc".to_string()),
watch: Some(vec![]), watch: Some(vec![]),
@ -3803,8 +3816,7 @@ mod tests {
"--options-single-quote", "--options-single-quote",
"--options-prose-wrap", "--options-prose-wrap",
"never", "never",
"--options-semi", "--options-no-semicolons",
"asi"
]); ]);
assert_eq!( assert_eq!(
r.unwrap(), r.unwrap(),
@ -3821,7 +3833,36 @@ mod tests {
indent_width: Some(NonZeroU8::new(4).unwrap()), indent_width: Some(NonZeroU8::new(4).unwrap()),
single_quote: Some(true), single_quote: Some(true),
prose_wrap: Some("never".to_string()), 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() ..Flags::default()
} }

View file

@ -15,7 +15,6 @@ pub use config_file::FmtOptionsConfig;
pub use config_file::JsxImportSourceConfig; pub use config_file::JsxImportSourceConfig;
pub use config_file::LintRulesConfig; pub use config_file::LintRulesConfig;
pub use config_file::ProseWrap; pub use config_file::ProseWrap;
pub use config_file::SemiColons;
pub use config_file::TsConfig; pub use config_file::TsConfig;
pub use config_file::TsConfigForEmit; pub use config_file::TsConfigForEmit;
pub use config_file::TsConfigType; pub use config_file::TsConfigType;
@ -201,13 +200,8 @@ fn resolve_fmt_options(
}); });
} }
if let Some(semi_colons) = &fmt_flags.semi_colons { if let Some(no_semis) = &fmt_flags.no_semicolons {
options.semi_colons = Some(match semi_colons.as_str() { options.semi_colons = Some(!no_semis);
"prefer" => SemiColons::Prefer,
"asi" => SemiColons::Asi,
// validators in `flags.rs` makes other values unreachable
_ => unreachable!(),
});
} }
} }

View file

@ -324,11 +324,8 @@
}, },
"semiColons": { "semiColons": {
"description": "Whether to prefer using semicolons.", "description": "Whether to prefer using semicolons.",
"default": "prefer", "type": "boolean",
"enum": [ "default": true
"prefer",
"asi"
]
} }
} }
} }

View file

@ -14,7 +14,7 @@
"indentWidth": 8, "indentWidth": 8,
"singleQuote": true, "singleQuote": true,
"proseWrap": "always", "proseWrap": "always",
"semiColons": "asi" "semiColons": false
} }
} }
} }

View file

@ -12,7 +12,6 @@ use crate::args::FilesConfig;
use crate::args::FmtOptions; use crate::args::FmtOptions;
use crate::args::FmtOptionsConfig; use crate::args::FmtOptionsConfig;
use crate::args::ProseWrap; use crate::args::ProseWrap;
use crate::args::SemiColons;
use crate::colors; use crate::colors;
use crate::util::diff::diff; use crate::util::diff::diff;
use crate::util::file_watcher; use crate::util::file_watcher;
@ -514,12 +513,8 @@ fn get_resolved_typescript_config(
if let Some(semi_colons) = options.semi_colons { if let Some(semi_colons) = options.semi_colons {
builder.semi_colons(match semi_colons { builder.semi_colons(match semi_colons {
SemiColons::Prefer => { true => dprint_plugin_typescript::configuration::SemiColons::Prefer,
dprint_plugin_typescript::configuration::SemiColons::Prefer false => dprint_plugin_typescript::configuration::SemiColons::Asi,
}
SemiColons::Asi => {
dprint_plugin_typescript::configuration::SemiColons::Asi
}
}); });
} }