From bcd92abae1da7f6cea5f6ff6faa37c609f910594 Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Tue, 2 Apr 2024 14:24:24 +0200 Subject: [PATCH] feat(json): add internal support for json, jsonc and json5 (#148) --- justfile | 2 ++ src/formatters/biome.rs | 6 ++---- src/formatters/clang_format.rs | 33 ++++++++++++++++++++------------- src/formatters/deno_fmt.rs | 7 +++++-- src/formatters/mod.rs | 2 +- src/formatters/prettier.rs | 6 +++--- src/languages/json.rs | 21 ++++++++++++++------- src/languages/mod.rs | 16 +++++++++++++--- src/lib.rs | 2 +- 9 files changed, 61 insertions(+), 34 deletions(-) diff --git a/justfile b/justfile index 7936af61..93feff04 100644 --- a/justfile +++ b/justfile @@ -43,6 +43,8 @@ precommit: just lint just test just update-readme + cargo run -- init + npx prettier --write mdsf.json typos . publish: diff --git a/src/formatters/biome.rs b/src/formatters/biome.rs index 93a2d09a..bc3388a6 100644 --- a/src/formatters/biome.rs +++ b/src/formatters/biome.rs @@ -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\", @@ -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()) diff --git a/src/formatters/clang_format.rs b/src/formatters/clang_format.rs index 98bb4b8a..1b370475 100644 --- a/src/formatters/clang_format.rs +++ b/src/formatters/clang_format.rs @@ -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)] @@ -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)] @@ -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)] @@ -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)] @@ -110,7 +110,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)] @@ -118,13 +118,20 @@ mod test_clang_format { 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()) @@ -132,7 +139,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)] @@ -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)] @@ -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)] @@ -205,6 +212,6 @@ mod test_clang_format { .1 .expect("it to be some"); - assert_eq!(expected_output, output); + assert_eq!(output, expected_output); } } diff --git a/src/formatters/deno_fmt.rs b/src/formatters/deno_fmt.rs index a899866d..fab58be3 100644 --- a/src/formatters/deno_fmt.rs +++ b/src/formatters/deno_fmt.rs @@ -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; @@ -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()) diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index 0c11835e..013bbc30 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -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), diff --git a/src/formatters/prettier.rs b/src/formatters/prettier.rs index b523acdf..b09b9375 100644 --- a/src/formatters/prettier.rs +++ b/src/formatters/prettier.rs @@ -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] @@ -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) diff --git a/src/languages/json.rs b/src/languages/json.rs index d9f85377..9a11b688 100644 --- a/src/languages/json.rs +++ b/src/languages/json.rs @@ -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\", @@ -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() { @@ -116,7 +115,7 @@ mod test_json { let expected_output = "{ \"key\": \"value\", - \"key2\": [\"value2\", \"value3\", 1, null], + \"key2\": [\"value2\", \"value3\", 1, null] } "; @@ -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); } diff --git a/src/languages/mod.rs b/src/languages/mod.rs index 420e04f9..fde8b426 100644 --- a/src/languages/mod.rs +++ b/src/languages/mod.rs @@ -2,6 +2,12 @@ use schemars::JsonSchema; use crate::formatters::MdsfFormatter; +pub enum JsonFlavor { + Json, + JsonC, + Json5, +} + pub enum Language { Blade, C, @@ -23,7 +29,7 @@ pub enum Language { Html, Java, JavaScript, - Json, + Json(JsonFlavor), Just, Kotlin, Lua, @@ -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), @@ -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", diff --git a/src/lib.rs b/src/lib.rs index 7f5e72db..4e3f5af8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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] } } ```