Class: RuboCop::Cop::RBS::Layout::SpaceBeforeOverload

Inherits:
RBS::CopBase
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rbs/layout/space_before_overload.rb

Overview

Examples:

default

# bad
def foo:() -> void
       |  () -> void

# good
def foo: () -> void
       | () -> void

Constant Summary collapse

MSG =
'Use one space before overload.'

Instance Attribute Summary

Attributes inherited from RBS::CopBase

#processed_rbs_source

Instance Method Summary collapse

Methods inherited from RBS::CopBase

#investigation_rbs, #location_to_range, #on_new_investigation, #on_other_file, #on_rbs_attribute, #on_rbs_class, #on_rbs_constant, #on_rbs_global, #on_rbs_interface, #on_rbs_module, #on_rbs_new_investigation, #on_rbs_parsing_error, #on_rbs_private, #on_rbs_public, #on_rbs_type_alias, #on_rbs_var, #parse_rbs, #rbs_buffer, #tokenize, #walk

Methods included from RBS::OnTypeHelper

#on_not_type, #on_type

Instance Method Details

#on_rbs_def(decl) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rbs/layout/space_before_overload.rb', line 21

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