Sha256: d53bac25329a2800604ad263f50b34778a8ab3806132b50d05ebef27fea718e6

Contents?: true

Size: 1.51 KB

Versions: 8

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Sorbet
      # 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
      class KeywordArgumentOrdering < ::RuboCop::Cop::Cop # rubocop:todo InternalAffairs/InheritDeprecatedCopClass
        include SignatureHelp

        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

        def check_order_for_kwoptargs(parameters)
          out_of_kwoptarg = false

          parameters.reverse.each do |param|
            out_of_kwoptarg = true unless param.kwoptarg_type? || param.blockarg_type? || param.kwrestarg_type?

            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
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-sorbet-0.8.3 lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb
rubocop-sorbet-0.8.2 lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb
rubocop-sorbet-0.8.1 lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb
rubocop-sorbet-0.8.0 lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb
rubocop-sorbet-0.7.8 lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb
rubocop-sorbet-0.7.7 lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb
rubocop-sorbet-0.7.6 lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb
rubocop-sorbet-0.7.5 lib/rubocop/cop/sorbet/signatures/keyword_argument_ordering.rb