-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_first.rb
74 lines (63 loc) · 1.89 KB
/
reverse_first.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
68
69
70
71
72
73
74
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# Identifies places where `reverse.first(n)` and `reverse.first`
# can be replaced by `last(n).reverse` and `last`.
#
# @example
#
# # bad
# array.reverse.first(5)
# array.reverse.first
#
# # good
# array.last(5).reverse
# array.last
#
class ReverseFirst < Base
include RangeHelp
extend AutoCorrector
MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
RESTRICT_ON_SEND = %i[first].freeze
def_node_matcher :reverse_first_candidate?, <<~PATTERN
(send $(call _ :reverse) :first (int _)?)
PATTERN
def on_send(node)
reverse_first_candidate?(node) do |receiver|
range = correction_range(receiver, node)
message = build_message(node)
add_offense(range, message: message) do |corrector|
replacement = build_good_method(node)
corrector.replace(range, replacement)
end
end
end
alias on_csend on_send
private
def correction_range(receiver, node)
range_between(receiver.loc.selector.begin_pos, node.source_range.end_pos)
end
def build_message(node)
good_method = build_good_method(node)
bad_method = build_bad_method(node)
format(MSG, good_method: good_method, bad_method: bad_method)
end
def build_good_method(node)
if node.arguments?
"last(#{node.arguments.first.source}).reverse"
else
'last'
end
end
def build_bad_method(node)
if node.arguments?
"reverse.first(#{node.arguments.first.source})"
else
'reverse.first'
end
end
end
end
end
end