Class: RuboCop::Cop::RBS::Layout::EmptyLinesAroundOverloads

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

Overview

Checks empty lines around overloads.

Examples:

# bad
def foo: () -> void

       | (Integer) -> Integer

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

Constant Summary collapse

MSG =
'Empty line detected around overloads.'

Instance Attribute Summary

Attributes inherited from RBS::CopBase

#processed_rbs_source

Instance Method Summary collapse

Methods inherited from RBS::CopBase

documentation_url, #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

#check_empty_lines(end_line, next_start_line) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_overloads.rb', line 48

def check_empty_lines(end_line, next_start_line)
  return if end_line + 1 == next_start_line

  total = 0
  range = end_line...(next_start_line - 1)
  processed_source.raw_source.each_line.each_with_index do |line, lineno|
    if range.cover?(lineno) && line == "\n"
      empty_line = range_between(total, total + 1)
      add_offense(empty_line) do |corrector|
        corrector.remove(empty_line)
      end
    end
    total += line.length
  end
end

#on_rbs_def(decl) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_overloads.rb', line 26

def on_rbs_def(decl)
  decl.overloads.each_cons(2) do |overload, next_overload|
    end_line = overload&.method_type&.location&.end_line || 0
    next_start_line = next_overload&.method_type&.location&.start_line || 0
    check_empty_lines(end_line, next_start_line)
  end

  if decl.overloading? && decl.overloads.length.positive?
    # : () -> void
    #
    #   | ...
    end_location = decl.overloads.last.method_type.location or return
    slice = processed_source.raw_source[end_location.end_pos..] or return
    bar_index = slice.index('|') or return
    bar_line = slice[0..bar_index]&.count("\n") or return
    check_empty_lines(end_location.end_line, end_location.end_line + bar_line)
  end
end