class RuboCop::Cop::Style::UnlessElse

This cop looks for unless expressions with else clauses.

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/unless_else.rb, line 21
def autocorrect(node)
  condition, = *node
  body_range = range_between_condition_and_else(node, condition)
  else_range = range_between_else_and_end(node)

  lambda do |corrector|
    corrector.replace(node.loc.keyword, 'if'.freeze)
    corrector.replace(body_range, else_range.source)
    corrector.replace(else_range, body_range.source)
  end
end
on_if(node) click to toggle source
# File lib/rubocop/cop/style/unless_else.rb, line 11
def on_if(node)
  loc = node.loc

  # discard ternary ops and modifier if/unless nodes
  return unless loc.respond_to?(:keyword) && loc.respond_to?(:else)
  return unless loc.keyword.is?('unless') && loc.else

  add_offense(node, :expression)
end
range_between_condition_and_else(node, condition) click to toggle source
# File lib/rubocop/cop/style/unless_else.rb, line 33
def range_between_condition_and_else(node, condition)
  Parser::Source::Range.new(node.source_range.source_buffer,
                            condition.source_range.end_pos,
                            node.loc.else.begin_pos)
end
range_between_else_and_end(node) click to toggle source
# File lib/rubocop/cop/style/unless_else.rb, line 39
def range_between_else_and_end(node)
  Parser::Source::Range.new(node.source_range.source_buffer,
                            node.loc.else.end_pos,
                            node.loc.end.begin_pos)
end