forked from rubocop/rubocop
/
binary_operator_parameter_name.rb
43 lines (36 loc) · 1.07 KB
/
binary_operator_parameter_name.rb
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
# frozen_string_literal: true
module RuboCop
module Cop
module Naming
# This cop makes sure that certain binary operator methods have their
# sole parameter named `other`.
#
# @example
#
# # bad
# def +(amount); end
#
# # good
# def +(other); end
class BinaryOperatorParameterName < Base
MSG = 'When defining the `%<opr>s` operator, ' \
'name its argument `other`.'
OP_LIKE_METHODS = %i[eql? equal?].freeze
EXCLUDED = %i[+@ -@ [] []= << === ` =~].freeze
def_node_matcher :op_method_candidate?, <<~PATTERN
(def [#op_method? $_] (args $(arg [!:other !:_other])) _)
PATTERN
def on_def(node)
op_method_candidate?(node) do |name, arg|
add_offense(arg, message: format(MSG, opr: name))
end
end
private
def op_method?(name)
return false if EXCLUDED.include?(name)
!/\A[[:word:]]/.match?(name) || OP_LIKE_METHODS.include?(name)
end
end
end
end
end