Skip to main content

rigsql_rules/aliasing/
al07.rs

1use rigsql_core::SegmentType;
2
3use crate::rule::{CrawlType, Rule, RuleContext, RuleGroup};
4use crate::violation::LintViolation;
5
6/// AL07: Table aliases should follow a naming convention.
7///
8/// This is a stub rule that is disabled by default. When enabled via
9/// `force_enable`, it flags all table aliases as requiring review against
10/// team naming conventions.
11#[derive(Debug, Default)]
12pub struct RuleAL07 {
13    pub force_enable: bool,
14}
15
16impl Rule for RuleAL07 {
17    fn code(&self) -> &'static str {
18        "AL07"
19    }
20    fn name(&self) -> &'static str {
21        "aliasing.table_naming"
22    }
23    fn description(&self) -> &'static str {
24        "Table aliases should follow a naming convention."
25    }
26    fn explanation(&self) -> &'static str {
27        "Table aliases should be meaningful and follow a consistent naming convention \
28         rather than using single letters or arbitrary abbreviations. This rule is \
29         disabled by default as naming conventions vary by team."
30    }
31    fn groups(&self) -> &[RuleGroup] {
32        &[RuleGroup::Aliasing]
33    }
34    fn is_fixable(&self) -> bool {
35        false
36    }
37
38    fn configure(&mut self, settings: &std::collections::HashMap<String, String>) {
39        if let Some(val) = settings.get("force_enable") {
40            self.force_enable = val.eq_ignore_ascii_case("true") || val == "1";
41        }
42    }
43
44    fn crawl_type(&self) -> CrawlType {
45        CrawlType::Segment(vec![SegmentType::AliasExpression])
46    }
47
48    fn eval(&self, ctx: &RuleContext) -> Vec<LintViolation> {
49        if !self.force_enable {
50            return vec![];
51        }
52
53        // Only apply to table aliases (FROM/JOIN context)
54        let in_table_context = ctx.parent.is_some_and(|p| {
55            let pt = p.segment_type();
56            pt == SegmentType::FromClause || pt == SegmentType::JoinClause
57        });
58
59        if !in_table_context {
60            return vec![];
61        }
62
63        vec![LintViolation::new(
64            self.code(),
65            "Table alias does not follow naming convention.",
66            ctx.segment.span(),
67        )]
68    }
69}