Class: RuboCop::Cop::RBS::Layout::EmptyLinesAroundAccessModifier

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

Overview

Access modifiers should be surrounded by blank lines.

# bad
class Foo
  def bar: () -> void
  private
  def baz: () -> void
end

# good
class Foo
  def bar: () -> void

  private

  def baz: () -> void
end

Constant Summary collapse

MSG =
'Keep a blank line before and after `<%<kind>s>`.'

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_constant, #on_rbs_def, #on_rbs_global, #on_rbs_interface, #on_rbs_new_investigation, #on_rbs_parsing_error, #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

#add_offense_with(member, kind) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 58

def add_offense_with(member, kind)
  return if expected_empty_lines?(member)

  range = location_to_range(member.location)
  add_offense(range, message: format(MSG, kind: kind)) do |corrector|
    current_line = range_by_whole_lines(range)
    corrector.insert_before(current_line, "\n") unless previous_line_empty?(member.location.start_line)
    corrector.insert_after(current_line, "\n") unless next_line_empty?(member.location.end_line)
  end
end

#body_end?(line) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 96

def body_end?(line)
  return false unless @class_or_module_def_last_line

  line == @class_or_module_def_last_line - 1
end

#check(member, kind) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 44

def check(member, kind)
  line = member.location.start_line

  before = processed_source.lines[line - 2]
  if before.empty? || before.match?(/[^\s]/)
    add_offense_with(member, kind)
  end

  after = processed_source.lines[line]
  if after&.empty? || after&.match?(/[^\s]/)
    add_offense_with(member, kind)
  end
end

#class_def?(line) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 90

def class_def?(line)
  return false unless @class_or_module_def_first_line

  line == @class_or_module_def_first_line + 1
end

#expected_empty_lines?(member) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 86

def expected_empty_lines?(member)
  previous_line_empty?(member.location.start_line) && next_line_empty?(member.location.end_line)
end

#next_line_empty?(last_send_line) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 80

def next_line_empty?(last_send_line)
  next_line = processed_source[last_send_line]

  body_end?(last_send_line) || next_line.blank?
end

#on_rbs_class(decl) ⇒ Object Also known as: on_rbs_module



30
31
32
33
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 30

def on_rbs_class(decl)
  @class_or_module_def_first_line = decl.location.start_line
  @class_or_module_def_last_line = decl.location.end_line
end

#on_rbs_private(member) ⇒ Object



40
41
42
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 40

def on_rbs_private(member)
  check(member, 'private')
end

#on_rbs_public(member) ⇒ Object



36
37
38
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 36

def on_rbs_public(member)
  check(member, 'public')
end

#previous_line_empty?(send_line) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 73

def previous_line_empty?(send_line)
  previous_line = previous_line_ignoring_comments(processed_source, send_line)
  return true unless previous_line

  class_def?(send_line) || previous_line.blank?
end

#previous_line_ignoring_comments(processed_source, send_line) ⇒ Object



69
70
71
# File 'lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb', line 69

def previous_line_ignoring_comments(processed_source, send_line)
  processed_source[0..send_line - 2].reverse.find { |line| !comment_line?(line) }
end