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(ruby): add support for rubocop #46

Merged
merged 3 commits into from
Mar 9, 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
29 changes: 20 additions & 9 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
# Used by gleam format, mix format
# NOTE: should be first since it is sometimes crashes (?)
- uses: erlef/setup-beam@v1
with:
otp-version: "26"
gleam-version: "1.0.0"
elixir-version: "1.16.1"
# Used by ruff
- uses: actions/setup-python@v5
with:
Expand All @@ -56,16 +63,14 @@ jobs:
# Used by zigfmt
- uses: goto-bus-stop/setup-zig@v2
- uses: uncenter/setup-taplo@v1
# Used by gleam format
- uses: erlef/setup-beam@v1
with:
otp-version: "26"
gleam-version: "1.0.0"
elixir-version: "1.16.1"
# Used go shfmt
- uses: actions/setup-go@v5
with:
go-version: "stable"
# Used by rubocop
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3"

- run: rustup toolchain install stable --profile minimal
- run: rustup component add rustfmt clippy
Expand All @@ -79,9 +84,6 @@ jobs:
- name: Validate ruff
run: ruff --version

- name: Validate stylua
run: stylua --version

- name: Validate biome
run: npx --yes @biomejs/biome --version

Expand All @@ -100,9 +102,18 @@ jobs:
- name: Validate mix format
run: mix help format

- name: Install rubocop
run: gem install rubocop

- name: Validate rubocop
run: rubocop --version

- name: Install stylua
run: cargo install stylua

- name: Validate stylua
run: stylua --version

