class RuboCop::Cop::Style::WhileUntilModifier

Checks for while and until statements that would fit on one line if written as a modifier while/until. The maximum line length is configurable.

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/while_until_modifier.rb, line 20
def autocorrect(node)
  cond, body = *node
  oneline = "#{body.source} #{node.loc.keyword.source} " + cond.source
  ->(corrector) { corrector.replace(node.source_range, oneline) }
end
on_until(node) click to toggle source
# File lib/rubocop/cop/style/while_until_modifier.rb, line 16
def on_until(node)
  check(node)
end
on_while(node) click to toggle source
# File lib/rubocop/cop/style/while_until_modifier.rb, line 12
def on_while(node)
  check(node)
end

Private Instance Methods

check(node) click to toggle source
# File lib/rubocop/cop/style/while_until_modifier.rb, line 28
def check(node)
  return unless node.loc.end
  return unless fit_within_line_as_modifier_form?(node)
  add_offense(node, :keyword, message(node.loc.keyword.source))
end
message(keyword) click to toggle source
# File lib/rubocop/cop/style/while_until_modifier.rb, line 34
def message(keyword)
  "Favor modifier `#{keyword}` usage when having a single-line body."
end