Class: Rubocop::Cop::Style::CollectionMethods

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/collection_methods.rb

Overview

This cop checks for uses of unidiomatic method names from the Enumerable module.

The current definition of the check is flawed and should be enhanced by check for by blocks & procs as arguments of the methods.

Constant Summary collapse

MSG =
'Prefer %s over %s.'
PREFERRED_METHODS =
{
  collect: 'map',
  inject: 'reduce',
  detect: 'find',
  find_all: 'select'
}

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, #autocorrect_action, cop_name, #do_autocorrect, #ignore_node, inherited, #initialize, #inspect, #name, rails?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_send(node) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubocop/cop/style/collection_methods.rb', line 22

def on_send(node)
  receiver, method_name, *_args = *node

  # a simple(but flawed way) to reduce false positives
  if receiver && PREFERRED_METHODS[method_name]
    add_offence(
      :convention,
      node.loc.selector,
      sprintf(MSG, PREFERRED_METHODS[method_name], method_name)
    )
  end
end