rigsql_rules/layout/
lt02.rs1use rigsql_core::{Segment, SegmentType, TokenKind};
2
3use crate::rule::{CrawlType, Rule, RuleContext, RuleGroup};
4use crate::violation::LintViolation;
5
6#[derive(Debug)]
10pub struct RuleLT02 {
11 pub indent_size: usize,
12}
13
14impl Default for RuleLT02 {
15 fn default() -> Self {
16 Self { indent_size: 4 }
17 }
18}
19
20impl Rule for RuleLT02 {
21 fn code(&self) -> &'static str {
22 "LT02"
23 }
24 fn name(&self) -> &'static str {
25 "layout.indent"
26 }
27 fn description(&self) -> &'static str {
28 "Incorrect indentation."
29 }
30 fn explanation(&self) -> &'static str {
31 "SQL should use consistent indentation. Each indentation level should use \
32 the same number of spaces (default 4). Tabs should not be mixed with spaces."
33 }
34 fn groups(&self) -> &[RuleGroup] {
35 &[RuleGroup::Layout]
36 }
37 fn is_fixable(&self) -> bool {
38 true
39 }
40
41 fn configure(&mut self, settings: &std::collections::HashMap<String, String>) {
42 if let Some(val) = settings.get("indent_unit") {
43 if val == "tab" {
44 self.indent_size = 1; }
46 }
47 if let Some(val) = settings.get("tab_space_size") {
48 if let Ok(n) = val.parse() {
49 self.indent_size = n;
50 }
51 }
52 }
53
54 fn crawl_type(&self) -> CrawlType {
55 CrawlType::Segment(vec![SegmentType::Whitespace])
56 }
57
58 fn eval(&self, ctx: &RuleContext) -> Vec<LintViolation> {
59 let Segment::Token(t) = ctx.segment else {
60 return vec![];
61 };
62 if t.token.kind != TokenKind::Whitespace {
63 return vec![];
64 }
65
66 let text = t.token.text.as_str();
67
68 if ctx.index_in_parent == 0 {
70 return vec![];
71 }
72 let prev = &ctx.siblings[ctx.index_in_parent - 1];
73 if prev.segment_type() != SegmentType::Newline {
74 return vec![];
75 }
76
77 if text.contains('\t') && text.contains(' ') {
79 return vec![LintViolation::new(
80 self.code(),
81 "Mixed tabs and spaces in indentation.",
82 t.token.span,
83 )];
84 }
85
86 if !text.contains('\t') && text.len() % self.indent_size != 0 {
88 return vec![LintViolation::new(
89 self.code(),
90 format!(
91 "Indentation is not a multiple of {} spaces (found {} spaces).",
92 self.indent_size,
93 text.len()
94 ),
95 t.token.span,
96 )];
97 }
98
99 vec![]
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106 use crate::test_utils::lint_sql;
107
108 #[test]
109 fn test_lt02_flags_odd_indent() {
110 let violations = lint_sql("SELECT *\n FROM t", RuleLT02::default());
111 assert!(!violations.is_empty());
112 assert!(violations.iter().all(|v| v.rule_code == "LT02"));
113 }
114
115 #[test]
116 fn test_lt02_accepts_4space_indent() {
117 let violations = lint_sql("SELECT *\n FROM t", RuleLT02::default());
118 assert_eq!(violations.len(), 0);
119 }
120
121 #[test]
122 fn test_lt02_flags_mixed_tabs_spaces() {
123 let violations = lint_sql("SELECT *\n\t FROM t", RuleLT02::default());
124 assert!(!violations.is_empty());
125 assert!(violations.iter().all(|v| v.rule_code == "LT02"));
126 }
127}