class RuboCop::Cop::Style::NegatedWhile

Checks for uses of while with a negated condition.

Constants

MSG

Public Instance Methods

message(node) click to toggle source
# File lib/rubocop/cop/style/negated_while.rb, line 20
def message(node)
  if node.type == :while
    format(MSG, 'until', 'while')
  else
    format(MSG, 'while', 'until')
  end
end
on_until(node) click to toggle source
# File lib/rubocop/cop/style/negated_while.rb, line 16
def on_until(node)
  check_negative_conditional(node)
end
on_while(node) click to toggle source
# File lib/rubocop/cop/style/negated_while.rb, line 12
def on_while(node)
  check_negative_conditional(node)
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/negated_while.rb, line 30
def autocorrect(node)
  lambda do |corrector|
    condition, _body, _rest = *node
    # Look inside parentheses around the condition, if any.
    condition, = *condition while condition.type == :begin
    # Unwrap the negated portion of the condition (a send node).
    pos_condition, _method, = *condition
    corrector.replace(
      node.loc.keyword,
      node.type == :while ? 'until' : 'while')
    corrector.replace(condition.source_range, pos_condition.source)
  end
end