Class: Rubocop::Cop::ReduceArguments

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

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

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #inspect, #name, #on_comment

Constructor Details

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

Instance Method Details

#on_block(node) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubocop/cop/reduce_arguments.rb', line 10

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.line, MSG)
  end

  super
end