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 arguments |a, e| (accumulator, element).'
ARGS_NODE =
s(:args, s(:arg, :a), s(:arg, :e))

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

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
# 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)

  unless args_node == ARGS_NODE
    add_offence(:convention, node.loc.expression, MSG)
  end
end