Sha256: 5c08aad21183165f0322db2e8c9e7c70991d261e1bfd5c35f2231d65f7788d70

Contents?: true

Size: 1.46 KB

Versions: 61

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of the class/module name instead of
      # self, when defining class/module methods.
      #
      # @example
      #   # bad
      #   class SomeClass
      #     def SomeClass.class_method
      #       # ...
      #     end
      #   end
      #
      #   # good
      #   class SomeClass
      #     def self.class_method
      #       # ...
      #     end
      #   end
      class ClassMethods < Cop
        MSG = 'Use `self.%<method>s` instead of `%<class>s.%<method>s`.'

        def on_class(node)
          return unless node.body

          if node.body.defs_type?
            check_defs(node.identifier, node.body)
          elsif node.body.begin_type?
            node.body.each_child_node(:defs) do |def_node|
              check_defs(node.identifier, def_node)
            end
          end
        end
        alias on_module on_class

        def autocorrect(node)
          ->(corrector) { corrector.replace(node.loc.name, 'self') }
        end

        private

        def check_defs(name, node)
          # check if the class/module name matches the definee for the defs node
          return unless name == node.receiver

          add_offense(node.receiver, location: :name)
        end

        def message(node)
          _, class_name = *node

          format(MSG, method: node.parent.method_name, class: class_name)
        end
      end
    end
  end
end

Version data entries

61 entries across 42 versions & 5 rubygems

Version Path
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/class_methods.rb
rubocop-0.79.0 lib/rubocop/cop/style/class_methods.rb
rubocop-0.78.0 lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/class_methods.rb
rubocop-0.77.0 lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/class_methods.rb
rubocop-0.76.0 lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/class_methods.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/class_methods.rb