forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
empty_file.rb
53 lines (45 loc) · 1.16 KB
/
empty_file.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
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop enforces that Ruby source files are not empty.
#
# @example
# # bad
# # Empty file
#
# # good
# # File containing non commented source lines
#
# @example AllowComments: true (default)
# # good
# # File consisting only of comments
#
# @example AllowComments: false
# # bad
# # File consisting only of comments
#
class EmptyFile < Base
include RangeHelp
MSG = 'Empty file detected.'
def on_new_investigation
return unless offending?
range = source_range(processed_source.buffer, 1, 0)
add_offense(range)
end
private
def offending?
empty_file? || (!cop_config['AllowComments'] && contains_only_comments?)
end
def empty_file?
processed_source.buffer.source.empty?
end
def contains_only_comments?
processed_source.lines.all? do |line|
line.blank? || comment_line?(line)
end
end
end
end
end
end