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 16 def on_if(node) return if node.loc.respond_to?(:question) check(node) end
on_until(node)
click to toggle source
# File lib/rubocop/cop/lint/condition_position.rb, line 26 def on_until(node) check(node) end
on_while(node)
click to toggle source
# File lib/rubocop/cop/lint/condition_position.rb, line 22 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 32 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 42 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 46 def on_different_line?(keyword_line, cond_line) keyword_line != cond_line end