forked from rubocop/rubocop
/
empty_else.rb
204 lines (183 loc) · 4.38 KB
/
empty_else.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# Checks for empty else-clauses, possibly including comments and/or an
# explicit `nil` depending on the EnforcedStyle.
#
# @example EnforcedStyle: both (default)
# # warn on empty else and else with nil in it
#
# # bad
# if condition
# statement
# else
# nil
# end
#
# # bad
# if condition
# statement
# else
# end
#
# # good
# if condition
# statement
# else
# statement
# end
#
# # good
# if condition
# statement
# end
#
# @example EnforcedStyle: empty
# # warn only on empty else
#
# # bad
# if condition
# statement
# else
# end
#
# # good
# if condition
# statement
# else
# nil
# end
#
# # good
# if condition
# statement
# else
# statement
# end
#
# # good
# if condition
# statement
# end
#
# @example EnforcedStyle: nil
# # warn on else with nil in it
#
# # bad
# if condition
# statement
# else
# nil
# end
#
# # good
# if condition
# statement
# else
# end
#
# # good
# if condition
# statement
# else
# statement
# end
#
# # good
# if condition
# statement
# end
#
# @example AllowComments: false (default)
#
# # bad
# if condition
# statement
# else
# # something comment
# nil
# end
#
# # bad
# if condition
# statement
# else
# # something comment
# end
#
# @example AllowComments: true
#
# # good
# if condition
# statement
# else
# # something comment
# nil
# end
#
# # good
# if condition
# statement
# else
# # something comment
# end
#
class EmptyElse < Base
include OnNormalIfUnless
include ConfigurableEnforcedStyle
include RangeHelp
extend AutoCorrector
MSG = 'Redundant `else`-clause.'
def on_normal_if_unless(node)
check(node)
end
def on_case(node)
check(node)
end
private
def check(node)
return if cop_config['AllowComments'] && comment_in_else?(node.loc)
empty_check(node) if empty_style?
nil_check(node) if nil_style?
end
def nil_style?
style == :nil || style == :both
end
def empty_style?
style == :empty || style == :both
end
def empty_check(node)
return unless node.else? && !node.else_branch
add_offense(node.loc.else) { |corrector| autocorrect(corrector, node) }
end
def nil_check(node)
return unless node.else_branch&.nil_type?
add_offense(node.loc.else) { |corrector| autocorrect(corrector, node) }
end
def autocorrect(corrector, node)
return false if autocorrect_forbidden?(node.type.to_s)
return false if comment_in_else?(node.loc)
end_pos = base_node(node).loc.end.begin_pos
corrector.remove(range_between(node.loc.else.begin_pos, end_pos))
end
def comment_in_else?(loc)
return false if loc.else.nil? || loc.end.nil?
processed_source.contains_comment?(loc.else.join(loc.end))
end
def base_node(node)
return node if node.case_type?
return node unless node.elsif?
node.each_ancestor(:if, :case, :when).find(-> { node }) { |parent| parent.loc.end }
end
def autocorrect_forbidden?(type)
[type, 'both'].include?(missing_else_style)
end
def missing_else_style
missing_cfg = config.for_cop('Style/MissingElse')
missing_cfg.fetch('Enabled') ? missing_cfg['EnforcedStyle'] : nil
end
end
end
end
end