/
binary_operator_parameter_name.cr
50 lines (46 loc) · 1.18 KB
/
binary_operator_parameter_name.cr
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
module Ameba::Rule::Naming
# A rule that enforces that certain binary operator methods have
# their sole parameter named `other`.
#
# For example, this is considered valid:
#
# ```
# class Money
# def +(other)
# end
# end
# ```
#
# And this is invalid parameter name:
#
# ```
# class Money
# def +(amount)
# end
# end
# ```
#
# YAML configuration example:
#
# ```
# Naming/BinaryOperatorParameterName:
# Enabled: true
# ExcludedOperators: ["[]", "[]?", "[]=", "<<", ">>", "=~", "!~"]
# ```
class BinaryOperatorParameterName < Base
properties do
description "Enforces that certain binary operator methods have " \
"their sole parameter named `other`"
excluded_operators %w[[] []? []= << >> ` =~ !~]
end
MSG = "When defining the `%s` operator, name its argument `other`"
def test(source, node : Crystal::Def)
name = node.name
return if name == "->" || name.in?(excluded_operators)
return if name.chars.any?(&.alphanumeric?)
return unless node.args.size == 1
return if (arg = node.args.first).name == "other"
issue_for arg, MSG % name
end
end
end