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
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb