Class: RuboCop::Cop::Performance::Detect
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/detect.rb |
Overview
Identifies usages of first
, last
, [0]
or [-1]
chained to select
, find_all
or filter
and change them to use
detect
instead.
Constant Summary
-
CANDIDATE_METHODS =
# File 'lib/rubocop/cop/performance/detect.rb', line 33Set[:select, :find_all, :filter].freeze
-
INDEX_MSG =
# File 'lib/rubocop/cop/performance/detect.rb', line 37'Use `%<prefer>s` instead of `%<first_method>s[%<index>i]`.'
-
INDEX_REVERSE_MSG =
# File 'lib/rubocop/cop/performance/detect.rb', line 38'Use `reverse.%<prefer>s` instead of `%<first_method>s[%<index>i]`.'
-
MSG =
# File 'lib/rubocop/cop/performance/detect.rb', line 35'Use `%<prefer>s` instead of `%<first_method>s.%<second_method>s`.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/detect.rb', line 39%i[first last []].freeze
-
REVERSE_MSG =
# File 'lib/rubocop/cop/performance/detect.rb', line 36'Use `reverse.%<prefer>s` instead of `%<first_method>s.%<second_method>s`.'
Instance Method Summary
-
#on_csend(node)
Alias for #on_send.
- #on_send(node) (also: #on_csend)
- #accept_first_call?(receiver, body) ⇒ Boolean private
- #autocorrect(corrector, node, replacement) private
- #lazy?(node) ⇒ Boolean private
- #message_for_method(method, index) private
- #preferred_method private
- #register_offense(node, receiver, second_method, index) private
- #replacement(method, index) private
Instance Method Details
#accept_first_call?(receiver, body) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/performance/detect.rb', line 70
def accept_first_call?(receiver, body) caller, _first_method, args = *receiver # check that we have usual block or block pass return true if body.nil? && (args.nil? || !args.block_pass_type?) lazy?(caller) end
#autocorrect(corrector, node, replacement) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/detect.rb', line 102
def autocorrect(corrector, node, replacement) receiver, _first_method = *node first_range = receiver.source_range.end.join(node.loc.selector) receiver, _args, _body = *receiver if receiver.block_type? corrector.remove(first_range) corrector.replace(receiver.loc.selector, replacement) end
#lazy?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/performance/detect.rb', line 128
def lazy?(node) return false unless node receiver, method, _args = *node method == :lazy && !receiver.nil? end
#message_for_method(method, index) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/detect.rb', line 113
def (method, index) case method when :[] index == -1 ? INDEX_REVERSE_MSG : INDEX_MSG when :last REVERSE_MSG else MSG end end
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/performance/detect.rb', line 66
alias on_csend on_send
#on_send(node) Also known as: #on_csend
[ GitHub ]# File 'lib/rubocop/cop/performance/detect.rb', line 50
def on_send(node) detect_candidate?(node) do |receiver, second_method, args| if second_method == :[] index = args args = {} end return unless args.empty? return unless receiver receiver, _args, body = *receiver if receiver.block_type? return if accept_first_call?(receiver, body) register_offense(node, receiver, second_method, index) end end
#preferred_method (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/detect.rb', line 124
def preferred_method config.for_cop('Style/CollectionMethods')['PreferredMethods']['detect'] || 'detect' end
#register_offense(node, receiver, second_method, index) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/detect.rb', line 79
def register_offense(node, receiver, second_method, index) _caller, first_method, _args = *receiver range = receiver.loc.selector.join(node.loc.selector) = (second_method, index) = format(, prefer: preferred_method, first_method: first_method, second_method: second_method, index: index) add_offense(range, message: ) do |corrector| autocorrect(corrector, node, replacement(second_method, index)) end end
#replacement(method, index) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/detect.rb', line 94
def replacement(method, index) if method == :last || (method == :[] && index == -1) "reverse.#{preferred_method}" else preferred_method end end