class RuboCop::Cop::Style::TrailingCommaInArguments

This cop checks for trailing comma in argument lists.

@example EnforcedStyleForMultiline: consistent_comma

# bad
method(1, 2,)

# good
method(1, 2)

# good
method(
  1, 2,
  3,
)

# good
method(
  1,
  2,
)

@example EnforcedStyleForMultiline: comma

# bad
method(1, 2,)

# good
method(1, 2)

# good
method(
  1,
  2,
)

@example EnforcedStyleForMultiline: no_comma (default)

# bad
method(1, 2,)

# good
method(1, 2)

# good
method(
  1,
  2
)

Public Class Methods

autocorrect_incompatible_with() click to toggle source
# File lib/rubocop/cop/style/trailing_comma_in_arguments.rb, line 68
def self.autocorrect_incompatible_with
  [Layout::HeredocArgumentClosingParenthesis]
end

Public Instance Methods

autocorrect(range) click to toggle source
# File lib/rubocop/cop/style/trailing_comma_in_arguments.rb, line 64
def autocorrect(range)
  PunctuationCorrector.swap_comma(range)
end
on_csend(node)
Alias for: on_send
on_send(node) click to toggle source
# File lib/rubocop/cop/style/trailing_comma_in_arguments.rb, line 55
def on_send(node)
  return unless node.arguments? && node.parenthesized?

  check(node, node.arguments, 'parameter of %<article>s method call',
        node.last_argument.source_range.end_pos,
        node.source_range.end_pos)
end
Also aliased as: on_csend

Private Instance Methods

avoid_autocorrect?(args) click to toggle source
# File lib/rubocop/cop/style/trailing_comma_in_arguments.rb, line 74
def avoid_autocorrect?(args)
  args.last.hash_type? && args.last.braces? &&
    braces_will_be_removed?(args)
end
braces_will_be_removed?(args) click to toggle source

Returns true if running with –auto-correct would remove the braces of the last argument.

# File lib/rubocop/cop/style/trailing_comma_in_arguments.rb, line 81
def braces_will_be_removed?(args)
  brace_config = config.for_cop('Style/BracesAroundHashParameters')
  return false unless brace_config.fetch('Enabled')
  return false if brace_config['AutoCorrect'] == false

  brace_style = brace_config['EnforcedStyle']
  return true if brace_style == 'no_braces'

  return false unless brace_style == 'context_dependent'

  args.one? || !args[-2].hash_type?
end