- name: Install shfmt
run: go install mvdan.cc/sh/v3/cmd/shfmt@latest

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mdsf init
| Lua | `stylua` |
| Nim | `nimpretty` |
| Python | `ruff` |
| Ruby | `rubocop` |
| Rust | `rustfmt` |
| Shell | `shfmt` |
| TOML | `taplo` |
Expand Down
32 changes: 32 additions & 0 deletions schemas/v0.0.0/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@
}
]
},
"ruby": {
"default": {
"enabled": true,
"formatter": "rubocop"
},
"allOf": [
{
"$ref": "#/definitions/Ruby"
}
]
},
"rust": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -402,6 +413,27 @@
"type": "string",
"enum": ["ruff"]
},
"Ruby": {
"type": "object",
"properties": {
"enabled": {
"default": true,
"type": "boolean"
},
"formatter": {
"default": "rubocop",
"allOf": [
{
"$ref": "#/definitions/RubyFormatter"
}
]
}
}
},
"RubyFormatter": {
"type": "string",
"enum": ["rubocop"]
},
"Rust": {
"type": "object",
"properties": {
Expand Down
8 changes: 6 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use schemars::JsonSchema;

use crate::languages::{
css::Css, elixir::Elixir, gleam::Gleam, html::Html, javascript::JavaScript, json::Json,
lua::Lua, markdown::Markdown, nim::Nim, python::Python, rust::Rust, shell::Shell, toml::Toml,
typescript::TypeScript, vue::Vue, yaml::Yaml, zig::Zig,
lua::Lua, markdown::Markdown, nim::Nim, python::Python, ruby::Ruby, rust::Rust, shell::Shell,
toml::Toml, typescript::TypeScript, vue::Vue, yaml::Yaml, zig::Zig,
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand Down Expand Up @@ -42,6 +42,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub python: Python,

#[serde(default)]
pub ruby: Ruby,

#[serde(default)]
pub rust: Rust,

Expand Down Expand Up @@ -79,6 +82,7 @@ impl Default for MdsfConfig {
markdown: Markdown::default(),
nim: Nim::default(),
python: Python::default(),
ruby: Ruby::default(),
rust: Rust::default(),
shell: Shell::default(),
toml: Toml::default(),
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/mix_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod test_gleam_format {
};

#[test]
fn it_should_format_gleam() {
fn it_should_format_elixir() {
let input = "
def add(a , b ) do a + b end

Expand Down
2 changes: 2 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod gleam_format;
pub mod mix_format;
pub mod nimpretty;
pub mod prettier;
pub mod rubocop;
pub mod ruff;
pub mod rustfmt;
pub mod shfmt;
Expand Down Expand Up @@ -93,6 +94,7 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
Language::Markdown => config.markdown.format(snippet_path),
Language::Nim => config.nim.format(snippet_path),
Language::Python => config.python.format(snippet_path),
Language::Ruby => config.ruby.format(snippet_path),
Language::Rust => config.rust.format(snippet_path),
Language::Shell => config.shell.format(snippet_path),
Language::Toml => config.toml.format(snippet_path),
Expand Down
46 changes: 46 additions & 0 deletions src/formatters/rubocop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use super::execute_command;

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

cmd.arg("--fix-layout")
.arg("--autocorrect")
.arg("--format")
.arg("quiet")
.arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

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

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

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

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

assert_eq!(expected_output, output);
}
}
13 changes: 8 additions & 5 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum Language {
Markdown,
Nim,
Python,
Ruby,
Rust,
Shell,
Toml,
Expand All @@ -22,7 +23,6 @@ pub enum Language {
// TODO: Haskell,
// TODO: OCaml,
// TODO: Crystal,
// TODO: Ruby,
// TODO: PHP,
// TODO: Java,
// TODO: Kotlin,
Expand Down Expand Up @@ -54,6 +54,7 @@ pub mod lua;
pub mod markdown;
pub mod nim;
pub mod python;
pub mod ruby;
pub mod rust;
pub mod shell;
pub mod toml;
Expand All @@ -74,16 +75,17 @@ impl Language {
"elixir" => Some(Self::Elixir),
"gleam" => Some(Self::Gleam),
"html" => Some(Self::Html),
"js" | "jsx" | "javascript" => Some(Self::JavaScript),
"javascript" | "js" | "jsx" => Some(Self::JavaScript),
"json" => Some(Self::Json),
"lua" => Some(Self::Lua),
"markdown" | "md" => Some(Self::Markdown),
"nim" => Some(Self::Nim),
"python" => Some(Self::Python),
"rust" => Some(Self::Rust),
"sh" | "shell" | "bash" | "zh" => Some(Self::Shell),
"ruby" => Some(Self::Ruby),
"rust" | "rb" => Some(Self::Rust),
"shell" | "sh" | "bash" | "zsh" => Some(Self::Shell),
"toml" => Some(Self::Toml),
"ts" | "tsx" | "typescript" => Some(Self::TypeScript),
"typescript" | "ts" | "tsx" => Some(Self::TypeScript),
"yml" | "yaml" => Some(Self::Yaml),
"zig" => Some(Self::Zig),
"vue" => Some(Self::Vue),
Expand All @@ -105,6 +107,7 @@ impl Language {
Self::Markdown => ".md",
Self::Nim => ".nim",
Self::Python => ".py",
Self::Ruby => ".rb",
Self::Rust => ".rs",
Self::Shell => ".sh",
Self::Toml => ".toml",
Expand Down
43 changes: 43 additions & 0 deletions src/languages/ruby.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use schemars::JsonSchema;

use crate::{config::default_enabled, formatters::rubocop::format_using_rubocop};

use super::LanguageFormatter;

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
pub enum RubyFormatter {
#[default]
#[serde(rename = "rubocop")]
RuboCop,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
pub struct Ruby {
#[serde(default = "default_enabled")]
pub enabled: bool,
#[serde(default)]
pub formatter: RubyFormatter,
}

impl Default for Ruby {
#[inline]
fn default() -> Self {
Self {
enabled: true,
formatter: RubyFormatter::default(),
}
}
}

impl LanguageFormatter for Ruby {
#[inline]
fn format(&self, snippet_path: &std::path::Path) -> std::io::Result<Option<String>> {
if !self.enabled {
return Ok(None);
}

match self.formatter {
RubyFormatter::RuboCop => format_using_rubocop(snippet_path).map(|res| res.1),
}
}
}
12 changes: 12 additions & 0 deletions tests/ruby.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```ruby


def add( a , b )
return a + b
end





```
Loading