Sha256: 8b6e0f19146c453f55f9d027aebcb8f71db9c4535738b126c60a2e8728c555c5

Contents?: true

Size: 1.36 KB

Versions: 12

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RBS
      module Layout
        # @example default
        #   # bad
        #   def foo:() -> void
        #          |  () -> void
        #
        #   # good
        #   def foo: () -> void
        #          | () -> void
        class SpaceBeforeOverload < RuboCop::RBS::CopBase
          extend AutoCorrector

          MSG = 'Use one space before overload.'

          # @sig decl: ::RBS::AST::Members::MethodDefinition
          def on_rbs_def(decl)
            source = processed_source.raw_source
            decl.overloads.each_with_index do |overload, i|
              loc = overload.method_type.location
              overload_char = i == 0 ? ':' : '|'

              char_start_pos = source.rindex(overload_char, loc.start_pos)
              next unless char_start_pos

              word_after_char_pos = source.index(/[^\s]/, char_start_pos + 1)
              next unless word_after_char_pos

              if char_start_pos + 2 != word_after_char_pos
                char = range_between(char_start_pos, char_start_pos + 1)
                add_offense(char) do |corrector|
                  range = range_between(char_start_pos + 1, word_after_char_pos)
                  corrector.replace(range, ' ')
                end
              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/layout/space_before_overload.rb
rubocop-on-rbs-1.2.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-1.1.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-1.0.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-0.9.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-0.8.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-0.7.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-0.6.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-0.5.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-0.4.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-0.3.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb
rubocop-on-rbs-0.2.0 lib/rubocop/cop/rbs/layout/space_before_overload.rb