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(overload, next_overload) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_overloads.rb', line 32

def check_empty_lines(overload, next_overload)
  return if overload.method_type.location.end_line + 1 == next_overload.method_type.location.start_line

  total = 0
  range = overload.method_type.location.end_line...(next_overload.method_type.location.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



24
25
26
27
28
29
30
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_overloads.rb', line 24

def on_rbs_def(decl)
  return unless 1 < decl.overloads.length

  decl.overloads.each_cons(2) do |overload, next_overload|
    check_empty_lines(overload, next_overload)
  end
end