class RuboCop::Cop::Style::EmptyCaseCondition

Checks for case statements with an empty condition.

@example

# bad:
case
when x == 0
  puts 'x is 0'
when y == 0
  puts 'y is 0'
else
  puts 'neither is 0'
end

# good:
if x == 0
  puts 'x is 0'
elsif y == 0
  puts 'y is 0'
else
  puts 'neither is 0'
end

# good: (the case condition node is not empty)
case n
when 0
  puts 'zero'
when 1
  puts 'one'
else
  puts 'more'
end

Constants

MSG

Public Instance Methods

on_case(case_node) click to toggle source
# File lib/rubocop/cop/style/empty_case_condition.rb, line 44
def on_case(case_node)
  return if case_node.condition

  branch_bodies = [*case_node.when_branches.map(&:body), case_node.else_branch].compact

  return if branch_bodies.any? do |body|
    body.return_type? || body.each_descendant.any?(&:return_type?)
  end

  add_offense(case_node.loc.keyword) { |corrector| autocorrect(corrector, case_node) }
end

Private Instance Methods

autocorrect(corrector, case_node) click to toggle source
# File lib/rubocop/cop/style/empty_case_condition.rb, line 58
def autocorrect(corrector, case_node)
  when_branches = case_node.when_branches

  correct_case_when(corrector, case_node, when_branches)
  correct_when_conditions(corrector, when_branches)
end
correct_case_when(corrector, case_node, when_nodes) click to toggle source
# File lib/rubocop/cop/style/empty_case_condition.rb, line 65
def correct_case_when(corrector, case_node, when_nodes)
  case_range = case_node.loc.keyword.join(when_nodes.first.loc.keyword)

  corrector.replace(case_range, 'if')

  keep_first_when_comment(case_range, corrector)

  when_nodes[1..].each do |when_node|
    corrector.replace(when_node.loc.keyword, 'elsif')
  end
end
correct_when_conditions(corrector, when_nodes) click to toggle source
# File lib/rubocop/cop/style/empty_case_condition.rb, line 77
def correct_when_conditions(corrector, when_nodes)
  when_nodes.each do |when_node|
    conditions = when_node.conditions

    replace_then_with_line_break(corrector, conditions, when_node)

    next unless conditions.size > 1

    range = range_between(conditions.first.source_range.begin_pos,
                          conditions.last.source_range.end_pos)

    corrector.replace(range, conditions.map(&:source).join(' || '))
  end
end
keep_first_when_comment(case_range, corrector) click to toggle source
# File lib/rubocop/cop/style/empty_case_condition.rb, line 92
def keep_first_when_comment(case_range, corrector)
  indent = ' ' * case_range.column
  comments = processed_source.each_comment_in_lines(
    case_range.first_line...case_range.last_line
  ).map { |comment| "#{indent}#{comment.text}\n" }.join

  line_beginning = case_range.adjust(begin_pos: -case_range.column)
  corrector.insert_before(line_beginning, comments)
end
replace_then_with_line_break(corrector, conditions, when_node) click to toggle source
# File lib/rubocop/cop/style/empty_case_condition.rb, line 102
def replace_then_with_line_break(corrector, conditions, when_node)
  return unless when_node.parent.parent && when_node.then?

  range = range_between(conditions.last.source_range.end_pos, when_node.loc.begin.end_pos)

  corrector.replace(range, "\n")
end