1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -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,
}
#[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<u8>,
pub single_quote: Option<bool>,
pub prose_wrap: Option<ProseWrap>,
pub semi_colons: Option<SemiColons>,
pub semi_colons: Option<bool>,
}
#[derive(Clone, Debug, Default, Deserialize)]

View file

@ -131,7 +131,7 @@ pub struct FmtFlags {
pub indent_width: Option<NonZeroU8>,
pub single_quote: Option<bool>,
pub prose_wrap: Option<String>,
pub semi_colons: Option<String>,
pub no_semicolons: Option<bool>,
}
#[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<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) {
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()
}

View file

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

View file

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

View file

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

View file

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