-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
method_name_equal.rb
51 lines (43 loc) · 1.27 KB
/
method_name_equal.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
44
45
46
47
48
49
50
51
# frozen_string_literal: true
module RuboCop
module Cop
module InternalAffairs
# Checks that method names are checked using `method?` method.
#
# @example
# # bad
# node.method_name == :do_something
#
# # good
# node.method?(:do_something)
#
class MethodNameEqual < Base
include RangeHelp
extend AutoCorrector
MSG = 'Use `method?(%<method_name>s)` instead of ' \
'`method_name == %<method_name>s`.'
def_node_matcher :method_name?, <<~PATTERN
(send
$(send
(...) :method_name) :==
$...)
PATTERN
def on_send(node)
method_name?(node) do |method_name_node, method_name_arg|
message = format(MSG, method_name: method_name_arg.first.source)
range = range(method_name_node, node)
add_offense(range, message: message) do |corrector|
corrector.replace(range, "method?(#{method_name_arg.first.source})")
end
end
end
private
def range(method_name_node, node)
range_between(
method_name_node.loc.selector.begin_pos, node.source_range.end_pos
)
end
end
end
end
end