Sha256: 44d2474c33d868bfb465ed0f22ea1d32ecfb1b2104248f7c84ce73c77a8e031c

Contents?: true

Size: 1.8 KB

Versions: 13

Compression:

Stored size: 1.8 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.'

        def on_send(node)
          return unless node.command?(:attr) && node.arguments?
          # check only for method definitions in class/module body
          return if node.parent &&
                    !node.parent.class_type? &&
                    !class_eval?(node.parent)

          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

          remove = range_between(attr_expr.end_pos, node_expr.end_pos) if setter&.boolean_type?

          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&.boolean_type?
            setter.true_type? ? 'attr_accessor' : 'attr_reader'
          else
            'attr_reader'
          end
        end

        def_node_matcher :class_eval?, <<~PATTERN
          (block (send _ {:class_eval :module_eval}) ...)
        PATTERN
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
rubocop-0.89.0 lib/rubocop/cop/style/attr.rb
rubocop-0.88.0 lib/rubocop/cop/style/attr.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/attr.rb
rubocop-0.87.1 lib/rubocop/cop/style/attr.rb
rubocop-0.87.0 lib/rubocop/cop/style/attr.rb
rubocop-0.86.0 lib/rubocop/cop/style/attr.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/attr.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/attr.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/attr.rb
rubocop-0.85.1 lib/rubocop/cop/style/attr.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/attr.rb
rubocop-0.85.0 lib/rubocop/cop/style/attr.rb
rubocop-0.84.0 lib/rubocop/cop/style/attr.rb