Skip to main content

code_baseline/rules/
mod.rs

1pub mod ast;
2pub mod banned_dependency;
3pub mod banned_import;
4pub mod banned_pattern;
5pub mod factory;
6pub mod file_presence;
7pub mod ratchet;
8pub mod required_pattern;
9pub mod tailwind_dark_mode;
10pub mod tailwind_theme_tokens;
11pub mod window_pattern;
12
13use crate::config::Severity;
14use std::path::{Path, PathBuf};
15
16/// A lint rule that checks source files for violations.
17pub trait Rule: Send + Sync {
18    /// Unique identifier for this rule (e.g. `"tailwind-dark-mode"`).
19    fn id(&self) -> &str;
20
21    /// Severity level reported when the rule fires.
22    fn severity(&self) -> Severity;
23
24    /// Optional glob pattern restricting which files are scanned.
25    fn file_glob(&self) -> Option<&str>;
26
27    /// Scan a single file and return any violations found.
28    fn check_file(&self, ctx: &ScanContext) -> Vec<Violation>;
29}
30
31/// The file currently being scanned.
32pub struct ScanContext<'a> {
33    pub file_path: &'a Path,
34    pub content: &'a str,
35}
36
37/// Machine-actionable fix data for a violation.
38#[derive(Debug, Clone)]
39pub struct Fix {
40    pub old: String,
41    pub new: String,
42}
43
44/// A single violation emitted by a rule.
45#[derive(Debug, Clone)]
46pub struct Violation {
47    pub rule_id: String,
48    pub severity: Severity,
49    pub file: PathBuf,
50    pub line: Option<usize>,
51    pub column: Option<usize>,
52    pub message: String,
53    pub suggest: Option<String>,
54    pub source_line: Option<String>,
55    pub fix: Option<Fix>,
56}
57
58/// Errors that can occur when constructing a rule from config.
59#[derive(Debug)]
60pub enum RuleBuildError {
61    InvalidRegex(String, regex::Error),
62    MissingField(String, &'static str),
63}
64
65impl std::fmt::Display for RuleBuildError {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        match self {
68            RuleBuildError::InvalidRegex(id, err) => {
69                write!(f, "rule '{}': invalid regex: {}", id, err)
70            }
71            RuleBuildError::MissingField(id, field) => {
72                write!(f, "rule '{}': missing required field '{}'", id, field)
73            }
74        }
75    }
76}
77
78impl std::error::Error for RuleBuildError {}