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