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.

Examples:

# 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

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



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

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)


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

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



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

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)


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

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)


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

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)


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

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



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

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



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

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

#on_rbs_public(member) ⇒ Object



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

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

#previous_line_empty?(send_line) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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