Sha256: 535b97fcfb134b1b6ea606fad9ac064d38753ad0f4ce03ed90148e674fa86b20

Contents?: true

Size: 1.3 KB

Versions: 151

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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
      #
      class TrailingCommaInAttributeDeclaration < Base
        extend AutoCorrector
        include RangeHelp

        MSG = 'Avoid leaving a trailing comma in attribute declarations.'

        def on_send(node)
          return unless node.attribute_accessor? && node.arguments.last.def_type?

          trailing_comma = trailing_comma_range(node)

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

        private

        def trailing_comma_range(node)
          range_with_surrounding_space(
            node.arguments[-2].source_range,
            side: :right
          ).end.resize(1)
        end
      end
    end
  end
end

Version data entries

151 entries across 144 versions & 13 rubygems

Version Path
rubocop-1.37.0 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.36.0 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.35.1 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.35.0 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.34.1 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.34.0 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.33.0 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.32.0 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.31.2 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.31.1 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
rubocop-1.31.0 lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb