Class: RuboCop::Cop::Performance::ReverseFirst
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
RangeHelp,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/reverse_first.rb |
Overview
Identifies places where reverse.first(n)
and reverse.first
can be replaced by last(n).reverse
and last
.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/performance/reverse_first.rb', line 23'Use `%<good_method>s` instead of `%<bad_method>s`.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/reverse_first.rb', line 24%i[first].freeze
Instance Method Summary
-
#on_csend(node)
Alias for #on_send.
- #on_send(node) (also: #on_csend)
- #build_good_method(node) private
- #build_message(node, range) private
- #correction_range(receiver, node) private
Instance Method Details
#build_good_method(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/reverse_first.rb', line 56
def build_good_method(node) if node.arguments? "last(#{node.first_argument.source})#{node.loc.dot.source}reverse" else 'last' end end
#build_message(node, range) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/reverse_first.rb', line 50
def (node, range) good_method = build_good_method(node) bad_method = range.source format(MSG, good_method: good_method, bad_method: bad_method) end
#correction_range(receiver, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/reverse_first.rb', line 46
def correction_range(receiver, node) range_between(receiver.loc.selector.begin_pos, node.source_range.end_pos) end
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/performance/reverse_first.rb', line 42
alias on_csend on_send
#on_send(node) Also known as: #on_csend
[ GitHub ]# File 'lib/rubocop/cop/performance/reverse_first.rb', line 30
def on_send(node) reverse_first_candidate?(node) do |receiver| range = correction_range(receiver, node) = (node, range) add_offense(range, message: ) do |corrector| replacement = build_good_method(node) corrector.replace(range, replacement) end end end