class RuboCop::Cop::Style::TrailingCommaInArguments

This cop checks for trailing comma in argument lists.

@example

# always bad
method(1, 2,)

# good if EnforcedStyleForMultiline is consistent_comma
method(
  1, 2,
  3,
)

# good if EnforcedStyleForMultiline is comma or consistent_comma
method(
  1,
  2,
)

# good if EnforcedStyleForMultiline is no_comma
method(
  1,
  2
)

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/trailing_comma_in_arguments.rb, line 32
def on_send(node)
  _receiver, _method_name, *args = *node
  return if args.empty?
  # It's impossible for a method call without parentheses to have
  # a trailing comma.
  return unless brackets?(node)

  check(node, args, 'parameter of %s method call',
        args.last.source_range.end_pos, node.source_range.end_pos)
end

Private Instance Methods

avoid_autocorrect?(args) click to toggle source
# File lib/rubocop/cop/style/trailing_comma_in_arguments.rb, line 45
def avoid_autocorrect?(args)
  hash_with_braces?(args.last) && 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 55
def braces_will_be_removed?(args)
  brace_config = config.for_cop('Style/BracesAroundHashParameters')
  return false unless brace_config['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.size == 1 || !args[-2].hash_type?
end
hash_with_braces?(node) click to toggle source
# File lib/rubocop/cop/style/trailing_comma_in_arguments.rb, line 49
def hash_with_braces?(node)
  node.hash_type? && node.loc.begin
end