class RuboCop::Cop::Style::EachWithObject

Looks for inject / reduce calls where the passed in object is returned at the end and so could be replaced by each_with_object without the need to return the object at the end.

However, we can’t replace with each_with_object if the accumulator parameter is assigned to within the block.

@example

# bad
[1, 2].inject({}) { |a, e| a[e] = e; a }

# good
[1, 2].each_with_object({}) { |e, a| a[e] = e }

Constants

METHODS
MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 26
def on_block(node)
  each_with_object_block_candidate?(node) do |method, args, body|
    _, method_name, method_arg = *method
    return if simple_method_arg?(method_arg)

    return_value = return_value(body)
    return unless return_value
    return unless first_argument_returned?(args, return_value)
    return if accumulator_param_assigned_to?(body, args)

    message = format(MSG, method: method_name)
    add_offense(method.loc.selector, message: message) do |corrector|
      autocorrect_block(corrector, node, return_value)
    end
  end
end
on_numblock(node) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 43
def on_numblock(node)
  each_with_object_numblock_candidate?(node) do |method, body|
    _, method_name, method_arg = *method
    return if simple_method_arg?(method_arg)

    return unless return_value(body)&.source == '_1'

    message = format(MSG, method: method_name)
    add_offense(method.loc.selector, message: message) do |corrector|
      autocorrect_numblock(corrector, node)
    end
  end
end

Private Instance Methods

accumulator_param_assigned_to?(body, args) click to toggle source

if the accumulator parameter is assigned to in the block, then we can’t convert to each_with_object

# File lib/rubocop/cop/style/each_with_object.rb, line 102
def accumulator_param_assigned_to?(body, args)
  first_arg, = *args
  accumulator_var, = *first_arg

  body.each_descendant.any? do |n|
    next unless n.assignment?

    lhs, _rhs = *n
    lhs.equal?(accumulator_var)
  end
end
autocorrect_block(corrector, node, return_value) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 69
def autocorrect_block(corrector, node, return_value)
  corrector.replace(node.send_node.loc.selector, 'each_with_object')

  first_arg, second_arg = *node.arguments

  corrector.replace(first_arg, second_arg.source)
  corrector.replace(second_arg, first_arg.source)

  if return_value_occupies_whole_line?(return_value)
    corrector.remove(whole_line_expression(return_value))
  else
    corrector.remove(return_value)
  end
end
autocorrect_numblock(corrector, node) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 84
def autocorrect_numblock(corrector, node)
  corrector.replace(node.send_node.loc.selector, 'each_with_object')

  # We don't remove the return value to avoid a clobbering error.
  node.body.each_descendant do |var|
    next unless var.lvar_type?

    corrector.replace(var, '_2') if var.source == '_1'
    corrector.replace(var, '_1') if var.source == '_2'
  end
end
first_argument_returned?(args, return_value) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 121
def first_argument_returned?(args, return_value)
  first_arg, = *args
  accumulator_var, = *first_arg
  return_var, = *return_value

  accumulator_var == return_var
end
return_value(body) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 114
def return_value(body)
  return unless body

  return_value = body.begin_type? ? body.children.last : body
  return_value if return_value&.lvar_type?
end
return_value_occupies_whole_line?(node) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 129
def return_value_occupies_whole_line?(node)
  whole_line_expression(node).source.strip == node.source
end
simple_method_arg?(method_arg) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 96
def simple_method_arg?(method_arg)
  method_arg&.basic_literal?
end
whole_line_expression(node) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 133
def whole_line_expression(node)
  range_by_whole_lines(node.loc.expression, include_final_newline: true)
end