mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00

The new `ruff rule` output format introduced in
551b810aeb
doesn't print Markdown but
rather some rich text with escape sequences for colors and links,
it's actually the "text" format that prints Markdown, so naming the new
format "markdown" is very confusing. This commit therefore renames it to
"pretty".
This isn't a breaking change since there hasn't been a release yet.
89 lines
2.8 KiB
Rust
89 lines
2.8 KiB
Rust
use std::io;
|
|
use std::io::BufWriter;
|
|
use std::io::Write;
|
|
|
|
use anyhow::Result;
|
|
use itertools::Itertools;
|
|
use serde::Serialize;
|
|
use strum::IntoEnumIterator;
|
|
|
|
use ruff::registry::{Linter, RuleNamespace, UpstreamCategory};
|
|
|
|
use crate::args::HelpFormat;
|
|
|
|
#[derive(Serialize)]
|
|
struct LinterInfo {
|
|
prefix: &'static str,
|
|
name: &'static str,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
categories: Option<Vec<LinterCategoryInfo>>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
struct LinterCategoryInfo {
|
|
prefix: &'static str,
|
|
name: &'static str,
|
|
}
|
|
|
|
pub fn linter(format: HelpFormat) -> Result<()> {
|
|
let mut stdout = BufWriter::new(io::stdout().lock());
|
|
let mut output = String::new();
|
|
|
|
match format {
|
|
HelpFormat::Text => {
|
|
for linter in Linter::iter() {
|
|
let prefix = match linter.common_prefix() {
|
|
"" => linter
|
|
.upstream_categories()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|UpstreamCategory(prefix, ..)| prefix.as_ref())
|
|
.join("/"),
|
|
prefix => prefix.to_string(),
|
|
};
|
|
output.push_str(&format!("{:>4} {}\n", prefix, linter.name()));
|
|
}
|
|
}
|
|
|
|
HelpFormat::Json => {
|
|
let linters: Vec<_> = Linter::iter()
|
|
.map(|linter_info| LinterInfo {
|
|
prefix: linter_info.common_prefix(),
|
|
name: linter_info.name(),
|
|
categories: linter_info.upstream_categories().map(|cats| {
|
|
cats.iter()
|
|
.map(|UpstreamCategory(prefix, name, ..)| LinterCategoryInfo {
|
|
prefix: prefix.as_ref(),
|
|
name,
|
|
})
|
|
.collect()
|
|
}),
|
|
})
|
|
.collect();
|
|
output.push_str(&serde_json::to_string_pretty(&linters)?);
|
|
output.push('\n');
|
|
}
|
|
|
|
HelpFormat::Pretty => {
|
|
output.push_str(&format!("| {:>6} | {:<27} |\n", "Prefix", "Name"));
|
|
output.push_str(&format!("| {:>6} | {:<27} |\n", "------", "-".repeat(27)));
|
|
|
|
for linter in Linter::iter() {
|
|
let prefix = match linter.common_prefix() {
|
|
"" => linter
|
|
.upstream_categories()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|UpstreamCategory(prefix, ..)| prefix.as_ref())
|
|
.join("/"),
|
|
prefix => prefix.to_string(),
|
|
};
|
|
output.push_str(&format!("| {:>6} | {:<27} |\n", prefix, linter.name()));
|
|
}
|
|
}
|
|
}
|
|
|
|
write!(stdout, "{output}")?;
|
|
|
|
Ok(())
|
|
}
|