From 67e0d3077afb6700a64f72df9a4d588cd8fe3a7b Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 14 Jun 2024 15:32:23 +0800 Subject: [PATCH] chore(linter): add description to website rules generator (#3670) --- crates/oxc_linter/src/rule.rs | 12 ++++++++ crates/oxc_linter/src/table.rs | 52 ++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/crates/oxc_linter/src/rule.rs b/crates/oxc_linter/src/rule.rs index 1581986b7..9c17d9dd9 100644 --- a/crates/oxc_linter/src/rule.rs +++ b/crates/oxc_linter/src/rule.rs @@ -70,6 +70,18 @@ impl RuleCategory { _ => None, } } + + pub fn description(self) -> &'static str { + match self { + Self::Correctness => "Code that is outright wrong or useless.", + Self::Suspicious => "code that is most likely wrong or useless.", + Self::Pedantic => "Lints which are rather strict or have occasional false positives.", + Self::Perf => "Code that can be written to run faster.", + Self::Style => "Code that should be written in a more idiomatic way.", + Self::Restriction => "Lints which prevent the use of language and library features. Must not be enabled as a whole, should be considered on a case-by-case basis before enabling.", + Self::Nursery => "New lints that are still under development.", + } + } } impl fmt::Display for RuleCategory { diff --git a/crates/oxc_linter/src/table.rs b/crates/oxc_linter/src/table.rs index 76e2402a7..e59c488da 100644 --- a/crates/oxc_linter/src/table.rs +++ b/crates/oxc_linter/src/table.rs @@ -1,8 +1,8 @@ -use std::{collections::HashMap, fmt::Write}; +use std::fmt::Write; -use rustc_hash::FxHashSet; +use rustc_hash::{FxHashMap, FxHashSet}; -use crate::{rules::RULES, Linter}; +use crate::{rules::RULES, Linter, RuleCategory}; pub struct RuleTable { pub sections: Vec, @@ -12,7 +12,7 @@ pub struct RuleTable { pub struct RuleTableSection { pub rows: Vec, - pub category: String, + pub category: RuleCategory, pub rule_column_width: usize, pub plugin_column_width: usize, } @@ -20,7 +20,7 @@ pub struct RuleTableSection { pub struct RuleTableRow { pub name: &'static str, pub plugin: String, - pub category: String, + pub category: RuleCategory, pub documentation: Option<&'static str>, pub turned_on_by_default: bool, } @@ -47,7 +47,7 @@ impl RuleTable { name, documentation: rule.documentation(), plugin: rule.plugin_name().to_string(), - category: rule.category().to_string(), + category: rule.category(), turned_on_by_default: default_rules.contains(name), } }) @@ -58,28 +58,30 @@ impl RuleTable { rows.sort_by_key(|row| (row.plugin.clone(), row.name)); let mut rows_by_category = rows.into_iter().fold( - HashMap::default(), - |mut map: HashMap>, row| { - map.entry(row.category.clone()).or_default().push(row); + FxHashMap::default(), + |mut map: FxHashMap>, row| { + map.entry(row.category).or_default().push(row); map }, ); - let sections = - ["Correctness", "Perf", "Restriction", "Suspicious", "Pedantic", "Style", "Nursery"] - .into_iter() - .filter_map(|category| { - let rows = rows_by_category.remove(category)?; - let rule_column_width = rows.iter().map(|r| r.name.len()).max()?; - let plugin_column_width = rows.iter().map(|r| r.plugin.len()).max()?; - Some(RuleTableSection { - rows, - category: category.to_string(), - rule_column_width, - plugin_column_width, - }) - }) - .collect::>(); + let sections = [ + RuleCategory::Correctness, + RuleCategory::Perf, + RuleCategory::Restriction, + RuleCategory::Suspicious, + RuleCategory::Pedantic, + RuleCategory::Style, + RuleCategory::Nursery, + ] + .into_iter() + .filter_map(|category| { + let rows = rows_by_category.remove(&category)?; + let rule_column_width = rows.iter().map(|r| r.name.len()).max()?; + let plugin_column_width = rows.iter().map(|r| r.plugin.len()).max()?; + Some(RuleTableSection { rows, category, rule_column_width, plugin_column_width }) + }) + .collect::>(); RuleTable { total, sections, turned_on_by_default_count: default_rules.len() } } @@ -94,6 +96,8 @@ impl RuleTableSection { let plugin_width = self.plugin_column_width; writeln!(s, "## {} ({}):", category, rows.len()).unwrap(); + writeln!(s, "{}", category.description()).unwrap(); + let x = ""; writeln!(s, "| {: