Skip to content

Commit

Permalink
feat(ruby): add support for standardrb (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 21, 2024
1 parent 3fff094 commit 30dceda
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,8 @@ jobs:
- name: yamlfmt
run: go install github.com/google/yamlfmt/cmd/yamlfmt@latest

- name: standardrb
run: gem install standardrb

- name: run tests
run: cargo test
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub mod scalafmt;
pub mod shfmt;
pub mod sql_formatter;
pub mod sqlfluff;
pub mod standardrb;
pub mod stylua;
pub mod swiftformat;
pub mod taplo;
Expand Down
43 changes: 43 additions & 0 deletions src/formatters/standardrb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::execute_command;

#[inline]
pub fn format_using_standardrb(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
let mut cmd = std::process::Command::new("standardrb");

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

execute_command(&mut cmd, snippet_path)
}

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

#[test_with::executable(standardrb)]
#[test]
fn it_should_format_ruby() {
let input = "def add( a , b )
return a + b
end";

let expected_output = "def add(a, b)
a + b
end
";

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

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

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

use crate::formatters::{
rubocop::format_using_rubocop, rubyfmt::format_using_rubyfmt, rufo::format_using_rufo,
MdsfFormatter,
standardrb::format_using_standardrb, MdsfFormatter,
};

use super::{Lang, LanguageFormatter};
Expand All @@ -17,6 +17,8 @@ pub enum Ruby {
RuboCop,
#[serde(rename = "rufo")]
Rufo,
#[serde(rename = "standardrb")]
Standardrb,
}

impl Default for Lang<Ruby> {
Expand All @@ -36,6 +38,7 @@ impl Default for MdsfFormatter<Ruby> {
Self::Single(Ruby::RuboCop),
Self::Single(Ruby::Rufo),
Self::Single(Ruby::RubyFmt),
Self::Single(Ruby::Standardrb),
])])
}
}
Expand All @@ -50,6 +53,7 @@ impl LanguageFormatter for Ruby {
Self::RuboCop => format_using_rubocop(snippet_path),
Self::Rufo => format_using_rufo(snippet_path),
Self::RubyFmt => format_using_rubyfmt(snippet_path),
Self::Standardrb => format_using_standardrb(snippet_path),
}
}
}
Expand Down Expand Up @@ -136,4 +140,28 @@ end

assert_eq!(output, expected_output);
}

#[test_with::executable(standardrb)]
#[test]
fn test_standardrb() {
let expected_output = "def add(a, b)
a + b
end
";

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

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!(output, expected_output);
}
}

0 comments on commit 30dceda

Please sign in to comment.