forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
guard_clause.rb
277 lines (237 loc) · 7.89 KB
/
guard_clause.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# Use a guard clause instead of wrapping the code inside a conditional
# expression
#
# A condition with an `elsif` or `else` branch is allowed unless
# one of `return`, `break`, `next`, `raise`, or `fail` is used
# in the body of the conditional expression.
#
# NOTE: Autocorrect works in most cases except with if-else statements
# that contain logical operators such as `foo || raise('exception')`
#
# @example
# # bad
# def test
# if something
# work
# end
# end
#
# # good
# def test
# return unless something
#
# work
# end
#
# # also good
# def test
# work if something
# end
#
# # bad
# if something
# raise 'exception'
# else
# ok
# end
#
# # good
# raise 'exception' if something
# ok
#
# # bad
# if something
# foo || raise('exception')
# else
# ok
# end
#
# # good
# foo || raise('exception') if something
# ok
#
# @example AllowConsecutiveConditionals: false (default)
# # bad
# def test
# if foo?
# work
# end
#
# if bar? # <- reports an offense
# work
# end
# end
#
# @example AllowConsecutiveConditionals: true
# # good
# def test
# if foo?
# work
# end
#
# if bar?
# work
# end
# end
#
# # bad
# def test
# if foo?
# work
# end
#
# do_something
#
# if bar? # <- reports an offense
# work
# end
# end
#
class GuardClause < Base
extend AutoCorrector
include RangeHelp
include MinBodyLength
include StatementModifier
MSG = 'Use a guard clause (`%<example>s`) instead of wrapping the ' \
'code inside a conditional expression.'
def on_def(node)
body = node.body
return unless body
check_ending_body(body)
end
alias on_defs on_def
def on_if(node)
return if accepted_form?(node)
if (guard_clause = node.if_branch&.guard_clause?)
kw = node.loc.keyword.source
guard = :if
elsif (guard_clause = node.else_branch&.guard_clause?)
kw = node.inverse_keyword
guard = :else
else
return
end
guard = nil if and_or_guard_clause?(guard_clause)
register_offense(node, guard_clause_source(guard_clause), kw, guard)
end
private
def check_ending_body(body)
return if body.nil?
if body.if_type?
check_ending_if(body)
elsif body.begin_type?
final_expression = body.children.last
check_ending_if(final_expression) if final_expression&.if_type?
end
end
def check_ending_if(node)
return if accepted_form?(node, ending: true) || !min_body_length?(node)
return if allowed_consecutive_conditionals? &&
consecutive_conditionals?(node.parent, node)
register_offense(node, 'return', node.inverse_keyword)
check_ending_body(node.if_branch)
end
def consecutive_conditionals?(parent, node)
parent.each_child_node.inject(false) do |if_type, child|
break if_type if node == child
child.if_type?
end
end
def register_offense(node, scope_exiting_keyword, conditional_keyword, guard = nil)
condition, = node.node_parts
example = [scope_exiting_keyword, conditional_keyword, condition.source].join(' ')
if too_long_for_single_line?(node, example)
return if trivial?(node)
example = "#{conditional_keyword} #{condition.source}; #{scope_exiting_keyword}; end"
replacement = <<~RUBY.chomp
#{conditional_keyword} #{condition.source}
#{scope_exiting_keyword}
end
RUBY
end
add_offense(node.loc.keyword, message: format(MSG, example: example)) do |corrector|
next if node.else? && guard.nil?
autocorrect(corrector, node, condition, replacement || example, guard)
end
end
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def autocorrect(corrector, node, condition, replacement, guard)
corrector.replace(node.loc.keyword.join(condition.source_range), replacement)
if_branch = node.if_branch
else_branch = node.else_branch
corrector.replace(node.loc.begin, "\n") if node.loc.begin&.is?('then')
if if_branch&.send_type? && heredoc?(if_branch.last_argument)
autocorrect_heredoc_argument(corrector, node, if_branch, else_branch, guard)
elsif else_branch&.send_type? && heredoc?(else_branch.last_argument)
autocorrect_heredoc_argument(corrector, node, else_branch, if_branch, guard)
else
corrector.remove(node.loc.end)
return unless node.else?
corrector.remove(node.loc.else)
corrector.remove(range_of_branch_to_remove(node, guard))
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def heredoc?(argument)
argument.respond_to?(:heredoc?) && argument.heredoc?
end
def autocorrect_heredoc_argument(corrector, node, heredoc_branch, leave_branch, guard)
return unless node.else?
remove_whole_lines(corrector, leave_branch.source_range)
remove_whole_lines(corrector, node.loc.else)
remove_whole_lines(corrector, node.loc.end)
remove_whole_lines(corrector, range_of_branch_to_remove(node, guard))
corrector.insert_after(
heredoc_branch.last_argument.loc.heredoc_end, "\n#{leave_branch.source}"
)
end
def range_of_branch_to_remove(node, guard)
branch = case guard
when :if then node.if_branch
when :else then node.else_branch
end
branch.source_range
end
def guard_clause_source(guard_clause)
if and_or_guard_clause?(guard_clause)
guard_clause.parent.source
else
guard_clause.source
end
end
def and_or_guard_clause?(guard_clause)
parent = guard_clause.parent
parent.and_type? || parent.or_type?
end
def too_long_for_single_line?(node, example)
max = max_line_length
max && node.source_range.column + example.length > max
end
def accepted_form?(node, ending: false)
accepted_if?(node, ending) || node.condition.multiline? || node.parent&.assignment?
end
def trivial?(node)
node.branches.one? && !node.if_branch.if_type? && !node.if_branch.begin_type?
end
def accepted_if?(node, ending)
return true if node.modifier_form? || node.ternary? || node.elsif_conditional?
if ending
node.else?
else
!node.else? || node.elsif?
end
end
def remove_whole_lines(corrector, range)
corrector.remove(range_by_whole_lines(range, include_final_newline: true))
end
def allowed_consecutive_conditionals?
cop_config.fetch('AllowConsecutiveConditionals', false)
end
end
end
end
end