Class: RuboCop::Cop::RBS::Layout::OverloadIndentation

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

Overview

Examples:

default

# bad
def foo: () -> String | () -> (Integer)

# bad
def foo: () -> String
    | () -> (Integer)

# bad
def foo: () -> String |
         () -> (Integer)

# good
def foo: () -> String
       | () -> Integer

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/rbs/layout/overload_indentation.rb', line 25

def on_rbs_def(decl)
  base_pos = decl.location.start_pos
  base_col = decl.location.start_column
  overload_starts = decl.overloads.map { |overload| overload.method_type.location.start_pos }
  tokens = ::RBS::Parser.lex(decl.location.source).value.reject { |t| t.type == :tTRIVIA }
  first_colon_column = base_col + tokens.find { |t| t.type == :pCOLON }&.location&.start_column
  ([nil] + tokens).each_cons(3) do |before, bar, after|
    next unless before
    next unless bar
    next unless after
    next unless bar&.type == :pBAR

    loc = bar&.location
    next unless loc
    next unless overload_starts.include?(base_pos + after.location.start_pos)

    if before.location.end_line == bar.location.start_line
      range = range_between(base_pos + bar.location.start_pos, base_pos + bar.location.end_pos)
      add_offense(range, message: "Insert newline before `|`") do |corrector|
        space = range_between(base_pos + before.location.end_pos, base_pos + bar.location.start_pos)
        corrector.replace(space, "\n#{' ' * first_colon_column}")
      end
    end

    if bar.location.end_line != after.location.start_line
      range = range_between(base_pos + bar.location.start_pos, base_pos + bar.location.end_pos)
      add_offense(range, message: "Remove newline after `|`") do |corrector|
        space = range_between(base_pos + bar.location.end_pos, base_pos + after.location.start_pos)
        corrector.replace(space, ' ')
      end
    elsif bar.location.start_column != first_colon_column
      range = range_between(base_pos + bar.location.start_pos, base_pos + bar.location.end_pos)
      add_offense(range, message: 'Indent the `|` to the first `:`') do |corrector|
        space = range_between(base_pos + bar.location.start_pos - bar.location.start_column,
                              base_pos + bar.location.start_pos)
        corrector.replace(space, ' ' * first_colon_column)
      end
    end
  end
end