Class: RuboCop::Cop::Yoshiki::Layout::HangingKeywordArguments

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
AllowedMethods, ReparsedEquivalence, KeywordOnlyArguments
Defined in:
lib/rubocop/cop/yoshiki/layout/hanging_keyword_arguments.rb

Overview

Prefers multiline keyword-only method calls in hanging form: first keyword on the same line as the method, continuations aligned under the first keyword.

Ordinary kwargs omit parentheses. Hash value omission (key:) keeps parentheses (see Yoshiki/Lint/ParenthesizeValueOmission); this cop only hangs inside existing parens for those calls.

Key order is Yoshiki/Style/SortedKeywordArguments. Padding is Layout/HashAlignment.

Examples:

# bad
annotations(
  read_only_hint: true,
  destructive_hint: false
)

# good
annotations read_only_hint: true,
            destructive_hint: false

Hash value omission (parentheses required; hang inside them)

# bad
annotations(
  destructive_hint:,
  read_only_hint:
)

# good
annotations(destructive_hint:,
            read_only_hint:)

Constant Summary collapse

MSG_OMIT_PARENS =
[
  'Use hanging keyword arguments ',
  '(first kwarg on the method line, no parentheses).'
].join.freeze
MSG_KEEP_PARENS =
[
  'Use hanging keyword arguments with parentheses ',
  'when using hash value omission ',
  '(first kwarg on the method line).'
].join.freeze

Instance Method Summary collapse

Instance Method Details

#on_investigation_endObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/yoshiki/layout/hanging_keyword_arguments.rb', line 61

def on_investigation_end
  verified_by_reparse(@pending || []).each do |node|
    add_offense(offense_range(node), message: message_for(node)) do |corrector|
      autocorrect(corrector, node)
    end
  end
  @pending = []

  super
end

#on_send(node) ⇒ Object Also known as: on_csend



54
55
56
57
58
# File 'lib/rubocop/cop/yoshiki/layout/hanging_keyword_arguments.rb', line 54

def on_send node
  return unless eligible?(node)

  (@pending ||= []) << node
end