Skip to content

Commit 02eebfa

Browse files
ntBreGeorge-Ogden
authored andcommitted
Add default indicator to rules table (astral-sh#27724)
Summary -- I saw this suggestion on [Discord]: > Would it make sense to put a mark in the full rule page for rules that are selected by default? I feel like that would be very useful in seeing for example what rules are not selected from a group like UP by default. The extra "default rules" page doesn't help with that, for example. > Like a blue dot, maybe in front of the rules in the table. This PR implements this suggestion with a couple of modifications. I included a check mark instead of a blue dot and as an additional indicator at the end of each row, near the preview, fix, and other indicators, rather than at the start. Test Plan -- Local build: <img width="569" height="237" alt="image" src="https://github.com/user-attachments/assets/1a6db243-ac2c-4b53-a378-541467a6018d" /> [Discord]: https://discord.com/channels/1039017663004942429/1070132471699607623/1536942546972839957
1 parent 6f860a5 commit 02eebfa

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

crates/ruff_dev/src/generate_rules_table.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,31 @@ use strum::IntoEnumIterator;
1010

1111
use ruff_linter::FixAvailability;
1212
use ruff_linter::registry::{Linter, Rule, RuleNamespace};
13+
use ruff_linter::settings::LinterSettings;
14+
use ruff_linter::settings::rule_table::RuleTable;
1315
use ruff_linter::upstream_categories::UpstreamCategoryAndPrefix;
1416
use ruff_options_metadata::OptionsMetadata;
1517
use ruff_workspace::options::Options;
1618

19+
const DEFAULT_SYMBOL: &str = "✅";
1720
const FIX_SYMBOL: &str = "🛠️";
1821
const PREVIEW_SYMBOL: &str = "🧪";
1922
const REMOVED_SYMBOL: &str = "❌";
2023
const WARNING_SYMBOL: &str = "⚠️";
2124
const SPACER: &str = "&nbsp;&nbsp;&nbsp;&nbsp;";
2225

23-
/// Style for the rule's fixability and status icons.
26+
/// Style for the rule's default selection, fixability, and status icons.
2427
const SYMBOL_STYLE: &str = "style='width: 1em; display: inline-block;'";
25-
/// Style for the container wrapping the fixability and status icons.
28+
/// Style for the container wrapping the default selection, fixability, and status icons.
2629
const SYMBOLS_CONTAINER: &str = "style='display: flex; gap: 0.5rem; justify-content: end;'";
2730

28-
fn generate_table(table_out: &mut String, rules: impl IntoIterator<Item = Rule>, linter: &Linter) {
29-
table_out.push_str("| Code { scope='col' } | Name { scope='col' } | Message { scope='col' } | Fix/Status { scope='col' .sr-only } |");
31+
fn generate_table(
32+
table_out: &mut String,
33+
rules: impl IntoIterator<Item = Rule>,
34+
linter: &Linter,
35+
default_rules: &RuleTable,
36+
) {
37+
table_out.push_str("| Code { scope='col' } | Name { scope='col' } | Message { scope='col' } | Status/Fix/Default { scope='col' .sr-only } |");
3038
table_out.push('\n');
3139
table_out.push_str("| ---- | ---- | ------- | -: |");
3240
table_out.push('\n');
@@ -63,6 +71,14 @@ fn generate_table(table_out: &mut String, rules: impl IntoIterator<Item = Rule>,
6371
FixAvailability::None => format!("<span {SYMBOL_STYLE}></span>"),
6472
};
6573

74+
let default_token = if default_rules.enabled(rule) {
75+
format!(
76+
"<span aria-hidden='true' {SYMBOL_STYLE} title='Enabled by default'>{DEFAULT_SYMBOL}</span><span class='sr-only'>Enabled by default</span>"
77+
)
78+
} else {
79+
format!("<span {SYMBOL_STYLE}></span>")
80+
};
81+
6682
let rule_name = rule.name();
6783

6884
// If the message ends in a bracketed expression (like: "Use {replacement}"), escape the
@@ -89,7 +105,7 @@ fn generate_table(table_out: &mut String, rules: impl IntoIterator<Item = Rule>,
89105
#[expect(clippy::or_fun_call)]
90106
let _ = write!(
91107
table_out,
92-
"| {ss}{prefix}{code}{se} {{ #{prefix}{code} }} | {ss}{explanation}{se} | {ss}{message}{se} | <div {SYMBOLS_CONTAINER}>{status_token}{fix_token}</div>|",
108+
"| {ss}{prefix}{code}{se} {{ #{prefix}{code} }} | {ss}{explanation}{se} | {ss}{message}{se} | <div {SYMBOLS_CONTAINER}>{status_token}{fix_token}{default_token}</div>|",
93109
prefix = linter.common_prefix(),
94110
code = linter.code_for_rule(rule).unwrap(),
95111
explanation = rule
@@ -132,10 +148,17 @@ pub(crate) fn generate() -> String {
132148
&mut table_out,
133149
"{SPACER}{FIX_SYMBOL}{SPACER} The rule is automatically fixable by the `--fix` command-line option."
134150
);
151+
table_out.push_str("<br />");
152+
153+
let _ = write!(
154+
&mut table_out,
155+
"{SPACER}{DEFAULT_SYMBOL}{SPACER} The rule is enabled by default."
156+
);
135157
table_out.push_str("\n\n");
136158
table_out.push_str("All rules not marked as preview, deprecated or removed are stable.");
137159
table_out.push('\n');
138160

161+
let default_rules = LinterSettings::default().rules;
139162
for linter in Linter::iter() {
140163
let codes_csv: String = match linter.common_prefix() {
141164
"" => linter
@@ -222,10 +245,10 @@ pub(crate) fn generate() -> String {
222245
}
223246
table_out.push('\n');
224247
table_out.push('\n');
225-
generate_table(&mut table_out, rules.clone(), &linter);
248+
generate_table(&mut table_out, rules.clone(), &linter, &default_rules);
226249
}
227250
} else {
228-
generate_table(&mut table_out, linter.all_rules(), &linter);
251+
generate_table(&mut table_out, linter.all_rules(), &linter, &default_rules);
229252
}
230253
}
231254

0 commit comments

Comments
 (0)