Skip to content

Commit

Permalink
feat(purescript): support purs-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 19, 2024
1 parent 2b335e3 commit 644607d
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ mdsf init
| Ruby | `rubocop` |
| Rust | `rustfmt` |
| Shell | `shfmt` |
| PureScript | `purs-tidy` |
| SQL | `sqlfluff`, `sql-formatter` |
| TOML | `taplo` |
| TypeScript | `prettier`, `biome`, `deno_fmt` |
Expand Down
40 changes: 40 additions & 0 deletions schemas/v0.0.1/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@
}
]
},
"purescript": {
"default": {
"enabled": true,
"formatter": "purs-tidy"
},
"allOf": [
{
"$ref": "#/definitions/Lang_for_PureScript"
}
]
},
"python": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -748,6 +759,18 @@
}
}
},
"Lang_for_PureScript": {
"type": "object",
"required": ["enabled", "formatter"],
"properties": {
"enabled": {
"type": "boolean"
},
"formatter": {
"$ref": "#/definitions/MdsfFormatter_for_PureScript"
}
}
},
"Lang_for_Python": {
"type": "object",
"required": ["enabled", "formatter"],
Expand Down Expand Up @@ -1211,6 +1234,19 @@
}
]
},
"MdsfFormatter_for_PureScript": {
"anyOf": [
{
"$ref": "#/definitions/PureScript"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/MdsfFormatter_for_PureScript"
}
}
]
},
"MdsfFormatter_for_Python": {
"anyOf": [
{
Expand Down Expand Up @@ -1396,6 +1432,10 @@
"type": "string",
"enum": ["clang-format"]
},
"PureScript": {
"type": "string",
"enum": ["purs-tidy"]
},
"Python": {
"type": "string",
"enum": ["ruff", "black", "yapf", "blue", "autopep8", "isort", "usort"]
Expand Down
10 changes: 7 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::languages::{
blade::Blade, c::C, cpp::Cpp, crystal::Crystal, csharp::CSharp, css::Css, dart::Dart,
elixir::Elixir, elm::Elm, gleam::Gleam, go::Go, graphql::GraphQL, html::Html, java::Java,
javascript::JavaScript, json::Json, just::Just, lua::Lua, markdown::Markdown, nim::Nim,
objective_c::ObjectiveC, ocaml::OCaml, protobuf::Protobuf, python::Python, rescript::ReScript,
roc::Roc, ruby::Ruby, rust::Rust, shell::Shell, sql::Sql, toml::Toml, typescript::TypeScript,
vue::Vue, xml::Xml, yaml::Yaml, zig::Zig, Lang,
objective_c::ObjectiveC, ocaml::OCaml, protobuf::Protobuf, purescript::PureScript,
python::Python, rescript::ReScript, roc::Roc, ruby::Ruby, rust::Rust, shell::Shell, sql::Sql,
toml::Toml, typescript::TypeScript, vue::Vue, xml::Xml, yaml::Yaml, zig::Zig, Lang,
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand Down Expand Up @@ -82,6 +82,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub ocaml: Lang<OCaml>,

#[serde(default)]
pub purescript: Lang<PureScript>,

#[serde(default)]
pub protobuf: Lang<Protobuf>,

Expand Down Expand Up @@ -155,6 +158,7 @@ impl Default for MdsfConfig {
ocaml: Lang::<OCaml>::default(),
protobuf: Lang::<Protobuf>::default(),
python: Lang::<Python>::default(),
purescript: Lang::<PureScript>::default(),
rescript: Lang::<ReScript>::default(),
roc: Lang::<Roc>::default(),
ruby: Lang::<Ruby>::default(),
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod blue;
pub mod clang_format;
pub mod crystal_format;
pub mod dart_format;
pub mod deno_format;
pub mod deno_fmt;
pub mod elm_format;
pub mod gleam_format;
pub mod gofmt;
Expand All @@ -24,6 +24,7 @@ pub mod mix_format;
pub mod nimpretty;
pub mod ocamlformat;
pub mod prettier;
pub mod purs_tidy;
pub mod rescript_format;
pub mod roc_format;
pub mod rubocop;
Expand Down Expand Up @@ -101,6 +102,7 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S

if let Ok(Some(formatted_code)) = match language {
Language::Blade => config.blade.format(snippet_path),
Language::PureScript => config.purescript.format(snippet_path),
Language::C => config.c.format(snippet_path),
Language::CSharp => config.csharp.format(snippet_path),
Language::Cpp => config.cpp.format(snippet_path),
Expand Down
70 changes: 70 additions & 0 deletions src/formatters/purs_tidy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::runners::{setup_npm_script, JavaScriptRuntime};

use super::execute_command;

#[inline]
fn set_purs_tidy_args(cmd: &mut std::process::Command, snippet_path: &std::path::Path) {
cmd.arg("format-in-place").arg(snippet_path);
}

#[inline]
fn invoke_purs_tidy(
mut cmd: std::process::Command,
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
set_purs_tidy_args(&mut cmd, snippet_path);

execute_command(&mut cmd, snippet_path)
}

#[inline]
pub fn format_using_purs_tidy(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
invoke_purs_tidy(
setup_npm_script(JavaScriptRuntime::default(), "purs-tidy"),
snippet_path,
)
}

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

use super::format_using_purs_tidy;

#[test]
fn it_should_format_purescript() {
let input = r#"module Test.Main where
import Prelude
import Effect (Effect)
import Effect.Class.Console (log)
main :: Effect Unit
main = do
log "You should add some tests.""#;

let expected_output = r#"module Test.Main where
import Prelude
import Effect (Effect)
import Effect.Class.Console (log)
main :: Effect Unit
main = do
log "You should add some tests.""#;

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

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

assert_eq!(expected_output, output);
}
}
2 changes: 1 addition & 1 deletion src/formatters/usort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn format_using_usort(
}

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

use super::format_using_usort;
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/xmllint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn format_using_xmllint(
}

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

use super::format_using_xmllint;
Expand Down
2 changes: 1 addition & 1 deletion src/languages/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use schemars::JsonSchema;

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

use super::{Lang, LanguageFormatter};
Expand Down
2 changes: 1 addition & 1 deletion src/languages/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use schemars::JsonSchema;

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

use super::{Lang, LanguageFormatter};
Expand Down
4 changes: 4 additions & 0 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub enum Language {
Xml,
Yaml,
Zig,
PureScript,
// TODO: Haskell,
// TODO: PHP,
// TODO: Kotlin,
Expand Down Expand Up @@ -77,6 +78,7 @@ pub mod nim;
pub mod objective_c;
pub mod ocaml;
pub mod protobuf;
pub mod purescript;
pub mod python;
pub mod rescript;
pub mod roc;
Expand Down Expand Up @@ -140,6 +142,7 @@ impl Language {
"elm" => Some(Self::Elm),
"rescript" => Some(Self::ReScript),
"xml" => Some(Self::Xml),
"purescript" | "purs" => Some(Self::PureScript),
_ => None,
}
}
Expand Down Expand Up @@ -185,6 +188,7 @@ impl Language {
Self::OCaml => ".ml",
Self::ReScript => ".res",
Self::Xml => ".xml",
Self::PureScript => ".purs",
}
}
}
Expand Down
113 changes: 113 additions & 0 deletions src/languages/purescript.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use schemars::JsonSchema;

use crate::formatters::{purs_tidy::format_using_purs_tidy, MdsfFormatter};

use super::{Lang, LanguageFormatter};

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum PureScript {
#[default]
#[serde(rename = "purs-tidy")]
PursTidy,
}

impl Default for Lang<PureScript> {
#[inline]
fn default() -> Self {
Self {
enabled: true,
formatter: MdsfFormatter::<PureScript>::default(),
}
}
}

impl Default for MdsfFormatter<PureScript> {
#[inline]
fn default() -> Self {
Self::Single(PureScript::PursTidy)
}
}

impl LanguageFormatter for PureScript {
#[inline]
fn format_snippet(
&self,
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::PursTidy => format_using_purs_tidy(snippet_path),
}
}
}

#[cfg(test)]
mod test_purescript {
use crate::{
formatters::{setup_snippet, MdsfFormatter},
languages::Lang,
};

use super::PureScript;

const INPUT: &str = r#"module Test.Main where
import Prelude
import Effect (Effect)
import Effect.Class.Console (log)
main :: Effect Unit
main = do
log "You should add some tests.""#;

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

#[test]
fn it_should_be_enabled_by_default() {
assert!(Lang::<PureScript>::default().enabled);
}

#[test]
fn it_should_not_format_when_enabled_is_false() {
let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

assert!(Lang::<PureScript> {
enabled: false,
formatter: MdsfFormatter::Single(PureScript::default())
}
.format(snippet_path)
.expect("it to not fail")
.is_none());
}

#[test]
fn test_purs_tidy() {
let l = Lang::<PureScript> {
enabled: true,
formatter: MdsfFormatter::Single(PureScript::PursTidy),
};

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 = r#"module Test.Main where
import Prelude
import Effect (Effect)
import Effect.Class.Console (log)
main :: Effect Unit
main = do
log "You should add some tests.""#;

assert_eq!(output, expected_output);
}
}
2 changes: 1 addition & 1 deletion src/languages/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;

use crate::formatters::{
biome::format_using_biome, deno_format::format_using_deno_fmt, prettier::format_using_prettier,
biome::format_using_biome, deno_fmt::format_using_deno_fmt, prettier::format_using_prettier,
MdsfFormatter,
};

Expand Down

0 comments on commit 644607d

Please sign in to comment.