Sha256: 410caa0d55c275e3d8cbf7382bef3b19a9fb5e65c81e940930ccd79f3d85596c

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 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
          alias on_rbs_var 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

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-on-rbs-1.3.0 lib/rubocop/cop/rbs/lint/literal_intersection.rb