Skip to content

Instantly share code, notes, and snippets.

@AnInternetTroll
Created June 10, 2022 19:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnInternetTroll/81ed97db853dc3628effed4157782509 to your computer and use it in GitHub Desktop.
Save AnInternetTroll/81ed97db853dc3628effed4157782509 to your computer and use it in GitHub Desktop.

fmt-semi-colons

This patch adds the ability to control how semi colons are palced by deno fmt.

Install

The general idea is to clone deno and patch with this file

git clone https://github.com/denoland/deno.git
cd deno
git am /path/to/fmt-semi-colons

Usage

deno fmt --options-semi-colons:

--options-semi-colons <options-semi-colons>
  Define whether statements should end in a semi-colon. Defaults to
  always.

  [possible values: always, prefer, asi]

In a config file:

{
  "fmt": {
    "options": {
      "semiColons": "always"
    }
  }
}

Options

Always

Always uses semi-colons where applicable.

Prefer

Prefers to use semi-colons, but doesn’t add one in certain scenarios such as for the last member of a single-line type literal.

Asi

Uses automatic semi-colon insertion. Only adds a semi-colon at the start of some expression statements when necessary.

From 64e2969522786260197dd09f516024ac4e56bfa2 Mon Sep 17 00:00:00 2001
From: AnInternetTroll <lucafulger@gmail.com>
Date: Wed, 8 Jun 2022 16:47:47 +0200
Subject: [PATCH] Add --options-semi-colons flag and semiColons config option
---
cli/config_file.rs | 9 +++++++++
cli/flags.rs | 28 +++++++++++++++++++++++++++-
cli/tools/fmt.rs | 24 ++++++++++++++++++++++++
3 files changed, 60 insertions(+), 1 deletion(-)
diff --git a/cli/config_file.rs b/cli/config_file.rs
index 3644bb7c1..119fceba3 100644
--- a/cli/config_file.rs
+++ b/cli/config_file.rs
@@ -494,6 +494,14 @@ pub enum ProseWrap {
Preserve,
}
+#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
+#[serde(deny_unknown_fields, rename_all = "camelCase")]
+pub enum SemiColons {
+ Always,
+ Prefer,
+ Asi,
+}
+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct FmtOptionsConfig {
@@ -502,6 +510,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)]
diff --git a/cli/flags.rs b/cli/flags.rs
index dbb716df8..2212c69a7 100644
--- a/cli/flags.rs
+++ b/cli/flags.rs
@@ -116,6 +116,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, PartialEq, Deserialize, Serialize)]
@@ -1160,6 +1161,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-colons")
+ .long("options-semi-colons")
+ .takes_value(true)
+ .possible_values(&["always", "prefer", "asi"])
+ .help("Define whether statements should end in a semi-colon. Defaults to always."),
+ )
}
fn info_subcommand<'a>() -> Command<'a> {
@@ -2414,6 +2422,12 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
None
};
+ let semi_colons = if matches.is_present("options-semi-colons") {
+ Some(matches.value_of("options-semi-colons").unwrap().to_string())
+ } else {
+ None
+ };
+
flags.subcommand = DenoSubcommand::Fmt(FmtFlags {
check: matches.is_present("check"),
ext,
@@ -2424,6 +2438,7 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
indent_width,
single_quote,
prose_wrap,
+ semi_colons,
});
}
@@ -3353,6 +3368,7 @@ mod tests {
indent_width: None,
single_quote: None,
prose_wrap: None,
+ semi_colons: None,
}),
..Flags::default()
}
@@ -3372,6 +3388,7 @@ mod tests {
indent_width: None,
single_quote: None,
prose_wrap: None,
+ semi_colons: None,
}),
..Flags::default()
}
@@ -3391,6 +3408,7 @@ mod tests {
indent_width: None,
single_quote: None,
prose_wrap: None,
+ semi_colons: None,
}),
..Flags::default()
}
@@ -3410,6 +3428,7 @@ mod tests {
indent_width: None,
single_quote: None,
prose_wrap: None,
+ semi_colons: None,
}),
watch: Some(vec![]),
..Flags::default()
@@ -3431,6 +3450,7 @@ mod tests {
indent_width: None,
single_quote: None,
prose_wrap: None,
+ semi_colons: None,
}),
watch: Some(vec![]),
no_clear_screen: true,
@@ -3459,6 +3479,7 @@ mod tests {
indent_width: None,
single_quote: None,
prose_wrap: None,
+ semi_colons: None,
}),
watch: Some(vec![]),
..Flags::default()
@@ -3479,6 +3500,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()
@@ -3506,6 +3528,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![]),
@@ -3523,7 +3546,9 @@ mod tests {
"4",
"--options-single-quote",
"--options-prose-wrap",
- "never"
+ "never",
+ "--options-semi-colons",
+ "asi",
]);
assert_eq!(
r.unwrap(),
@@ -3538,6 +3563,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()
}
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index 475767331..c92d88e9f 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -11,6 +11,7 @@ use crate::colors;
use crate::config_file::FmtConfig;
use crate::config_file::FmtOptionsConfig;
use crate::config_file::ProseWrap;
+use crate::config_file::SemiColons;
use crate::deno_dir::DenoDir;
use crate::diff::diff;
use crate::file_watcher;
@@ -535,6 +536,15 @@ fn resolve_fmt_options(
});
}
+ if let Some(semi_colons) = &fmt_flags.semi_colons {
+ options.semi_colons = Some(match semi_colons.as_str() {
+ "always" => SemiColons::Always,
+ "prefer" => SemiColons::Prefer,
+ "asi" => SemiColons::Asi,
+ _ => unreachable!(),
+ });
+ }
+
options
}
@@ -565,6 +575,20 @@ fn get_resolved_typescript_config(
}
}
+ if let Some(semi_colons) = options.semi_colons {
+ builder.semi_colons(match semi_colons {
+ SemiColons::Always => {
+ dprint_plugin_typescript::configuration::SemiColons::Always
+ }
+ SemiColons::Prefer => {
+ dprint_plugin_typescript::configuration::SemiColons::Prefer
+ }
+ SemiColons::Asi => {
+ dprint_plugin_typescript::configuration::SemiColons::Asi
+ }
+ });
+ }
+
builder.build()
}
--
2.36.1
@kigiri
Copy link

kigiri commented Jun 24, 2022

this would make me use deno fmt :( nice patch though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment