Sha256: 2d93d1abb9281f641dc46584bdea38864e387ba8a3ee9effca9af2aedb3a4587

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# rbs_inline: enabled
# frozen_string_literal: true

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

          # @rbs decl: RBS::AST::Declarations::Class
          def on_rbs_class(decl)
            # The meaning of `self` and `instance` changes in generic class.
            return unless decl.type_params.empty?

            decl.members.each do |member|
              case member
              when ::RBS::AST::Members::MethodDefinition
                next unless member.kind == :instance

                member.overloads.each do |overload|
                  overload.method_type.each_type do |type|
                    check_type(type)
                  end
                end
              when ::RBS::AST::Members::InstanceVariable
                check_type(member.type)
              end
            end
          end

          def check_type(type)
            case type
            when ::RBS::Types::Bases::Instance
              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/instance_with_instance.rb