-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
empty_line_after_magic_comment.rb
64 lines (56 loc) · 1.85 KB
/
empty_line_after_magic_comment.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# Checks for a newline after the final magic comment.
#
# @example
# # good
# # frozen_string_literal: true
#
# # Some documentation for Person
# class Person
# # Some code
# end
#
# # bad
# # frozen_string_literal: true
# # Some documentation for Person
# class Person
# # Some code
# end
class EmptyLineAfterMagicComment < Base
include RangeHelp
extend AutoCorrector
MSG = 'Add an empty line after magic comments.'
def on_new_investigation
return unless processed_source.ast &&
(last_magic_comment = last_magic_comment(processed_source))
return if processed_source[last_magic_comment.loc.line].strip.empty?
offending_range = offending_range(last_magic_comment)
add_offense(offending_range) do |corrector|
corrector.insert_before(offending_range, "\n")
end
end
private
def offending_range(last_magic_comment)
source_range(processed_source.buffer, last_magic_comment.loc.line + 1, 0)
end
# Find the last magic comment in the source file.
#
# Take all comments that precede the first line of code, select the
# magic comments, and return the last magic comment in the file.
#
# @return [Parser::Source::Comment] if magic comments exist before code
# @return [nil] otherwise
def last_magic_comment(source)
source
.comments
.take_while { |comment| comment.loc.line < source.ast.loc.line }
.reverse
.find { |comment| MagicComment.parse(comment.text).any? }
end
end
end
end
end