class RuboCop::Cop::Style::KeywordParametersOrder
end
# body omitted
do_something do |second:, first: false, third: 10|
# good
end
# body omitted
do_something do |first: false, second:, third: 10|
# bad
end
# body omitted
def some_method(second:, first: false, third: 10)
# good
end
# body omitted
def some_method(first: false, second:, third: 10)
# bad
@example
and optional parameters at the end.
it is expected to find required parameters at the beginning of parameters list
This improves readability, because when looking through the source,
end of the parameters list.
Enforces that optional keyword parameters are placed at the
def append_newline_to_last_kwoptarg(arguments, corrector)
def append_newline_to_last_kwoptarg(arguments, corrector) last_argument = arguments.last return if last_argument.kwrestarg_type? || last_argument.blockarg_type? last_kwoptarg = arguments.reverse.find(&:kwoptarg_type?) corrector.insert_after(last_kwoptarg, "\n") unless arguments.parent.block_type? end
def on_kwoptarg(node)
def on_kwoptarg(node) kwarg_nodes = node.right_siblings.select(&:kwarg_type?) return if kwarg_nodes.empty? add_offense(node) do |corrector| if node.parent.find(&:kwoptarg_type?) == node corrector.insert_before(node, "#{kwarg_nodes.map(&:source).join(', ')}, ") arguments = node.each_ancestor(:def, :defs, :block).first.arguments append_newline_to_last_kwoptarg(arguments, corrector) unless parentheses?(arguments) remove_kwargs(kwarg_nodes, corrector) end end end
def remove_kwargs(kwarg_nodes, corrector)
def remove_kwargs(kwarg_nodes, corrector) kwarg_nodes.each do |kwarg| with_space = range_with_surrounding_space(kwarg.source_range) corrector.remove(range_with_surrounding_comma(with_space, :left)) end end