use crate::lint::{DOUBLE_ENCODED, Group, Level, pedantic};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Lint {
pub name: &'static str,
pub group: Group,
}
pub(crate) const OUTSIDE_SET: Lint = Lint::new("outside-set", Group::Charset);
pub(crate) const LINTS: &[Lint] = &[
Lint::new("bidi-control", Group::Hazard),
Lint::new("tag-character", Group::Hazard),
Lint::new("stray-bom", Group::Hazard),
Lint::new("control-character", Group::Hazard),
Lint::new("invisible", Group::Hazard),
DOUBLE_ENCODED,
OUTSIDE_SET,
pedantic::NOT_NFC,
pedantic::NFKC_COMPAT,
pedantic::UNICODE_SPACE,
pedantic::MIXED_SCRIPT,
pedantic::CONFUSABLE,
pedantic::CRLF,
pedantic::TRAILING_WHITESPACE,
pedantic::FINAL_NEWLINE,
];
impl Lint {
pub(crate) const fn new(name: &'static str, group: Group) -> Self {
Self { name, group }
}
pub fn named(name: &str) -> Option<Self> {
LINTS.iter().copied().find(|lint| lint.name == name)
}
pub fn default_level(self) -> Level {
self.group.default_level()
}
pub(crate) fn grantable(class: &str) -> Option<Self> {
match class {
"tags" => Self::named("tag-character"),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::{LINTS, Lint};
use crate::lint::{Group, Level};
#[test]
fn every_registered_lint_resolves_back_to_itself() {
for lint in LINTS {
assert_eq!(Lint::named(lint.name), Some(*lint));
}
}
#[test]
fn an_unregistered_name_is_not_a_lint() {
assert_eq!(Lint::named("no-such-lint"), None);
assert_eq!(Lint::named(""), None);
}
#[test]
fn no_lint_name_is_registered_twice() {
let mut seen: Vec<&str> = Vec::new();
for lint in LINTS {
assert!(!seen.contains(&lint.name), "duplicate: {}", lint.name);
seen.push(lint.name);
}
}
#[test]
fn no_lint_is_named_after_a_group() {
for lint in LINTS {
assert_eq!(Group::named(lint.name), None, "{}", lint.name);
}
}
#[test]
fn a_lint_starts_at_its_groups_level() {
let outside = Lint::named("outside-set");
assert_eq!(outside.map(Lint::default_level), Some(Level::Deny));
let nfc = Lint::named("not-nfc");
assert_eq!(nfc.map(Lint::default_level), Some(Level::Allow));
}
#[test]
fn every_hazard_lint_forbids_before_any_rule_speaks() {
let hazards = LINTS.iter().filter(|l| l.group == Group::Hazard);
assert_eq!(hazards.clone().count(), 6);
for lint in hazards {
assert_eq!(lint.default_level(), Level::Forbid, "{}", lint.name);
}
}
#[test]
fn every_pedantic_lint_is_silent_until_asked() {
let pedantic = LINTS.iter().filter(|l| l.group == Group::Pedantic);
for lint in pedantic {
assert_eq!(lint.default_level(), Level::Allow, "{}", lint.name);
}
}
}