use crate::{
analysis::{
linting::{IssueCategory, IssueSeverity, LintIssue, LintRule},
ScriptAnalysis,
},
parser::Section,
};
use alloc::{format, vec::Vec};
pub struct MissingStyleRule;
impl LintRule for MissingStyleRule {
fn id(&self) -> &'static str {
"missing-style"
}
fn name(&self) -> &'static str {
"Missing Style"
}
fn description(&self) -> &'static str {
"Detects events referencing non-existent styles"
}
fn default_severity(&self) -> IssueSeverity {
IssueSeverity::Error
}
fn category(&self) -> IssueCategory {
IssueCategory::Styling
}
fn check_script(&self, analysis: &ScriptAnalysis) -> Vec<LintIssue> {
let mut issues = Vec::new();
let style_names: Vec<&str> = analysis
.resolved_styles()
.iter()
.map(|style| style.name)
.collect();
if let Some(Section::Events(events)) = analysis
.script()
.sections()
.iter()
.find(|s| matches!(s, Section::Events(_)))
{
for event in events {
if !style_names.contains(&event.style) {
let issue = LintIssue::new(
self.default_severity(),
IssueCategory::Styling,
self.id(),
format!("Event references undefined style: {}", event.style),
)
.with_suggested_fix(format!(
"Define style '{}' or use an existing style",
event.style
));
issues.push(issue);
}
}
}
issues
}
}