Sha256: 525823b294a19a76ee3995803e8f0995ec5314520fb6ae0996daea6b83e19a8e

Contents?: true

Size: 1.41 KB

Versions: 36

Compression:

Stored size: 1.41 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 < Base
        extend AutoCorrector

        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

        private

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

          message = format(MSG, method: node.method_name, class: name.source)

          add_offense(node.receiver.loc.name, message: message) do |corrector|
            corrector.replace(node.receiver, 'self')
          end
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_methods.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_methods.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_methods.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_methods.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_methods.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_methods.rb
rubocop-1.12.1 lib/rubocop/cop/style/class_methods.rb
rubocop-1.12.0 lib/rubocop/cop/style/class_methods.rb
rubocop-1.11.0 lib/rubocop/cop/style/class_methods.rb
rubocop-1.10.0 lib/rubocop/cop/style/class_methods.rb
rubocop-1.9.1 lib/rubocop/cop/style/class_methods.rb
rubocop-1.9.0 lib/rubocop/cop/style/class_methods.rb
rubocop-1.8.1 lib/rubocop/cop/style/class_methods.rb
rubocop-1.8.0 lib/rubocop/cop/style/class_methods.rb
rubocop-1.7.0 lib/rubocop/cop/style/class_methods.rb
rubocop-1.6.1 lib/rubocop/cop/style/class_methods.rb
rubocop-1.6.0 lib/rubocop/cop/style/class_methods.rb
rubocop-1.5.2 lib/rubocop/cop/style/class_methods.rb
rubocop-1.5.1 lib/rubocop/cop/style/class_methods.rb
rubocop-1.5.0 lib/rubocop/cop/style/class_methods.rb