Skip to content

Commit

Permalink
feat: add support for deno fmt (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 12, 2024
1 parent a832e5d commit e5ada18
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 49 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ jobs:
- uses: crystal-lang/install-crystal@v1
# roc_format
- uses: hasnep/[email protected]
# deno_fmt
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- run: rustup toolchain install stable --profile minimal
- run: rustup component add rustfmt clippy
Expand Down
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,32 @@ mdsf init
>
> Only formatters that are already installed will be used.
| Language | Formatters |
| ----------- | ------------------------------------------- |
| C | `clang-format` |
| CSS | `prettier` |
| Cpp | `clang-format` |
| Crystal | `crystal_format` |
| Dart | `dart_format` |
| Elixir | `mix_format` |
| Gleam | `gleam_format` |
| Go | `gofmt`, `gofumpt` |
| HTML | `prettier` |
| JSON | `prettier`, `biome`, `clang-format` |
| Java | `clang-format` |
| JavaScript | `prettier`, `biome`, `clang-format` |
| Lua | `stylua` |
| Nim | `nimpretty` |
| Objective C | `clang-format` |
| Protobuf | `clang-format` |
| Python | `ruff`, `black`, `blue`, `yapf`, `autopep8` |
| Roc | `roc_format` |
| Ruby | `rubocop` |
| Rust | `rustfmt` |
| SQL | `sqlfluff`, `sql-formatter` |
| Shell | `shfmt` |
| TOML | `taplo` |
| TypeScript | `prettier`, `biome` |
| Vue | `prettier` |
| YAML | `prettier` |
| Zig | `zigfmt` |
| Language | Formatters |
| ----------- | ----------------------------------------------- |
| C | `clang-format` |
| CSS | `prettier` |
| Cpp | `clang-format` |
| Crystal | `crystal_format` |
| Dart | `dart_format` |
| Elixir | `mix_format` |
| Gleam | `gleam_format` |
| Go | `gofmt`, `gofumpt` |
| HTML | `prettier` |
| JSON | `prettier`, `biome`, `clang-format`, `deno_fmt` |
| Java | `clang-format` |
| JavaScript | `prettier`, `biome`, `clang-format`, `deno_fmt` |
| Lua | `stylua` |
| Nim | `nimpretty` |
| Objective C | `clang-format` |
| Protobuf | `clang-format` |
| Python | `ruff`, `black`, `blue`, `yapf`, `autopep8` |
| Roc | `roc_format` |
| Ruby | `rubocop` |
| Rust | `rustfmt` |
| SQL | `sqlfluff`, `sql-formatter` |
| Shell | `shfmt` |
| TOML | `taplo` |
| TypeScript | `prettier`, `biome`, `deno_fmt` |
| Vue | `prettier` |
| YAML | `prettier` |
| Zig | `zigfmt` |
6 changes: 3 additions & 3 deletions schemas/v0.0.1/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@
},
"JavaScriptFormatter": {
"type": "string",
"enum": ["prettier", "biome", "clang-format"]
"enum": ["prettier", "biome", "deno_fmt", "clang-format"]
},
"Json": {
"type": "object",
Expand All @@ -595,7 +595,7 @@
},
"JsonFormatter": {
"type": "string",
"enum": ["prettier", "biome", "clang-format"]
"enum": ["prettier", "biome", "deno_fmt", "clang-format"]
},
"Lua": {
"type": "object",
Expand Down Expand Up @@ -868,7 +868,7 @@
},
"TypeScriptFormatter": {
"type": "string",
"enum": ["prettier", "biome"]
"enum": ["prettier", "biome", "deno_fmt"]
},
"Vue": {
"type": "object",
Expand Down
109 changes: 109 additions & 0 deletions src/formatters/deno_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use super::execute_command;

#[inline]
pub fn format_using_deno_fmt(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
// NOTE: the biome docs recommend running biome using npx, and not directly
let mut cmd = std::process::Command::new("deno");

cmd.arg("fmt").arg("--quiet").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
mod test_deno_fmt {
use crate::{formatters::setup_snippet, languages::Language};

use super::format_using_deno_fmt;

#[test]
fn it_should_format_json() {
let input = "
{
// comments are allowed
\"key\": \"value\",
\"key2\": [
\"value2\",
\"value3\",
1
, null]
}
";

let expected_output = "{
// comments are allowed
\"key\": \"value\",
\"key2\": [
\"value2\",
\"value3\",
1,
null
]
}
";

let snippet = setup_snippet(input, Language::Json.to_file_ext())
.expect("it to create a snippet file");

let output = format_using_deno_fmt(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}

#[test]
fn it_should_format_javascript() {
let input = "
async function asyncAddition(a,b){
return a+b
}
";

let expected_output = "async function asyncAddition(a, b) {
return a + b;
}
";

let snippet = setup_snippet(input, Language::JavaScript.to_file_ext())
.expect("it to create a snippet file");

let output = format_using_deno_fmt(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}

#[test]
fn it_should_format_typescript() {
let input = "
async function asyncAddition( a: \tnumber,b:number ) :Promise< number>
{
return a+b
}
";

let expected_output =
"async function asyncAddition(a: number, b: number): Promise<number> {
return a + b;
}
";

let snippet = setup_snippet(input, Language::TypeScript.to_file_ext())
.expect("it to create a snippet file");

let output = format_using_deno_fmt(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod blue;
pub mod clang_format;
pub mod crystal_format;
pub mod dart_format;
pub mod deno_format;
pub mod gleam_format;
pub mod gofmt;
pub mod gofumpt;
Expand Down
42 changes: 34 additions & 8 deletions src/languages/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
config::default_enabled,
formatters::{
biome::format_using_biome, clang_format::format_using_clang_format,
prettier::format_using_prettier,
deno_format::format_using_deno_fmt, prettier::format_using_prettier,
},
};

Expand All @@ -18,6 +18,8 @@ pub enum JavaScriptFormatter {
Prettier,
#[serde(rename = "biome")]
Biome,
#[serde(rename = "deno_fmt")]
DenoFmt,
#[serde(rename = "clang-format")]
ClangFormat,
}
Expand Down Expand Up @@ -49,14 +51,12 @@ impl LanguageFormatter for JavaScript {
}

match self.formatter {
JavaScriptFormatter::Biome => format_using_biome(snippet_path).map(|res| res.1),
JavaScriptFormatter::Prettier => {
format_using_prettier(snippet_path, true).map(|res| res.1)
}
JavaScriptFormatter::ClangFormat => {
format_using_clang_format(snippet_path).map(|res| res.1)
}
JavaScriptFormatter::Biome => format_using_biome(snippet_path),
JavaScriptFormatter::Prettier => format_using_prettier(snippet_path, true),
JavaScriptFormatter::ClangFormat => format_using_clang_format(snippet_path),
JavaScriptFormatter::DenoFmt => format_using_deno_fmt(snippet_path),
}
.map(|res| res.1)
}
}

