Sha256: 9a40e53d8fbb4f080fce1697cba94187130d4ed0c3ee34f62cdcaf72de18e0ef

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# rbs_inline: enabled
# frozen_string_literal: true

module RuboCop
  module Cop
    module RBS
      module Style
        # Checks that `class` in singleton context.
        #
        # @example (default)
        #   # bad
        #   def self.foo: (class) -> class
        #
        #   # good
        #   def self.foo: (self) -> self
        #
        class ClassWithSingleton < RuboCop::RBS::CopBase
          extend AutoCorrector
          MSG = 'Use `self` instead of `class`.'

          # @rbs decl: RBS::AST::Members::MethodDefinition
          def on_rbs_def(decl)
            return unless decl.kind == :singleton

            decl.overloads.each do |overload|
              overload.method_type.each_type do |type|
                check_type(type)
              end
            end
          end

          # @rbs type: RBS::Types::t
          def check_type(type)
            case type
            when ::RBS::Types::Bases::Class
              return unless type.location

              range = location_to_range(type.location)
              add_offense(range) do |corrector|
                corrector.replace(range, 'self')
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-on-rbs-1.3.0 lib/rubocop/cop/rbs/style/class_with_singleton.rb