mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
feat(fmt): add ability to configure semicolons (#17292)
Allows to change behavior of `deno fmt` to use "ASI" setting for semicolons instead of always prefering them, this is done by "--options-semi=asi" flag or `"semi": "asi"` setting in the config file.
This commit is contained in:
parent
abd9610530
commit
e1c51f3c0d
8 changed files with 72 additions and 12 deletions
|
@ -373,6 +373,13 @@ 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 {
|
||||
|
@ -381,6 +388,7 @@ pub struct FmtOptionsConfig {
|
|||
pub indent_width: Option<u8>,
|
||||
pub single_quote: Option<bool>,
|
||||
pub prose_wrap: Option<ProseWrap>,
|
||||
pub semi_colons: Option<SemiColons>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize)]
|
||||
|
|
|
@ -131,6 +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>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
|
@ -1214,6 +1215,13 @@ Ignore formatting a file by adding an ignore comment at the top of the file:
|
|||
.possible_values(["always", "never", "preserve"])
|
||||
.help("Define how prose should be wrapped. Defaults to always."),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("options-semi")
|
||||
.long("options-semi")
|
||||
.takes_value(true)
|
||||
.possible_values(["prefer", "asi"])
|
||||
.help("Use semi colons. Defaults to prefer."),
|
||||
)
|
||||
}
|
||||
|
||||
fn init_subcommand<'a>() -> Command<'a> {
|
||||
|
@ -2571,6 +2579,7 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
|||
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);
|
||||
|
||||
flags.subcommand = DenoSubcommand::Fmt(FmtFlags {
|
||||
check: matches.is_present("check"),
|
||||
|
@ -2581,6 +2590,7 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
|||
indent_width,
|
||||
single_quote,
|
||||
prose_wrap,
|
||||
semi_colons,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -3598,6 +3608,7 @@ mod tests {
|
|||
indent_width: None,
|
||||
single_quote: None,
|
||||
prose_wrap: None,
|
||||
semi_colons: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -3619,6 +3630,7 @@ mod tests {
|
|||
indent_width: None,
|
||||
single_quote: None,
|
||||
prose_wrap: None,
|
||||
semi_colons: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -3640,6 +3652,7 @@ mod tests {
|
|||
indent_width: None,
|
||||
single_quote: None,
|
||||
prose_wrap: None,
|
||||
semi_colons: None,
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
@ -3661,6 +3674,7 @@ mod tests {
|
|||
indent_width: None,
|
||||
single_quote: None,
|
||||
prose_wrap: None,
|
||||
semi_colons: None,
|
||||
}),
|
||||
watch: Some(vec![]),
|
||||
..Flags::default()
|
||||
|
@ -3684,6 +3698,7 @@ mod tests {
|
|||
indent_width: None,
|
||||
single_quote: None,
|
||||
prose_wrap: None,
|
||||
semi_colons: None,
|
||||
}),
|
||||
watch: Some(vec![]),
|
||||
no_clear_screen: true,
|
||||
|
@ -3714,6 +3729,7 @@ mod tests {
|
|||
indent_width: None,
|
||||
single_quote: None,
|
||||
prose_wrap: None,
|
||||
semi_colons: None,
|
||||
}),
|
||||
watch: Some(vec![]),
|
||||
..Flags::default()
|
||||
|
@ -3736,6 +3752,7 @@ mod tests {
|
|||
indent_width: None,
|
||||
single_quote: None,
|
||||
prose_wrap: None,
|
||||
semi_colons: None,
|
||||
}),
|
||||
config_flag: ConfigFlag::Path("deno.jsonc".to_string()),
|
||||
..Flags::default()
|
||||
|
@ -3765,6 +3782,7 @@ mod tests {
|
|||
indent_width: None,
|
||||
single_quote: None,
|
||||
prose_wrap: None,
|
||||
semi_colons: None,
|
||||
}),
|
||||
config_flag: ConfigFlag::Path("deno.jsonc".to_string()),
|
||||
watch: Some(vec![]),
|
||||
|
@ -3782,7 +3800,9 @@ mod tests {
|
|||
"4",
|
||||
"--options-single-quote",
|
||||
"--options-prose-wrap",
|
||||
"never"
|
||||
"never",
|
||||
"--options-semi",
|
||||
"asi"
|
||||
]);
|
||||
assert_eq!(
|
||||
r.unwrap(),
|
||||
|
@ -3799,6 +3819,7 @@ 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()),
|
||||
}),
|
||||
..Flags::default()
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ 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;
|
||||
|
@ -199,6 +200,15 @@ fn resolve_fmt_options(
|
|||
_ => unreachable!(),
|
||||
});
|
||||
}
|
||||
|
||||
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!(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
options
|
||||
|
|
|
@ -321,6 +321,14 @@
|
|||
"never",
|
||||
"preserve"
|
||||
]
|
||||
},
|
||||
"semiColons": {
|
||||
"description": "Whether to prefer using semicolons.",
|
||||
"default": "prefer",
|
||||
"enum": [
|
||||
"prefer",
|
||||
"asi"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,8 @@
|
|||
"lineWidth": 40,
|
||||
"indentWidth": 8,
|
||||
"singleQuote": true,
|
||||
"proseWrap": "always"
|
||||
"proseWrap": "always",
|
||||
"semiColons": "asi"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
18
cli/tests/testdata/fmt/with_config/subdir/a.ts
vendored
18
cli/tests/testdata/fmt/with_config/subdir/a.ts
vendored
|
@ -4,33 +4,33 @@ Deno.test(
|
|||
const response =
|
||||
await fetch(
|
||||
'http://localhost:4545/assets/fixture.json',
|
||||
);
|
||||
)
|
||||
const response1 =
|
||||
response.clone();
|
||||
response.clone()
|
||||
assert(
|
||||
response !==
|
||||
response1,
|
||||
);
|
||||
)
|
||||
assertEquals(
|
||||
response.status,
|
||||
response1
|
||||
.status,
|
||||
);
|
||||
)
|
||||
assertEquals(
|
||||
response.statusText,
|
||||
response1
|
||||
.statusText,
|
||||
);
|
||||
)
|
||||
const u8a =
|
||||
new Uint8Array(
|
||||
await response
|
||||
.arrayBuffer(),
|
||||
);
|
||||
)
|
||||
const u8a1 =
|
||||
new Uint8Array(
|
||||
await response1
|
||||
.arrayBuffer(),
|
||||
);
|
||||
)
|
||||
for (
|
||||
let i = 0;
|
||||
i <
|
||||
|
@ -40,7 +40,7 @@ Deno.test(
|
|||
assertEquals(
|
||||
u8a[i],
|
||||
u8a1[i],
|
||||
);
|
||||
)
|
||||
}
|
||||
},
|
||||
);
|
||||
)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
DEBUG RS - deno::args::config_file:529 - Config file found at '[WILDCARD]deno.jsonc'
|
||||
DEBUG RS - [WILDCARD] - Config file found at '[WILDCARD]deno.jsonc'
|
||||
[WILDCARD]
|
||||
ok
|
||||
|
|
|
@ -12,6 +12,7 @@ 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;
|
||||
|
@ -511,6 +512,17 @@ 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
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
builder.build()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue