class RuboCop::Cop::Lint::TrailingCommaInAttributeDeclaration

Checks for trailing commas in attribute declarations, such as ‘#attr_reader`. Leaving a trailing comma will nullify the next method definition by overriding it with a getter method.

@example

# bad
class Foo
  attr_reader :foo,

  def bar
    puts "Unreachable."
  end
end

# good
class Foo
  attr_reader :foo

  def bar
    puts "No problem!"
  end
end

Constants

MSG

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb, line 36
def on_send(node)
  return unless node.attribute_accessor? && node.last_argument.def_type?

  trailing_comma = trailing_comma_range(node)

  add_offense(trailing_comma) { |corrector| corrector.remove(trailing_comma) }
end

Private Instance Methods

trailing_comma_range(node) click to toggle source
# File lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb, line 46
def trailing_comma_range(node)
  range_with_surrounding_space(
    node.arguments[-2].source_range,
    side: :right
  ).end.resize(1)
end