class RuboCop::Cop::Style::KeywordArgumentsMerging
some_method(**opts, **other_opts)
some_method(**opts, foo: true)
# good
some_method(**opts.merge(other_opts))
some_method(**opts.merge(foo: true))
# bad
@example
also leads to shorter and simpler code.
Providing arguments directly is more performant than using ‘merge`, and
directly rather than using `merge`.
When passing an existing hash as keyword arguments, provide additional arguments
def autocorrect(corrector, kwsplat_node, hash_node, other_hash_node)
def autocorrect(corrector, kwsplat_node, hash_node, other_hash_node) other_hash_node_replacement = other_hash_node.map do |node| if node.hash_type? if node.braces? node.source[1...-1] else node.source end else "**#{node.source}" end end.join(', ') corrector.replace(kwsplat_node, "**#{hash_node.source}, #{other_hash_node_replacement}") end
def on_kwsplat(node)
def on_kwsplat(node) return unless (ancestor = node.parent&.parent) merge_kwargs?(ancestor) do |merge_node, hash_node, other_hash_node| add_offense(merge_node) do |corrector| autocorrect(corrector, node, hash_node, other_hash_node) end end end