class RuboCop::Cop::Lint::RedundantSplatExpansion


do_something(*%w[foo bar baz])
# bad
@example AllowPercentLiteralArrayArgument: false
do_something(*%w[foo bar baz])
# good
@example AllowPercentLiteralArrayArgument: true (default)
end
baz
else
bar
when 1, 2, 3
case foo
# good
end
baz
else
bar
when *[1, 2, 3]
case foo
# bad
end
bar
rescue StandardError, ApplicationError
foo
begin
# good
end
bar
rescue *[StandardError, ApplicationError]
foo
begin
# bad
do_something(‘foo’, ‘bar’, ‘baz’)
# good
do_something(*[‘foo’, ‘bar’, ‘baz’])
# bad
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’]
a = [‘a’]
a = *1..10
a, *b = *c
a, b = *c
a = *c
c = [1, 2, 3]
# good
[‘a’, ‘b’, *%w(c d e), ‘f’, ‘g’]
a = *1
a = *‘a’
a = *[1, 2, 3]
# bad
@example
Checks for unneeded usages of splat expansion.

def allow_percent_literal_array_argument?

def allow_percent_literal_array_argument?
  cop_config.fetch('AllowPercentLiteralArrayArgument', true)
end

def array_new_inside_array_literal?(array_new_node)

def array_new_inside_array_literal?(array_new_node)
  return false unless array_new?(array_new_node)
  grandparent = array_new_node.parent.parent
  grandparent.array_type? && grandparent.children.size > 1
end

def array_splat?(node)

def array_splat?(node)
  node.children.first.array_type?
end

def autocorrect(corrector, node)

def autocorrect(corrector, node)
  range, content = replacement_range_and_content(node)
  corrector.replace(range, content)
end

def method_argument?(node)

def method_argument?(node)
  node.parent.call_type?
end

def on_splat(node)

def on_splat(node)
  redundant_splat_expansion(node) do
    if array_splat?(node) && (method_argument?(node) || part_of_an_array?(node))
      return if allow_percent_literal_array_argument? &&
                use_percent_literal_array_argument?(node)
      add_offense(node, message: ARRAY_PARAM_MSG) do |corrector|
        autocorrect(corrector, node)
      end
    else
      add_offense(node) { |corrector| autocorrect(corrector, node) }
    end
  end
end

def part_of_an_array?(node)

def part_of_an_array?(node)
  # The parent of a splat expansion is an array that does not have
  # `begin` or `end`
  parent = node.parent
  parent.array_type? && parent.loc.begin && parent.loc.end
end

def redundant_brackets?(node)

def redundant_brackets?(node)
  parent = node.parent
  grandparent = node.parent.parent
  parent.when_type? || method_argument?(node) || part_of_an_array?(node) ||
    grandparent&.resbody_type?
end

def redundant_splat_expansion(node)

def redundant_splat_expansion(node)
  literal_expansion(node) do |expanded_item|
    if expanded_item.send_type?
      return if array_new_inside_array_literal?(expanded_item)
      grandparent = node.parent.parent
      return if grandparent && !ASSIGNMENT_TYPES.include?(grandparent.type)
    end
    yield
  end
end

def remove_brackets(array)

def remove_brackets(array)
  array_start = array.loc.begin.source
  elements = *array
  elements = elements.map(&:source)
  if array_start.start_with?(PERCENT_W)
    "'#{elements.join("', '")}'"
  elsif array_start.start_with?(PERCENT_CAPITAL_W)
    %("#{elements.join('", "')}")
  elsif array_start.start_with?(PERCENT_I)
    ":#{elements.join(', :')}"
  elsif array_start.start_with?(PERCENT_CAPITAL_I)
    %(:"#{elements.join('", :"')}")
  else
    elements.join(', ')
  end
end

def replacement_range_and_content(node)

rubocop:disable Metrics/AbcSize
def replacement_range_and_content(node)
  variable = node.children.first
  expression = node.source_range
  if array_new?(variable)
    expression = node.parent.source_range if node.parent.array_type?
    [expression, variable.source]
  elsif !variable.array_type?
    [expression, "[#{variable.source}]"]
  elsif redundant_brackets?(node)
    [expression, remove_brackets(variable)]
  else
    [node.loc.operator, '']
  end
end

def use_percent_literal_array_argument?(node)

def use_percent_literal_array_argument?(node)
  argument = node.children.first
  method_argument?(node) &&
    (argument.percent_literal?(:string) || argument.percent_literal?(:symbol))
end