Module: RuboCop::Cop::RSpec::EmptyLineSeparation

Includes:
RangeHelp, FinalEndLocation
Included in:
EmptyLineAfterExample, EmptyLineAfterExampleGroup, EmptyLineAfterFinalLet, EmptyLineAfterHook, EmptyLineAfterSubject
Defined in:
lib/rubocop/cop/rspec/mixin/empty_line_separation.rb

Overview

Helps determine the offending location if there is not an empty line following the node. Allows comments to follow directly after.

Instance Method Summary collapse

Methods included from FinalEndLocation

#final_end_location

Instance Method Details

#last_child?(node) ⇒ Boolean

Returns:

  • (Boolean)

43
44
45
46
47
# File 'lib/rubocop/cop/rspec/mixin/empty_line_separation.rb', line 43

def last_child?(node)
  return true unless node.parent&.begin_type?

  node.equal?(node.parent.children.last)
end

#missing_separating_line(node) {|offending_loc(line)| ... } ⇒ Object

Yields:


23
24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/rspec/mixin/empty_line_separation.rb', line 23

def missing_separating_line(node)
  line = final_end_location(node).line

  line += 1 while comment_line?(processed_source[line])

  return if processed_source[line].blank?

  yield offending_loc(line)
end

#missing_separating_line_offense(node) ⇒ Object


12
13
14
15
16
17
18
19
20
21
# File 'lib/rubocop/cop/rspec/mixin/empty_line_separation.rb', line 12

def missing_separating_line_offense(node)
  return if last_child?(node)

  missing_separating_line(node) do |location|
    msg = yield(node.method_name)
    add_offense(location, message: msg) do |corrector|
      corrector.insert_after(location.end, "\n")
    end
  end
end

#offending_loc(last_line) ⇒ Object


33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rspec/mixin/empty_line_separation.rb', line 33

def offending_loc(last_line)
  offending_line = processed_source[last_line - 1]

  content_length = offending_line.lstrip.length
  start          = offending_line.length - content_length

  source_range(processed_source.buffer,
               last_line, start, content_length)
end