1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use ahash::AHashMap;

use crate::core::config::Value;
use crate::core::rules::base::{Erased, ErasedRule, LintResult, Rule};
use crate::core::rules::context::RuleContext;
use crate::core::rules::crawlers::{Crawler, RootOnlyCrawler};
use crate::utils::reflow::sequence::ReflowSequence;

#[derive(Default, Debug, Clone)]
pub struct RuleLT02 {}

impl Rule for RuleLT02 {
    fn load_from_config(&self, _config: &AHashMap<String, Value>) -> ErasedRule {
        RuleLT02::default().erased()
    }

    fn name(&self) -> &'static str {
        "layout.indent"
    }

    fn description(&self) -> &'static str {
        "Incorrect Indentation."
    }

    fn eval(&self, context: RuleContext) -> Vec<LintResult> {
        ReflowSequence::from_root(context.segment, context.config.unwrap()).reindent().results()
    }

    fn crawl_behaviour(&self) -> Crawler {
        RootOnlyCrawler.into()
    }
}

#[cfg(test)]
mod tests {
    use crate::api::simple::{fix, lint};
    use crate::core::rules::base::{Erased, ErasedRule};
    use crate::rules::layout::LT02::RuleLT02;

    fn rules() -> Vec<ErasedRule> {
        vec![RuleLT02::default().erased()]
    }

    #[test]
    fn test_fail_reindent_first_line_1() {
        let fail_str = "     SELECT 1";
        let violations = lint(fail_str.into(), "ansi".into(), rules(), None, None).unwrap();

        assert_eq!(violations[0].desc(), "First line should not be indented.");
        assert_eq!(violations.len(), 1);
    }

    #[test]
    fn test_fail_reindent_first_line_2() {
        let fixed = fix("  select 1 from tbl;".into(), rules());
        assert_eq!(fixed, "select 1 from tbl;");
    }

    #[test]
    fn test_pass_indentation_of_comments_1() {
        let sql = "
SELECT
    -- Compute the thing
    (a + b) AS c
FROM
    acceptable_buckets"
            .trim_start();

        let violations = lint(sql.into(), "ansi".into(), rules(), None, None).unwrap();
        assert_eq!(violations, []);
    }

    #[test]
    fn test_pass_indentation_of_comments_2() {
        let pass_str = "
SELECT
    user_id
FROM
    age_data
JOIN
    audience_size
    USING (user_id, list_id)
-- We LEFT JOIN because blah
LEFT JOIN
    verts
    USING
        (user_id)"
            .trim_start();

        let violations = lint(pass_str.into(), "ansi".into(), rules(), None, None).unwrap();
        assert_eq!(violations, []);
    }

    #[test]
    #[ignore = "impl config"]
    fn test_fail_tab_indentation() {}

    #[test]
    fn test_pass_indented_joins_default() {
        let pass_str = "
SELECT a, b, c
FROM my_tbl
LEFT JOIN another_tbl USING(a)"
            .trim_start();

        let violations = lint(pass_str.into(), "ansi".into(), rules(), None, None).unwrap();
        assert_eq!(violations, []);
    }

    #[test]
    fn test_pass_trailing_comment_1() {
        let pass_str = "
select
    bar
    -- comment
from foo";

        let violations = lint(pass_str.into(), "ansi".into(), rules(), None, None).unwrap();
        assert_eq!(violations, []);
    }

    #[test]
    fn test_pass_trailing_comment_2() {
        let pass_str = "
select
    bar
    -- comment
from foo";

        let violations = lint(pass_str.into(), "ansi".into(), rules(), None, None).unwrap();
        assert_eq!(violations, []);
    }

    #[test]
    fn test_pass_issue_4582() {
        let pass_str = "
select
    bar
    -- comment
from foo";

        let violations = lint(pass_str.into(), "ansi".into(), rules(), None, None).unwrap();
        assert_eq!(violations, []);
    }

    // LT02-tab-space.yml

    #[test]
    fn spaces_pass_default() {
        let violations = lint("SELECT\n    1".into(), "ansi".into(), rules(), None, None).unwrap();
        assert_eq!(violations, []);
    }

    #[test]
    fn tabs_fail_default() {
        let fixed = fix("SELECT\n\t\t1\n".into(), rules());
        assert_eq!(fixed, "SELECT\n    1\n");
    }

    #[test]
    fn indented_comments() {
        let pass_str = "
SELECT
    a,         -- Some comment
    longer_col -- A lined up comment
FROM spam";

        let violations = lint(pass_str.into(), "ansi".into(), rules(), None, None).unwrap();
        assert_eq!(violations, []);
    }

    #[test]
    fn indented_comments_default_config() {
        let fail_str = "
SELECT
	a,			-- Some comment
	longer_col	-- A lined up comment
FROM spam";
        let fix_str = "\nSELECT\n    a,\t\t\t-- Some comment\n    longer_col\t-- A lined up \
                       comment\nFROM spam";

        assert_eq!(fix(fail_str.into(), rules()), fix_str);
    }
}