Sha256: 772be2ed8fd13fabdca4e7c2766f333c7e8a89e21fe222b1cea8653d04df9fb1

Contents?: true

Size: 1.58 KB

Versions: 6789

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of Module#attr.
      #
      # @example
      #   # bad - creates a single attribute accessor (deprecated in Ruby 1.9)
      #   attr :something, true
      #   attr :one, :two, :three # behaves as attr_reader
      #
      #   # good
      #   attr_accessor :something
      #   attr_reader :one, :two, :three
      #
      class Attr < Cop
        include RangeHelp

        MSG = 'Do not use `attr`. Use `%<replacement>s` instead.'.freeze

        def on_send(node)
          return unless node.command?(:attr) && node.arguments?

          add_offense(node, location: :selector)
        end

        def autocorrect(node)
          attr_name, setter = *node.arguments

          node_expr = node.source_range
          attr_expr = attr_name.source_range

          if setter && (setter.true_type? || setter.false_type?)
            remove = range_between(attr_expr.end_pos, node_expr.end_pos)
          end

          lambda do |corrector|
            corrector.replace(node.loc.selector, replacement_method(node))
            corrector.remove(remove) if remove
          end
        end

        private

        def message(node)
          format(MSG, replacement: replacement_method(node))
        end

        def replacement_method(node)
          setter = node.last_argument

          if setter && (setter.true_type? || setter.false_type?)
            setter.true_type? ? 'attr_accessor' : 'attr_reader'
          else
            'attr_reader'
          end
        end
      end
    end
  end
end

Version data entries

6,789 entries across 6,783 versions & 25 rubygems

Version Path
cybrid_api_organization_ruby-0.123.143 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_id_ruby-0.123.143 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_bank_ruby-0.123.142 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_id_ruby-0.123.142 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_organization_ruby-0.123.142 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_bank_ruby-0.123.140 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_id_ruby-0.123.140 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_organization_ruby-0.123.140 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_bank_ruby-0.123.139 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_id_ruby-0.123.139 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_organization_ruby-0.123.139 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_id_ruby-0.123.137 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_organization_ruby-0.123.137 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_bank_ruby-0.123.137 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_organization_ruby-0.123.135 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_bank_ruby-0.123.135 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_id_ruby-0.123.135 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_bank_ruby-0.123.134 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_id_ruby-0.123.134 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb
cybrid_api_organization_ruby-0.123.134 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/attr.rb