class RuboCop::Cop::Sorbet::KeywordArgumentOrdering
def foo(b:, a: 1); end
sig { params(b: String, a: Integer).void }
# good
def foo(a: 1, b:); end
sig { params(a: Integer, b: String).void }
# bad
@example
with a default value must be after those without default values.
are at the end of the parameters list, and all keyword arguments
sorbet-runtime. The ordering requires that all keyword arguments
Checks for the ordering of keyword arguments required by
def check_order_for_kwoptargs(parameters)
def check_order_for_kwoptargs(parameters) out_of_kwoptarg = false parameters.reverse.each do |param| out_of_kwoptarg = true unless param.type?(:kwoptarg, :blockarg, :kwrestarg) next unless param.kwoptarg_type? && out_of_kwoptarg add_offense( param, message: "Optional keyword arguments must be at the end of the parameter list.", ) end end
def on_signature(node)
def on_signature(node) method_node = node.parent.children[node.sibling_index + 1] return if method_node.nil? method_parameters = method_node.arguments check_order_for_kwoptargs(method_parameters) end