Skip to content

Commit

Permalink
chore(linter): add description to website rules generator (#3670)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 14, 2024
1 parent 2173f23 commit 67e0d30
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
12 changes: 12 additions & 0 deletions crates/oxc_linter/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
52 changes: 28 additions & 24 deletions crates/oxc_linter/src/table.rs
Original file line number Diff line number Diff line change
@@ -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<RuleTableSection>,
Expand All @@ -12,15 +12,15 @@ pub struct RuleTable {

pub struct RuleTableSection {
pub rows: Vec<RuleTableRow>,
pub category: String,
pub category: RuleCategory,
pub rule_column_width: usize,
pub plugin_column_width: usize,
}

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,
}
Expand All @@ -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),
}
})
Expand All @@ -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<String, Vec<RuleTableRow>>, row| {
map.entry(row.category.clone()).or_default().push(row);
FxHashMap::default(),
|mut map: FxHashMap<RuleCategory, Vec<RuleTableRow>>, 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::<Vec<_>>();
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::<Vec<_>>();

RuleTable { total, sections, turned_on_by_default_count: default_rules.len() }
}
Expand All @@ -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, "| {:<rule_width$} | {:<plugin_width$} | Default |", "Rule name", "Source")
.unwrap();
Expand Down

0 comments on commit 67e0d30

Please sign in to comment.