class RuboCop::Cop::Style::OneLineConditional

Checks for uses of if/then/else/end constructs on a single line. AlwaysCorrectToMultiline config option can be set to true to auto-convert all offenses to multi-line constructs. When AlwaysCorrectToMultiline is false (default case) the autocorrect will first try converting them to ternary operators.

@example

# bad
if foo then bar else baz end

# bad
unless foo then baz else bar end

# good
foo ? bar : baz

# good
bar if foo

# good
if foo then bar end

# good
if foo
  bar
else
  baz
end

Constants

MSG

Public Instance Methods

on_normal_if_unless(node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 41
def on_normal_if_unless(node)
  return unless node.single_line?
  return unless node.else_branch
  return if node.elsif?

  message = message(node)
  add_offense(node, message: message) do |corrector|
    autocorrect(corrector, node)
  end
end

Private Instance Methods

always_multiline?() click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 76
def always_multiline?
  @config.for_cop('Style/OneLineConditional')['AlwaysCorrectToMultiline']
end
autocorrect(corrector, node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 58
def autocorrect(corrector, node)
  if always_multiline? || cannot_replace_to_ternary?(node)
    IfThenCorrector.new(node, indentation: indentation_width).call(corrector)
  else
    corrector.replace(node, ternary_correction(node))
  end
end
cannot_replace_to_ternary?(node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 80
def cannot_replace_to_ternary?(node)
  node.elsif_conditional?
end
expr_replacement(node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 92
def expr_replacement(node)
  return 'nil' if node.nil?

  requires_parentheses?(node) ? "(#{node.source})" : node.source
end
indentation_width() click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 120
def indentation_width
  @config.for_cop('Layout/IndentationWidth')['Width']
end
keyword_with_changed_precedence?(node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 113
def keyword_with_changed_precedence?(node)
  return false unless node.keyword?
  return true if node.respond_to?(:prefix_not?) && node.prefix_not?

  node.respond_to?(:arguments?) && node.arguments? && !node.parenthesized_call?
end
message(node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 54
def message(node)
  format(MSG, keyword: node.keyword)
end
method_call_with_changed_precedence?(node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 106
def method_call_with_changed_precedence?(node)
  return false unless node.send_type? && node.arguments?
  return false if node.parenthesized_call?

  !node.operator_method?
end
requires_parentheses?(node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 98
def requires_parentheses?(node)
  return true if %i[and or if].include?(node.type)
  return true if node.assignment?
  return true if method_call_with_changed_precedence?(node)

  keyword_with_changed_precedence?(node)
end
ternary_correction(node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 66
def ternary_correction(node)
  replaced_node = ternary_replacement(node)

  return replaced_node unless node.parent
  return "(#{replaced_node})" if %i[and or].include?(node.parent.type)
  return "(#{replaced_node})" if node.parent.send_type? && node.parent.operator_method?

  replaced_node
end
ternary_replacement(node) click to toggle source
# File lib/rubocop/cop/style/one_line_conditional.rb, line 84
def ternary_replacement(node)
  condition, if_branch, else_branch = *node

  "#{expr_replacement(condition)} ? " \
    "#{expr_replacement(if_branch)} : " \
    "#{expr_replacement(else_branch)}"
end