Sha256: cad99c5e9316998dec97a9d309cdcfd8d6f534c50037549aa527c7283373dc56

Contents?: true

Size: 1.42 KB

Versions: 12

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RBS
      module Style
        # @example default
        #   # bad
        #   def foo: (nil?) -> void
        #
        #   # good
        #   def foo: (nil) -> void
        class OptionalNil < RuboCop::RBS::CopBase
          extend AutoCorrector

          # @sig decl: ::RBS::AST::Members::MethodDefinition
          def on_rbs_def(decl)
            decl.overloads.each do |overload|
              overload.method_type.each_type do |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.to_s)
                  end
                end
              end
            end
          end

          # @rbs type: ::RBS::Types::t
          def find_replacement(type, &block)
            case type
            when ::RBS::Types::Optional
              case type.type
              when ::RBS::Types::Bases::Nil
                block.call([type, ::RBS::Types::Bases::Nil.new(location: nil)])
              else
                find_replacement(type.type, &block)
              end
            else
              type.each_type do |type|
                find_replacement(type, &block)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

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