Sha256: 22c4893c216e6e6b2dab526ed651c26ccba4615b033496da0443c3acefb16768

Contents?: true

Size: 1.87 KB

Versions: 11

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

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

          MSG_BEFORE = 'Use one space before `->`.'
          MSG_AFTER = 'Use one space after `->`.'

          # @sig decl: ::RBS::AST::Members::MethodDefinition
          def on_rbs_def(decl)
            base = decl.location.start_pos
            tokens = ::RBS::Parser.lex(decl.location.source).value.reject { |t| t.type == :tTRIVIA }
            ([nil] + tokens).each_cons(3) do |before, token, after|
              next unless token&.type == :pARROW

              loc = token&.location
              next unless loc

              if before && (before.location.end_pos + 1 != loc.start_pos)
                arrow = location_to_range(loc).adjust(begin_pos: base, end_pos: base)
                add_offense(arrow, message: MSG_BEFORE) do |corrector|
                  range = range_between(before.location.end_pos, loc.start_pos)
                  corrector.replace(range.adjust(begin_pos: base, end_pos: base), ' ')
                end
              end

              if loc.end_pos + 1 != after.location.start_pos
                arrow = location_to_range(loc).adjust(begin_pos: base, end_pos: base)
                add_offense(arrow, message: MSG_AFTER) do |corrector|
                  range = range_between(loc.end_pos, after.location.start_pos)
                  corrector.replace(range.adjust(begin_pos: base, end_pos: base), ' ')
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

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