Skip to content

Commit

Permalink
feat(json): add internal support for json, jsonc and json5 (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Apr 2, 2024
1 parent 11b1323 commit bcd92ab
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 34 deletions.
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ precommit:
just lint
just test
just update-readme
cargo run -- init
npx prettier --write mdsf.json
typos .

publish:
Expand Down
6 changes: 2 additions & 4 deletions src/formatters/biome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ pub fn format_using_biome(
mod test_biome {
use crate::{
formatters::{biome::format_using_biome, setup_snippet},
languages::Language,
languages::{JsonFlavor, Language},
};

#[test]
fn it_should_format_json() {
let input = "
{
// comments are allowed
\"key\": \"value\",
\"key2\": [
\"value2\",
Expand All @@ -36,13 +35,12 @@ mod test_biome {
";

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

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

let output = format_using_biome(snippet.path())
Expand Down
33 changes: 20 additions & 13 deletions src/formatters/clang_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn format_using_clang_format(
mod test_clang_format {
use crate::{
formatters::{clang_format::format_using_clang_format, setup_snippet},
languages::Language,
languages::{JsonFlavor, Language},
};

#[test_with::executable(clang-format)]
Expand All @@ -39,7 +39,7 @@ mod test_clang_format {
.1
.expect("it to be some");

assert_eq!(expected_output, output);
assert_eq!(output, expected_output);
}

#[test_with::executable(clang-format)]
Expand All @@ -63,7 +63,7 @@ mod test_clang_format {
.1
.expect("it to be some");

assert_eq!(expected_output, output);
assert_eq!(output, expected_output);
}

#[test_with::executable(clang-format)]
Expand All @@ -89,7 +89,7 @@ mod test_clang_format {
.1
.expect("it to be some");

assert_eq!(expected_output, output);
assert_eq!(output, expected_output);
}

#[test_with::executable(clang-format)]
Expand All @@ -110,29 +110,36 @@ mod test_clang_format {
.1
.expect("it to be some");

assert_eq!(expected_output, output);
assert_eq!(output, expected_output);
}

#[test_with::executable(clang-format)]
#[test]
fn it_should_format_json() {
let input = " {
\"key\": \"value\",
\"key2\": [ \"value2\", \"value3\", 1 , null]
\"key2\": [\"value2\", \"value3\", 1 , null]
} ";

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

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

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

assert_eq!(expected_output, output);
assert_eq!(output, expected_output);
}

#[test_with::executable(clang-format)]
Expand All @@ -156,7 +163,7 @@ mod test_clang_format {
.1
.expect("it to be some");

assert_eq!(expected_output, output);
assert_eq!(output, expected_output);
}

#[test_with::executable(clang-format)]
Expand All @@ -177,7 +184,7 @@ mod test_clang_format {
.1
.expect("it to be some");

assert_eq!(expected_output, output);
assert_eq!(output, expected_output);
}

#[test_with::executable(clang-format)]
Expand Down Expand Up @@ -205,6 +212,6 @@ mod test_clang_format {
.1
.expect("it to be some");

assert_eq!(expected_output, output);
assert_eq!(output, expected_output);
}
}
7 changes: 5 additions & 2 deletions src/formatters/deno_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ pub fn format_using_deno_fmt(

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

use super::format_using_deno_fmt;

Expand Down Expand Up @@ -44,7 +47,7 @@ mod test_deno_fmt {
}
";

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

let output = format_using_deno_fmt(snippet.path())
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
Language::Html => config.html.format(snippet_path),
Language::Java => config.java.format(snippet_path),
Language::JavaScript => config.javascript.format(snippet_path),
Language::Json => config.json.format(snippet_path),
Language::Json(_flavor) => config.json.format(snippet_path),
Language::Just => config.just.format(snippet_path),
Language::Kotlin => config.kotlin.format(snippet_path),
Language::Lua => config.lua.format(snippet_path),
Expand Down
6 changes: 3 additions & 3 deletions src/formatters/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn format_using_prettier(
mod test_prettier {
use crate::{
formatters::{prettier::format_using_prettier, setup_snippet},
languages::Language,
languages::{JsonFlavor, Language},
};

#[test]
Expand All @@ -84,11 +84,11 @@ mod test_prettier {
let expected_output = "{
// comments are allowed
\"key\": \"value\",
\"key2\": [\"value2\", \"value3\", 1, null],
\"key2\": [\"value2\", \"value3\", 1, null]
}
";

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

let output = format_using_prettier(snippet.path(), true)
Expand Down
21 changes: 14 additions & 7 deletions src/languages/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ impl LanguageFormatter for Json {
mod test_json {
use crate::{
formatters::{setup_snippet, MdsfFormatter},
languages::Lang,
languages::{JsonFlavor, Lang},
};

use super::Json;

const INPUT: &str = "
{
const INPUT: &str = "{
\"key\": \"value\",
\"key2\": [
\"value2\",
Expand All @@ -78,7 +77,7 @@ mod test_json {
}
";

const EXTENSION: &str = crate::languages::Language::Json.to_file_ext();
const EXTENSION: &str = crate::languages::Language::Json(JsonFlavor::Json).to_file_ext();

#[test]
fn it_should_be_enabled_by_default() {
Expand Down Expand Up @@ -116,7 +115,7 @@ mod test_json {

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

Expand Down Expand Up @@ -163,8 +162,16 @@ mod test_json {
.expect("it to not fail")
.expect("it to be a snippet");

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

assert_eq!(output, expected_output);
}
Expand Down
16 changes: 13 additions & 3 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use schemars::JsonSchema;

use crate::formatters::MdsfFormatter;

pub enum JsonFlavor {
Json,
JsonC,
Json5,
}

pub enum Language {
Blade,
C,
Expand All @@ -23,7 +29,7 @@ pub enum Language {
Html,
Java,
JavaScript,
Json,
Json(JsonFlavor),
Just,
Kotlin,
Lua,
Expand Down Expand Up @@ -135,7 +141,9 @@ impl Language {
"html" | "html5" => Some(Self::Html),
"java" => Some(Self::Java),
"javascript" | "js" | "jsx" => Some(Self::JavaScript),
"json" | "jsonc" => Some(Self::Json),
"json" => Some(Self::Json(JsonFlavor::Json)),
"jsonc" => Some(Self::Json(JsonFlavor::JsonC)),
"json5" => Some(Self::Json(JsonFlavor::Json5)),
"just" | "justfile" => Some(Self::Just),
"kotlin" => Some(Self::Kotlin),
"lua" => Some(Self::Lua),
Expand Down Expand Up @@ -190,7 +198,9 @@ impl Language {
Self::Html => ".html",
Self::Java => ".java",
Self::JavaScript => ".js",
Self::Json => ".jsonc",
Self::Json(JsonFlavor::Json) => ".json",
Self::Json(JsonFlavor::JsonC) => ".jsonc",
Self::Json(JsonFlavor::Json5) => ".json5",
Self::Just => ".justfile",
Self::Kotlin => ".kt",
Self::Lua => ".lua",
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ This snippets is from 'json.md':
```json
// This is a comment
{
"add": { "a": 1, "b": ["1", 23, null] },
"add": { "a": 1, "b": ["1", 23, null] }
}
```
Expand Down

0 comments on commit bcd92ab

Please sign in to comment.