Sha256: 5fac8043af47750bd0858543c555a1d9391b1030ba56f3b2eceece02cc6b50e5

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of class variables. Offenses
      # are signaled only on assignment to class variables to
      # reduce the number of offenses that would be reported.
      #
      # You have to be careful when setting a value for a class
      # variable; if a class has been inherited, changing the
      # value of a class variable also affects the inheriting
      # classes. This means that it's almost always better to
      # use a class instance variable instead.
      #
      # @example
      #   # bad
      #   class A
      #     @@test = 10
      #   end
      #
      #   class A
      #     def self.test(name, value)
      #       class_variable_set("@@#{name}", value)
      #     end
      #   end
      #
      #   class A; end
      #   A.class_variable_set(:@@test, 10)
      #
      #   # good
      #   class A
      #     @test = 10
      #   end
      #
      #   class A
      #     def test
      #       @@test # you can access class variable without offense
      #     end
      #   end
      #
      #   class A
      #     def self.test(name)
      #       class_variable_get("@@#{name}") # you can access without offense
      #     end
      #   end
      #
      class ClassVars < Cop
        MSG = 'Replace class var %<class_var>s with a class ' \
              'instance var.'

        def on_cvasgn(node)
          add_offense(node, location: :name)
        end

        def on_send(node)
          return unless node.method?(:class_variable_set)

          add_offense(node.first_argument)
        end

        def message(node)
          class_var, = *node
          format(MSG, class_var: class_var)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rubocop-0.89.0 lib/rubocop/cop/style/class_vars.rb
rubocop-0.88.0 lib/rubocop/cop/style/class_vars.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/class_vars.rb
rubocop-0.87.1 lib/rubocop/cop/style/class_vars.rb
rubocop-0.87.0 lib/rubocop/cop/style/class_vars.rb