/
empty_class.rb
95 lines (89 loc) · 2.15 KB
/
empty_class.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# Checks for classes and metaclasses without a body.
# Such empty classes and metaclasses are typically an oversight or we should provide a comment
# to be clearer what we're aiming for.
#
# @example
# # bad
# class Foo
# end
#
# class Bar
# class << self
# end
# end
#
# class << obj
# end
#
# # good
# class Foo
# def do_something
# # ... code
# end
# end
#
# class Bar
# class << self
# attr_reader :bar
# end
# end
#
# class << obj
# attr_reader :bar
# end
#
# @example AllowComments: false (default)
# # bad
# class Foo
# # TODO: implement later
# end
#
# class Bar
# class << self
# # TODO: implement later
# end
# end
#
# class << obj
# # TODO: implement later
# end
#
# @example AllowComments: true
# # good
# class Foo
# # TODO: implement later
# end
#
# class Bar
# class << self
# # TODO: implement later
# end
# end
#
# class << obj
# # TODO: implement later
# end
#
class EmptyClass < Base
CLASS_MSG = 'Empty class detected.'
METACLASS_MSG = 'Empty metaclass detected.'
def on_class(node)
add_offense(node, message: CLASS_MSG) unless body_or_allowed_comment_lines?(node) ||
node.parent_class
end
def on_sclass(node)
add_offense(node, message: METACLASS_MSG) unless body_or_allowed_comment_lines?(node)
end
private
def body_or_allowed_comment_lines?(node)
return true if node.body
cop_config['AllowComments'] && processed_source.contains_comment?(node.source_range)
end
end
end
end
end