Expand Down Expand Up @@ -179,4 +179,30 @@ mod test_javascript {

assert_eq!(expected_output, output);
}

#[test]
fn test_deno_fmt() {
let input = " async function asyncAddition( a,b) {
a * b;
return a+b
} ";

let expected_output =
"async function asyncAddition(a, b) {\n a * b;\n return a + b;\n}\n";

let l = JavaScript {
enabled: true,
formatter: JavaScriptFormatter::DenoFmt,
};

let snippet = setup_snippet(input, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

let output = l
.format(snippet_path)
.expect("it to not fail")
.expect("it to be a snippet");

assert_eq!(expected_output, output);
}
}
41 changes: 37 additions & 4 deletions src/languages/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
config::default_enabled,
formatters::{
biome::format_using_biome, clang_format::format_using_clang_format,
prettier::format_using_prettier,
deno_format::format_using_deno_fmt, prettier::format_using_prettier,
},
};

Expand All @@ -18,6 +18,8 @@ pub enum JsonFormatter {
Prettier,
#[serde(rename = "biome")]
Biome,
#[serde(rename = "deno_fmt")]
DenoFmt,
#[serde(rename = "clang-format")]
ClangFormat,
}
Expand Down Expand Up @@ -49,10 +51,12 @@ impl LanguageFormatter for Json {
}

match self.formatter {
JsonFormatter::Biome => format_using_biome(snippet_path).map(|res| res.1),
JsonFormatter::Prettier => format_using_prettier(snippet_path, true).map(|res| res.1),
JsonFormatter::ClangFormat => format_using_clang_format(snippet_path).map(|res| res.1),
JsonFormatter::Biome => format_using_biome(snippet_path),
JsonFormatter::Prettier => format_using_prettier(snippet_path, true),
JsonFormatter::ClangFormat => format_using_clang_format(snippet_path),
JsonFormatter::DenoFmt => format_using_deno_fmt(snippet_path),
}
.map(|res| res.1)
}
}

Expand Down Expand Up @@ -162,4 +166,33 @@ mod test_json {

assert_eq!(output, expected_output);
}

#[test]
fn test_deno_fmt() {
let l = Json {
enabled: true,
formatter: JsonFormatter::DenoFmt,
};

let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

let output = l
.format(snippet_path)
.expect("it to not fail")
.expect("it to be a snippet");

let expected_output = "{
\"key\": \"value\",
\"key2\": [
\"value2\",
\"value3\",
1,
null
]
}
";

assert_eq!(output, expected_output);
}
}
Loading

0 comments on commit e5ada18

Please sign in to comment.