Class: Rubocop::Cop::Style::ReduceArguments

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

Overview

This cop checks whether the block arguments of a single-line reduce(inject) call are named a(for accumulator) and e (for element).

Constant Summary collapse

MSG =
'Name reduce block arguments |a, e| (accumulator, element).'
ARGS_NODE =
s(:args, s(:arg, :a), s(:arg, :e))

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

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

Instance Method Details

#on_block(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubocop/cop/style/reduce_arguments.rb', line 14

def on_block(node)
  # we care only for single line blocks
  return unless Util.block_length(node) == 0

  method_node, args_node, _body_node = *node
  receiver, method_name, _method_args = *method_node

  # discard other scenarios
  return unless receiver
  return unless [:reduce, :inject].include?(method_name)

  # discard second argument destructuring
  _, element_node = *args_node
  return unless element_node.type == :arg

  convention(args_node, :expression) unless args_node == ARGS_NODE
end