forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
empty_else.rb
173 lines (153 loc) · 3.72 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
# 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: 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 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
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)
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) do |corrector|
autocorrect(corrector, node)
end
end
def nil_check(node)
return unless node.else_branch&.nil_type?
add_offense(node.loc.else) do |corrector|
autocorrect(corrector, node)
end
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 }) do |parent|
parent.loc.end
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