class RuboCop::Cop::Lint::Loop

end
break if some_condition
do_something
loop do
# until replacement
# good
@example
end
break unless some_condition
do_something
loop do
# while replacement
# good
@example
end until some_condition
do_something
begin
# using until
# bad
@example
end while some_condition
do_something
begin
# using while
# bad
@example
loop body raises a ‘StopIteration` exception (which `Kernel#loop` rescues).
if a local variable inside the loop body is accessed outside of it, or if the
The cop is unsafe because behavior can change in some cases, including
@safety
Checks for uses of `begin…end while/until something`.

def build_break_line(node)

def build_break_line(node)
  conditional_keyword = node.while_post_type? ? 'unless' : 'if'
  "break #{conditional_keyword} #{node.condition.source}\n#{indent(node)}"
end

def keyword_and_condition_range(node)

def keyword_and_condition_range(node)
  node.body.loc.end.end.join(node.source_range.end)
end

def on_until_post(node)

def on_until_post(node)
  register_offense(node)
end

def on_while_post(node)

def on_while_post(node)
  register_offense(node)
end

def register_offense(node)

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