/
loop.rb
86 lines (77 loc) · 2.07 KB
/
loop.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
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for uses of `begin...end while/until something`.
#
# @safety
# The cop is unsafe because behaviour can change in some cases, including
# if a local variable inside the loop body is accessed outside of it, or if the
# loop body raises a `StopIteration` exception (which `Kernel#loop` rescues).
#
# @example
#
# # bad
#
# # using while
# begin
# do_something
# end while some_condition
#
# @example
#
# # bad
#
# # using until
# begin
# do_something
# end until some_condition
#
# @example
#
# # good
#
# # while replacement
# loop do
# do_something
# break unless some_condition
# end
#
# @example
#
# # good
#
# # until replacement
# loop do
# do_something
# break if some_condition
# end
class Loop < Base
extend AutoCorrector
MSG = 'Use `Kernel#loop` with `break` rather than `begin/end/until`(or `while`).'
def on_while_post(node)
register_offense(node)
end
def on_until_post(node)
register_offense(node)
end
private
def register_offense(node)
body = node.body
add_offense(node.loc.keyword) do |corrector|
corrector.replace(body.loc.begin, 'loop do')
corrector.remove(keyword_and_condition_range(node))
corrector.insert_before(body.loc.end, build_break_line(node))
end
end
def keyword_and_condition_range(node)
node.body.loc.end.end.join(node.source_range.end)
end
def build_break_line(node)
conditional_keyword = node.while_post_type? ? 'unless' : 'if'
"break #{conditional_keyword} #{node.condition.source}\n#{indent(node)}"
end
end
end
end
end