Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(javascript): add support for standardjs #163

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mdsf init
| Haskell | `fourmolu`, `hindent`, `ormolu` |
| Html | `prettier` |
| Java | `clang-format`, `google-java-format` |
| JavaScript | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| JavaScript | `biome`, `clang-format`, `deno_fmt`, `prettier`, `standardjs` |
| Json | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| Just | `just_fmt` |
| Kotlin | `ktfmt`, `ktlint` |
Expand Down
2 changes: 1 addition & 1 deletion schemas/v0.0.3/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@
},
"JavaScript": {
"type": "string",
"enum": ["prettier", "biome", "deno_fmt", "clang-format"]
"enum": ["prettier", "biome", "deno_fmt", "clang-format", "standardjs"]
},
"JavaScriptRuntime": {
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub mod scalafmt;
pub mod shfmt;
pub mod sql_formatter;
pub mod sqlfluff;
pub mod standardjs;
pub mod standardrb;
pub mod stylua;
pub mod swiftformat;
Expand Down
54 changes: 54 additions & 0 deletions src/formatters/standardjs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::{runners::setup_npm_script, terminal::print_formatter_info};

use super::execute_command;

#[inline]
pub fn format_using_standardjs(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_formatter_info("standardjs");

let mut cmd = setup_npm_script("standard");

cmd.arg("--fix").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

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

console.info(asyncAddition(1, 2));
";

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

console.info(asyncAddition(1, 2))
";

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

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

assert_eq!(output, expected_output);
}
}
41 changes: 40 additions & 1 deletion src/languages/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use schemars::JsonSchema;

use crate::formatters::{
biome::format_using_biome, clang_format::format_using_clang_format,
deno_fmt::format_using_deno_fmt, prettier::format_using_prettier, MdsfFormatter,
deno_fmt::format_using_deno_fmt, prettier::format_using_prettier,
standardjs::format_using_standardjs, MdsfFormatter,
};

use super::{Lang, LanguageFormatter};
Expand All @@ -19,6 +20,8 @@ pub enum JavaScript {
DenoFmt,
#[serde(rename = "clang-format")]
ClangFormat,
#[serde(rename = "standardjs")]
Standardjs,
}

impl Default for Lang<JavaScript> {
Expand Down Expand Up @@ -54,6 +57,7 @@ impl LanguageFormatter for JavaScript {
Self::Prettier => format_using_prettier(snippet_path, true),
Self::ClangFormat => format_using_clang_format(snippet_path),
Self::DenoFmt => format_using_deno_fmt(snippet_path),
Self::Standardjs => format_using_standardjs(snippet_path),
}
}
}
Expand Down Expand Up @@ -208,4 +212,39 @@ mod test_javascript {

assert_eq!(expected_output, output);
}

#[test_with::executable(npx)]
#[test]
fn test_standardjs() {
let input = "
async function asyncAddition(a,b )
{
return a+b
}

console.info(asyncAddition(1, 2));
";

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

console.info(asyncAddition(1, 2))
";

let l = Lang::<JavaScript> {
enabled: true,
formatter: MdsfFormatter::Single(JavaScript::Standardjs),
};

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);
}
}
39 changes: 37 additions & 2 deletions tests/go.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
With package name
With package name

```go

Expand All @@ -13,13 +13,36 @@ With package name

```

Without package name

```go




func add(a int , b int ) int {
return a + b
}


```

```go


// package mdsf

func add(a int , b int ) int {
return a + b
}

Without package name

```

```go


//package mdsf


func add(a int , b int ) int {
Expand All @@ -29,3 +52,15 @@ Without package name

```

```go


// package mdsf


func add(a int , b int ) int {
return a + b
}


```
Loading