Sha256: feb4de880d0b6e3888856658aab031582528a3e9bb10b37f67551e28e5cd4793

Contents?: true

Size: 1.64 KB

Versions: 12

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RBS
      module Style
        # @example default
        #   # bad
        #   def foo: (Integer | Integer) -> void
        #
        #   # bad
        #   def foo: (Integer & Integer) -> void
        #
        #   # good
        #   def foo: (Integer) -> void
        class DuplicatedType < 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|
                check_type(type)
              end
            end
          end

          # @rbs type: ::RBS::Types::t
          def check_type(type)
            case type
            when ::RBS::Types::Record,
                 ::RBS::Types::Tuple,
                 ::RBS::Types::Optional,
                 ::RBS::Types::ClassInstance,
                 ::RBS::Types::Proc
              type.each_type do |t|
                check_type(t)
              end
            when ::RBS::Types::Union,
                 ::RBS::Types::Intersection
              set = Set.new
              type.types.each do |t|
                if set.include?(t)
                  if t.location
                    range = location_to_range(t.location)
                    add_offense(range, message: "Duplicated type `#{t}`.")
                  end
                else
                  set.add(t)
                end
              end
              type.each_type do |t|
                check_type(t)
              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/duplicated_type.rb
rubocop-on-rbs-1.2.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-1.1.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-1.0.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-0.9.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-0.8.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-0.7.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-0.6.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-0.5.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-0.4.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-0.3.0 lib/rubocop/cop/rbs/style/duplicated_type.rb
rubocop-on-rbs-0.2.0 lib/rubocop/cop/rbs/style/duplicated_type.rb