Sha256: 135a107b2de0ac705c80975649a11981e4c749d7842b31457f2d01f93b3563b5

Contents?: true

Size: 1.94 KB

Versions: 45

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

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

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

        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)

          message = message(node)
          add_offense(node.loc.selector, message: message) do |corrector|
            autocorrect(corrector, node)
          end
        end

        private

        def autocorrect(corrector, 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?

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

        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

        # @!method class_eval?(node)
        def_node_matcher :class_eval?, <<~PATTERN
          (block (send _ {:class_eval :module_eval}) ...)
        PATTERN
      end
    end
  end
end

Version data entries

45 entries across 41 versions & 6 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/attr.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/attr.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/attr.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/attr.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/attr.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/attr.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/attr.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/attr.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/attr.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/rubocop-1.50.2/lib/rubocop/cop/style/attr.rb
rubocop-1.50.2 lib/rubocop/cop/style/attr.rb
rubocop-1.50.1 lib/rubocop/cop/style/attr.rb
rubocop-1.50.0 lib/rubocop/cop/style/attr.rb
rubocop-1.49.0 lib/rubocop/cop/style/attr.rb
call_your_name-0.1.0 vendor/bundle/ruby/3.1.0/gems/rubocop-1.48.1/lib/rubocop/cop/style/attr.rb
rubocop-1.48.1 lib/rubocop/cop/style/attr.rb
rubocop-1.48.0 lib/rubocop/cop/style/attr.rb
rubocop-1.47.0 lib/rubocop/cop/style/attr.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/style/attr.rb
rubocop-1.46.0 lib/rubocop/cop/style/attr.rb