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
use crate::config::ConfigOption;
use crate::linter::{SyntaxRule, SyntaxRuleResult};
use sv_parser::{NodeEvent, RefNode, SyntaxTree};

pub struct StyleOperatorBooleanLeadingSpace {
    /// Number of trailing spaces of the previous node
    prev_trailing_space: usize,

    /// True if the previous node is an `Expression`
    prev_is_expr: bool,
}

impl SyntaxRule for StyleOperatorBooleanLeadingSpace {
    fn check(
        &mut self,
        syntax_tree: &SyntaxTree,
        event: &NodeEvent,
        _option: &ConfigOption,
    ) -> SyntaxRuleResult {
        match event {
            NodeEvent::Enter(node) => {
                if !self.prev_is_expr {
                    return SyntaxRuleResult::Pass;
                }

                match node {
                    RefNode::BinaryOperator(x) => {
                        let t = syntax_tree.get_str_trim(*x).unwrap();

                        match t {
                            "==" | "!=" | "<" | ">" | "<=" | ">=" | "&&" | "||" | "===" | "==?"
                            | "!==" | "!=?" | "->" | "<->" => {
                                if self.prev_trailing_space == 1 {
                                    SyntaxRuleResult::Pass
                                } else {
                                    SyntaxRuleResult::Fail
                                }
                            }
                            _ => SyntaxRuleResult::Pass,
                        }
                    }
                    _ => SyntaxRuleResult::Pass,
                }
            }
            NodeEvent::Leave(node) => {
                match node {
                    RefNode::Expression(expr) => {
                        self.prev_is_expr = true;
                        let trailing_space =
                            count_trailing_space(syntax_tree.get_str(*expr).unwrap());
                        self.prev_trailing_space = trailing_space;
                    }
                    RefNode::ConstantExpression(expr) => {
                        self.prev_is_expr = true;
                        let trailing_space =
                            count_trailing_space(syntax_tree.get_str(*expr).unwrap());
                        self.prev_trailing_space = trailing_space;
                    }
                    _ => {
                        self.prev_is_expr = false;
                        self.prev_trailing_space = 0;
                    }
                }
                SyntaxRuleResult::Pass
            }
        }
    }

    fn name(&self) -> String {
        String::from("style_operator_boolean_leading_space")
    }

    fn hint(&self, _option: &ConfigOption) -> String {
        String::from(format!("Put exactly one space before binary boolean operators."))
    }

    fn reason(&self) -> String {
        String::from("Consistent use of whitespace enhances readability by reducing visual noise.")
    }
}

fn count_trailing_space(s: &str) -> usize {
    let mut count = 0;
    for c in s.chars().rev() {
        if c == ' ' {
            count += 1;
        } else {
            break;
        }
    }
    count
}

impl std::default::Default for StyleOperatorBooleanLeadingSpace {
    fn default() -> Self {
        Self {
            prev_trailing_space: 0,
            prev_is_expr: false,
        }
    }
}