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
use crate::cop;
use crate::cop::register_node_handler;
use crate::source;
use crate::types;

const MSG_READER: &str = "Do not prefix reader method names with `get_`.";
const MSG_WRITER: &str = "Do not prefix writer method names with `set_`.";

static COP_NAME: &str = "Naming/AccessorMethodName";

pub fn init() {
    register_node_handler("def", COP_NAME, on_def);
    cop::register(COP_NAME);
}

pub fn on_def(node: &types::Node, file: &source::File) {
    if let types::Node::Def(node) = node {
        if node.name.starts_with("get_") {
            if None == node.args {
                file.add_offense(COP_NAME, node.name_l, MSG_READER);
            }
        } else if node.name.starts_with("set_") {
            if let Some(args) = &node.args {
                if let types::Node::Args(args) = &**args {
                    if args.args.len() == 1 {
                        file.add_offense(COP_NAME, node.name_l, MSG_WRITER);
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_detects_get_attribute() {
        crate::expect_offense!(
            "
            def get_attribute
            end
        "
        );
    }

    #[test]
    fn it_detects_set_attribute() {
        crate::expect_offense!(
            "
            def set_attribute(aa)
            end
        "
        );
    }

    #[test]
    fn it_works_fine_with_other_method_names() {
        crate::expect_no_offense!(
            "
            def set_name
            end
        "
        );
    }
}