Rework upstream categories so we can all_rules() (#5591)

## Summary

This PR reworks the `upstream_categories` mechanism that is only used
for documentation purposes to make it easier to generate docs using
`all_rules()`. The new implementation also relies on "tribal knowledge"
about rule codes, so it's not the best implementation, but gets us
forward.

Another option would be to change the rule-defining proc macros to allow
configuring an optional `RuleCategory`, but that seems more heavy-handed
and possibly unnecessary in the long run...

Draft since this builds on #5439.

cc @charliermarsh :)
This commit is contained in:
Aarni Koskela 2023-07-10 16:41:26 +03:00 committed by GitHub
parent 089a671adb
commit 24bcbb85a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 101 additions and 40 deletions

View file

@ -3,8 +3,9 @@
use itertools::Itertools;
use strum::IntoEnumIterator;
use ruff::registry::{Linter, Rule, RuleNamespace, UpstreamCategory};
use ruff::registry::{Linter, Rule, RuleNamespace};
use ruff::settings::options::Options;
use ruff::upstream_categories::UpstreamCategoryAndPrefix;
use ruff_diagnostics::AutofixKind;
const FIX_SYMBOL: &str = "🛠";
@ -69,7 +70,7 @@ pub(crate) fn generate() -> String {
.upstream_categories()
.unwrap()
.iter()
.map(|UpstreamCategory(prefix, ..)| prefix.short_code())
.map(|c| c.prefix)
.join(", "),
prefix => prefix.to_string(),
};
@ -114,16 +115,20 @@ pub(crate) fn generate() -> String {
table_out.push('\n');
}
if let Some(categories) = linter.upstream_categories() {
for UpstreamCategory(prefix, name) in categories {
table_out.push_str(&format!(
"#### {name} ({}{})",
linter.common_prefix(),
prefix.short_code()
));
let rules_by_upstream_category = linter
.all_rules()
.map(|rule| (rule.upstream_category(&linter), rule))
.into_group_map();
if rules_by_upstream_category.len() > 1 {
for (opt, rules) in &rules_by_upstream_category {
if opt.is_some() {
let UpstreamCategoryAndPrefix { category, prefix } = opt.unwrap();
table_out.push_str(&format!("#### {category} ({prefix})"));
}
table_out.push('\n');
table_out.push('\n');
generate_table(&mut table_out, prefix.clone().rules(), &linter);
generate_table(&mut table_out, rules.clone(), &linter);
}
} else {
generate_table(&mut table_out, linter.all_rules(), &linter);