Skip to main content

code_baseline/rules/
mod.rs

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