forked from rubocop/rubocop
/
next.rb
244 lines (198 loc) · 6.94 KB
/
next.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
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# Use `next` to skip iteration instead of a condition at the end.
#
# @example EnforcedStyle: skip_modifier_ifs (default)
# # bad
# [1, 2].each do |a|
# if a == 1
# puts a
# end
# end
#
# # good
# [1, 2].each do |a|
# next unless a == 1
# puts a
# end
#
# # good
# [1, 2].each do |o|
# puts o unless o == 1
# end
#
# @example EnforcedStyle: always
# # With `always` all conditions at the end of an iteration needs to be
# # replaced by next - with `skip_modifier_ifs` the modifier if like
# # this one are ignored: `[1, 2].each { |a| return 'yes' if a == 1 }`
#
# # bad
# [1, 2].each do |o|
# puts o unless o == 1
# end
#
# # bad
# [1, 2].each do |a|
# if a == 1
# puts a
# end
# end
#
# # good
# [1, 2].each do |a|
# next unless a == 1
# puts a
# end
class Next < Cop
include ConfigurableEnforcedStyle
include MinBodyLength
include RangeHelp
MSG = 'Use `next` to skip iteration.'.freeze
EXIT_TYPES = %i[break return].freeze
def self.autocorrect_incompatible_with
[Style::SafeNavigation]
end
def investigate(_processed_source)
# When correcting nested offenses, we need to keep track of how much
# we have adjusted the indentation of each line
@reindented_lines = Hash.new(0)
end
def on_block(node)
return unless node.send_node.send_type? &&
node.send_node.enumerator_method?
check(node)
end
def on_while(node)
check(node)
end
alias on_until on_while
alias on_for on_while
def autocorrect(node)
lambda do |corrector|
if node.modifier_form?
autocorrect_modifier(corrector, node)
else
autocorrect_block(corrector, node)
end
end
end
private
def check(node)
return unless node.body && ends_with_condition?(node.body)
offending_node = offense_node(node.body)
add_offense(offending_node,
location: offense_location(offending_node))
end
def ends_with_condition?(body)
return true if simple_if_without_break?(body)
body.begin_type? && simple_if_without_break?(body.children.last)
end
def simple_if_without_break?(node)
return false unless if_without_else?(node)
return false if if_else_children?(node)
return false if allowed_modifier_if?(node)
!exit_body_type?(node)
end
def allowed_modifier_if?(node)
if node.modifier_form?
style == :skip_modifier_ifs
else
!min_body_length?(node)
end
end
def if_else_children?(node)
node.each_child_node(:if).any?(&:else?)
end
def if_without_else?(node)
node && node.if_type? && !node.ternary? && !node.else?
end
def exit_body_type?(node)
return false unless node.if_branch
EXIT_TYPES.include?(node.if_branch.type)
end
def offense_node(body)
*_, condition = *body
condition && condition.if_type? ? condition : body
end
def offense_location(offense_node)
offense_begin_pos = offense_node.source_range.begin
offense_begin_pos.join(offense_node.condition.source_range)
end
def autocorrect_modifier(corrector, node)
body = node.if_branch || node.else_branch
replacement =
"next #{node.inverse_keyword} #{node.condition.source}\n" \
"#{' ' * node.source_range.column}#{body.source}"
corrector.replace(node.source_range, replacement)
end
def autocorrect_block(corrector, node)
next_code = "next #{node.inverse_keyword} #{node.condition.source}"
corrector.insert_before(node.source_range, next_code)
corrector.remove(cond_range(node, node.condition))
corrector.remove(end_range(node))
lines = reindentable_lines(node)
return if lines.empty?
reindent(lines, node.condition, corrector)
end
def cond_range(node, cond)
end_pos = if node.loc.begin
node.loc.begin.end_pos # after "then"
else
cond.source_range.end_pos
end
range_between(node.source_range.begin_pos, end_pos)
end
def end_range(node)
source_buffer = node.source_range.source_buffer
end_pos = node.loc.end.end_pos
begin_pos = node.loc.end.begin_pos - node.loc.end.column
begin_pos -= 1 if end_followed_by_whitespace_only?(source_buffer,
end_pos)
range_between(begin_pos, end_pos)
end
def end_followed_by_whitespace_only?(source_buffer, end_pos)
source_buffer.source[end_pos..-1] =~ /\A\s*$/
end
def reindentable_lines(node)
buffer = node.source_range.source_buffer
# end_range starts with the final newline of the if body
lines = (node.source_range.line + 1)...node.loc.end.line
lines = lines.to_a - heredoc_lines(node)
# Skip blank lines
lines.reject { |lineno| buffer.source_line(lineno) =~ /\A\s*\z/ }
end
# Adjust indentation of `lines` to match `node`
def reindent(lines, node, corrector)
range = node.source_range
buffer = range.source_buffer
target_indent = range.source_line =~ /\S/
delta = actual_indent(lines, buffer) - target_indent
lines.each do |lineno|
reindent_line(corrector, lineno, delta, buffer)
end
end
def actual_indent(lines, buffer)
lines.map { |lineno| buffer.source_line(lineno) =~ /\S/ }.min
end
def heredoc_lines(node)
node.each_node(:dstr)
.select(&:heredoc?)
.map { |n| n.loc.heredoc_body }
.flat_map { |b| (b.line...b.last_line).to_a }
end
def reindent_line(corrector, lineno, delta, buffer)
adjustment = delta + @reindented_lines[lineno]
@reindented_lines[lineno] = adjustment
if adjustment > 0
corrector.remove_leading(buffer.line_range(lineno), adjustment)
elsif adjustment < 0
corrector.insert_before(buffer.line_range(lineno),
' ' * -adjustment)
end
end
end
end
end
end