Sha256: ae8b32eec57ad7bba9583fdc7d1f29402a9f9c9918b15e96cb461361b07d12b2

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RBS
      module Lint
        # Checks that there are no repeated overload bodies
        #
        # @example default
        #   # bad
        #   1 & 2
        #
        #   # bad
        #   1 & _Foo
        #
        class LiteralIntersection < RuboCop::RBS::CopBase
          MSG = "Don't use literals with `&`."

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

          def check_type(type)
            on_type([::RBS::Types::Intersection], type) do |intersection|
              check_intersection(intersection)
            end
          end

          def on_rbs_constant(type)
            check_type(type.type)
          end
          alias on_rbs_global on_rbs_constant
          alias on_rbs_type_alias on_rbs_constant
          alias on_rbs_attribute on_rbs_constant

          def check_intersection(intersection)
            intersection.types.each do |type|
              check_intersection_child(type)
            end
          end

          def check_intersection_child(type)
            case type
            when ::RBS::Types::Literal
              range = location_to_range(type.location)
              add_offense(range)
            when ::RBS::Types::Intersection
              check_intersection(type)
            end
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-on-rbs-1.2.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb
rubocop-on-rbs-1.1.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb
rubocop-on-rbs-1.0.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb
rubocop-on-rbs-0.9.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb
rubocop-on-rbs-0.8.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb
rubocop-on-rbs-0.7.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb
rubocop-on-rbs-0.6.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb
rubocop-on-rbs-0.5.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb
rubocop-on-rbs-0.4.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb