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

#[derive(Default)]
pub struct StyleOperatorUnary {
    re_succ: Option<Regex>,
}

impl SyntaxRule for StyleOperatorUnary {
    fn check(
        &mut self,
        syntax_tree: &SyntaxTree,
        event: &NodeEvent,
        _option: &ConfigOption,
    ) -> SyntaxRuleResult {
        /*
        re_succ matches the unary symbol, then what is allowed after the
        operator symbol.
            - nothing, immediately followed by a symbol or identifier.
        */
        if self.re_succ.is_none() {
            self.re_succ = Some(Regex::new(r"^\S+$").unwrap());
        }

        let node = match event {
            NodeEvent::Enter(x) => x,
            NodeEvent::Leave(_) => {
                return SyntaxRuleResult::Pass;
            }
        };

        let s: Option<&str> = match node {
            RefNode::UnaryOperator(x) => {
                Some(syntax_tree.get_str(*x).unwrap())
            }
            RefNode::UnaryModulePathOperator(x) => {
                Some(syntax_tree.get_str(*x).unwrap())
            }
            RefNode::IncOrDecOperator(x) => {
                Some(syntax_tree.get_str(*x).unwrap())
            }
            _ => None,
        };

        if let Some(t) = s {
            let re_succ = self.re_succ.as_ref().unwrap();
            if re_succ.is_match(t) {
                SyntaxRuleResult::Pass
            } else {
                SyntaxRuleResult::Fail
            }
        } else {
            SyntaxRuleResult::Pass
        }
    }

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

    fn hint(&self, _option: &ConfigOption) -> String {
        String::from("Remove all whitespace following the operator.")
    }

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