Sha256: fbe77281a74c1b416ec09bdd992ba517b0ec4394b4487d11d1ad33d753cf4199

Contents?: true

Size: 1.73 KB

Versions: 170

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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 < Base
        MSG = 'Replace class var %<class_var>s with a class instance var.'
        RESTRICT_ON_SEND = %i[class_variable_set].freeze

        def on_cvasgn(node)
          add_offense(node.loc.name, message: format(MSG, class_var: node.children.first))
        end

        def on_send(node)
          add_offense(
            node.first_argument, message: format(MSG, class_var: node.first_argument.source)
          )
        end
      end
    end
  end
end

Version data entries

170 entries across 163 versions & 14 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/class_vars.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/class_vars.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/class_vars.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/class_vars.rb
rubocop-1.62.0 lib/rubocop/cop/style/class_vars.rb
rubocop-1.61.0 lib/rubocop/cop/style/class_vars.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/style/class_vars.rb
rubocop-1.60.2 lib/rubocop/cop/style/class_vars.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/class_vars.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/class_vars.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/class_vars.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/class_vars.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/class_vars.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/class_vars.rb
rubocop-1.60.1 lib/rubocop/cop/style/class_vars.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/class_vars.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/class_vars.rb
rubocop-1.60.0 lib/rubocop/cop/style/class_vars.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/class_vars.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/class_vars.rb