Sha256: b53baeb5b0f24d8e8e92341e54606dfa7e5102dee83e47be6920f071ce9efb64

Contents?: true

Size: 1.97 KB

Versions: 11

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RBS
      module Style
        # @example default
        #   # bad
        #   def foo: () -> TrueClass
        #
        #   # bad
        #   def bar: () -> NilClass
        #
        #   # good
        #   def foo: () -> true
        #
        #   # good
        #   def bar: () -> nil
        class ClassicType < RuboCop::RBS::CopBase
          extend AutoCorrector

          Types = ::RBS::Types

          def on_rbs_def(decl)
            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)
            find_replacement(type) do |t, replaced|
              range = location_to_range(t.location)
              add_offense(range, message: "Use `#{replaced}` instead of `#{t}`") do |corrector|
                corrector.replace(range, replaced)
              end
            end
          end

          def find_replacement(type, &block)
            case type
            when Types::Record,
                 Types::Tuple,
                 Types::Union,
                 Types::Intersection,
                 Types::Optional,
                 Types::Proc,
                 Types::Alias,
                 Types::Interface
              type.each_type do |t|
                find_replacement(t, &block)
              end
            when Types::ClassInstance
              case type.name.to_s
              when 'TrueClass', '::TrueClass'
                block.call([type, 'true'])
              when 'FalseClass', '::FalseClass'
                block.call([type, 'false'])
              when 'NilClass', '::NilClass'
                block.call([type, 'nil'])
              end
              type.each_type do |arg|
                find_replacement(arg, &block)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rubocop-on-rbs-1.2.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-1.1.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-1.0.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-0.9.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-0.8.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-0.7.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-0.6.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-0.5.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-0.4.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-0.3.0 lib/rubocop/cop/rbs/style/classic_type.rb
rubocop-on-rbs-0.2.0 lib/rubocop/cop/rbs/style/classic_type.rb