forked from rubocop/rubocop
/
block_delimiters.rb
331 lines (288 loc) · 9.67 KB
/
block_delimiters.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# Check for uses of braces or do/end around single line or
# multi-line blocks.
#
# @example EnforcedStyle: line_count_based (default)
# # bad - single line block
# items.each do |item| item / 5 end
#
# # good - single line block
# items.each { |item| item / 5 }
#
# # bad - multi-line block
# things.map { |thing|
# something = thing.some_method
# process(something)
# }
#
# # good - multi-line block
# things.map do |thing|
# something = thing.some_method
# process(something)
# end
#
# @example EnforcedStyle: semantic
# # Prefer `do...end` over `{...}` for procedural blocks.
#
# # return value is used/assigned
# # bad
# foo = map do |x|
# x
# end
# puts (map do |x|
# x
# end)
#
# # return value is not used out of scope
# # good
# map do |x|
# x
# end
#
# # Prefer `{...}` over `do...end` for functional blocks.
#
# # return value is not used out of scope
# # bad
# each { |x|
# x
# }
#
# # return value is used/assigned
# # good
# foo = map { |x|
# x
# }
# map { |x|
# x
# }.inspect
#
# # The AllowBracesOnProceduralOneLiners option is ignored unless the
# # EnforcedStyle is set to `semantic`. If so:
#
# # If the AllowBracesOnProceduralOneLiners option is unspecified, or
# # set to `false` or any other falsey value, then semantic purity is
# # maintained, so one-line procedural blocks must use do-end, not
# # braces.
#
# # bad
# collection.each { |element| puts element }
#
# # good
# collection.each do |element| puts element end
#
# # If the AllowBracesOnProceduralOneLiners option is set to `true`, or
# # any other truthy value, then one-line procedural blocks may use
# # either style. (There is no setting for requiring braces on them.)
#
# # good
# collection.each { |element| puts element }
#
# # also good
# collection.each do |element| puts element end
#
# @example EnforcedStyle: braces_for_chaining
# # bad
# words.each do |word|
# word.flip.flop
# end.join("-")
#
# # good
# words.each { |word|
# word.flip.flop
# }.join("-")
#
# @example EnforcedStyle: always_braces
# # bad
# words.each do |word|
# word.flip.flop
# end
#
# # good
# words.each { |word|
# word.flip.flop
# }
#
class BlockDelimiters < Cop
include ConfigurableEnforcedStyle
include IgnoredMethods
ALWAYS_BRACES_MESSAGE = 'Prefer `{...}` over `do...end` for blocks.'
.freeze
def on_send(node)
return unless node.arguments?
return if node.parenthesized? || node.operator_method?
node.arguments.each do |arg|
get_blocks(arg) do |block|
# If there are no parentheses around the arguments, then braces
# and do-end have different meaning due to how they bind, so we
# allow either.
ignore_node(block)
end
end
end
def on_block(node)
return if ignored_node?(node)
add_offense(node, location: :begin) unless proper_block_style?(node)
end
def autocorrect(node)
return if correction_would_break_code?(node)
if node.braces?
replace_braces_with_do_end(node.loc)
else
replace_do_end_with_braces(node.loc)
end
end
private
def line_count_based_message(node)
if node.multiline?
'Avoid using `{...}` for multi-line blocks.'
else
'Prefer `{...}` over `do...end` for single-line blocks.'
end
end
def semantic_message(node)
block_begin = node.loc.begin.source
if block_begin == '{'
'Prefer `do...end` over `{...}` for procedural blocks.'
else
'Prefer `{...}` over `do...end` for functional blocks.'
end
end
def braces_for_chaining_message(node)
if node.multiline?
if node.chained?
'Prefer `{...}` over `do...end` for multi-line chained blocks.'
else
'Prefer `do...end` for multi-line blocks without chaining.'
end
else
'Prefer `{...}` over `do...end` for single-line blocks.'
end
end
def message(node)
case style
when :line_count_based then line_count_based_message(node)
when :semantic then semantic_message(node)
when :braces_for_chaining then braces_for_chaining_message(node)
when :always_braces then ALWAYS_BRACES_MESSAGE
end
end
def replace_braces_with_do_end(loc)
b = loc.begin
e = loc.end
lambda do |corrector|
corrector.insert_before(b, ' ') unless whitespace_before?(b)
corrector.insert_before(e, ' ') unless whitespace_before?(e)
corrector.insert_after(b, ' ') unless whitespace_after?(b)
corrector.replace(b, 'do')
corrector.replace(e, 'end')
end
end
def replace_do_end_with_braces(loc)
b = loc.begin
e = loc.end
lambda do |corrector|
corrector.insert_after(b, ' ') unless whitespace_after?(b, 2)
corrector.replace(b, '{')
corrector.replace(e, '}')
end
end
def whitespace_before?(range)
range.source_buffer.source[range.begin_pos - 1, 1] =~ /\s/
end
def whitespace_after?(range, length = 1)
range.source_buffer.source[range.begin_pos + length, 1] =~ /\s/
end
# rubocop:disable Metrics/CyclomaticComplexity
def get_blocks(node, &block)
case node.type
when :block
yield node
when :send
get_blocks(node.receiver, &block) if node.receiver
when :hash
# A hash which is passed as method argument may have no braces
# In that case, one of the K/V pairs could contain a block node
# which could change in meaning if do...end replaced {...}
return if node.braces?
node.each_child_node { |child| get_blocks(child, &block) }
when :pair
node.each_child_node { |child| get_blocks(child, &block) }
end
end
# rubocop:enable Metrics/CyclomaticComplexity
def proper_block_style?(node)
return true if ignored_method?(node.method_name)
case style
when :line_count_based then line_count_based_block_style?(node)
when :semantic then semantic_block_style?(node)
when :braces_for_chaining then braces_for_chaining_style?(node)
when :always_braces then braces_style?(node)
end
end
def line_count_based_block_style?(node)
node.multiline? ^ node.braces?
end
def semantic_block_style?(node)
method_name = node.method_name
if node.braces?
functional_method?(method_name) || functional_block?(node) ||
(procedural_oneliners_may_have_braces? && !node.multiline?)
else
procedural_method?(method_name) || !return_value_used?(node)
end
end
def braces_for_chaining_style?(node)
block_begin = node.loc.begin.source
block_begin == if node.multiline?
(node.chained? ? '{' : 'do')
else
'{'
end
end
def braces_style?(node)
node.loc.begin.source == '{'
end
def correction_would_break_code?(node)
return unless node.keywords?
node.send_node.arguments? && !node.send_node.parenthesized?
end
def functional_method?(method_name)
cop_config['FunctionalMethods'].map(&:to_sym).include?(method_name)
end
def functional_block?(node)
return_value_used?(node) || return_value_of_scope?(node)
end
def procedural_oneliners_may_have_braces?
cop_config['AllowBracesOnProceduralOneLiners']
end
def procedural_method?(method_name)
cop_config['ProceduralMethods'].map(&:to_sym).include?(method_name)
end
def return_value_used?(node)
return unless node.parent
# If there are parentheses around the block, check if that
# is being used.
if node.parent.begin_type?
return_value_used?(node.parent)
else
node.parent.assignment? || node.parent.send_type?
end
end
def return_value_of_scope?(node)
return unless node.parent
conditional?(node.parent) || array_or_range?(node.parent) ||
node.parent.children.last == node
end
def conditional?(node)
node.if_type? || node.or_type? || node.and_type?
end
def array_or_range?(node)
node.array_type? || node.range_type?
end
end
end
end
end