Class: RuboCop::Cop::Sevencop::MethodDefinitionKeywordArgumentOrdered

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/sevencop/method_definition_keyword_argument_ordered.rb

Overview

Sort method definition keyword arguments in alphabetical order.

Examples:

# bad
def foo(b:, a:); end

# good
def foo(a:, b:); end

# bad
def foo(c:, d:, b: 1, a: 2); end

# good
def foo(c:, d:, a: 2, b: 1); end

Constant Summary collapse

MSG =
'Sort method definition keyword arguments in alphabetical order.'

Instance Method Summary collapse

Instance Method Details

#on_kwarg(node) ⇒ Object Also known as: on_kwoptarg

Parameters:

  • node (RuboCop::AST::ArgNode)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/sevencop/method_definition_keyword_argument_ordered.rb', line 26

def on_kwarg(node)
  previous_older_kwarg = find_previous_older_sibling(node)
  return unless previous_older_kwarg

  add_offense(node) do |corrector|
    corrector.insert_before(
      previous_older_kwarg,
      "#{node.source}, "
    )
    corrector.remove(
      node.source_range.with(
        begin_pos: node.left_sibling.source_range.end_pos
      )
    )
  end
end