forked from rubocop/rubocop-rspec
/
blank_line_separation.rb
45 lines (34 loc) · 1.18 KB
/
blank_line_separation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true
module RuboCop
module RSpec
# Helps determine the offending location if there is not a blank line
# following the node. Allows comments to follow directly after.
module BlankLineSeparation
include FinalEndLocation
include RuboCop::Cop::RangeHelp
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
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
def last_child?(node)
return true unless node.parent&.begin_type?
node.equal?(node.parent.children.last)
end
def autocorrect(node)
lambda do |corrector|
missing_separating_line(node) do |location|
corrector.insert_after(location.end, "\n")
end
end
end
end
end
end