class RuboCop::Cop::Lint::ConditionPosition

This cop checks for conditions that are not on the same line as if/while/until.

@example

if
  some_condition
  do_something
end

Public Instance Methods

on_if(node) click to toggle source
# File lib/rubocop/cop/lint/condition_position.rb, line 18
def on_if(node)
  return if ternary?(node)

  check(node)
end
on_until(node) click to toggle source
# File lib/rubocop/cop/lint/condition_position.rb, line 28
def on_until(node)
  check(node)
end
on_while(node) click to toggle source
# File lib/rubocop/cop/lint/condition_position.rb, line 24
def on_while(node)
  check(node)
end

Private Instance Methods

check(node) click to toggle source
# File lib/rubocop/cop/lint/condition_position.rb, line 34
def check(node)
  return if !node.loc.keyword.is?('elsif') && node.loc.end.nil?

  condition, = *node
  return unless on_different_line?(node.loc.keyword.line,
                                   condition.source_range.line)

  add_offense(condition, :expression, message(node.loc.keyword.source))
end
message(keyword) click to toggle source
# File lib/rubocop/cop/lint/condition_position.rb, line 44
def message(keyword)
  "Place the condition on the same line as `#{keyword}`."
end
on_different_line?(keyword_line, cond_line) click to toggle source
# File lib/rubocop/cop/lint/condition_position.rb, line 48
def on_different_line?(keyword_line, cond_line)
  keyword_line != cond_line
end