forked from rubocop/rubocop-performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaller.rb
67 lines (57 loc) · 1.76 KB
/
caller.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# Identifies places where `caller[n]` can be replaced by `caller(n..n).first`.
#
# @example
# # bad
# caller[1]
# caller.first
# caller_locations[1]
# caller_locations.first
#
# # good
# caller(2..2).first
# caller(1..1).first
# caller_locations(2..2).first
# caller_locations(1..1).first
class Caller < Base
extend AutoCorrector
MSG = 'Use `%<preferred_method>s` instead of `%<current_method>s`.'
RESTRICT_ON_SEND = %i[first []].freeze
def_node_matcher :slow_caller?, <<~PATTERN
{
(send nil? {:caller :caller_locations})
(send nil? {:caller :caller_locations} int)
}
PATTERN
def_node_matcher :caller_with_scope_method?, <<~PATTERN
{
(send #slow_caller? :first)
(send #slow_caller? :[] int)
}
PATTERN
def on_send(node)
return unless caller_with_scope_method?(node)
method_name = node.receiver.method_name
caller_arg = node.receiver.first_argument
n = caller_arg ? int_value(caller_arg) : 1
if node.method?(:[])
m = int_value(node.first_argument)
n += m
end
preferred_method = "#{method_name}(#{n}..#{n}).first"
message = format(MSG, preferred_method: preferred_method, current_method: node.source)
add_offense(node, message: message) do |corrector|
corrector.replace(node, preferred_method)
end
end
private
def int_value(node)
node.children[0]
end
end
end
end
end