class RuboCop::Cop::Sorbet::KeywordArgumentOrdering

This cop checks for the ordering of keyword arguments required by sorbet-runtime. The ordering requires that all keyword arguments are at the end of the parameters list, and all keyword arguments with a default value must be after those without default values.

@example

# bad
sig { params(a: Integer, b: String).void }
def foo(a: 1, b:); end

# good
sig { params(b: String, a: Integer).void }
def foo(b:, a: 1); end

Public Instance Methods

on_signature(node) click to toggle source
# File lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb, line 24
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

Private Instance Methods

check_order_for_kwoptargs(parameters) click to toggle source
# File lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb, line 34
def check_order_for_kwoptargs(parameters)
  out_of_kwoptarg = false

  parameters.reverse.each do |param|
    out_of_kwoptarg = true unless param.type == :kwoptarg || param.type == :blockarg || param.type == :kwrestarg

    next unless param.type == :kwoptarg && out_of_kwoptarg

    add_offense(
      param,
      message: 'Optional keyword arguments must be at the end of the parameter list.'
    )
  end
end