Class: RuboCop::Cop::Ipepe::TernaryOperator

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/ipepe/ternary_operator.rb

Constant Summary collapse

MSG =
"Use `if` instead of ternary operator.".freeze

Instance Method Summary collapse

Instance Method Details

#on_if(node) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rubocop/cop/ipepe/ternary_operator.rb', line 9

def on_if(node)
  return unless node.ternary?

  add_offense(node) do |corrector|
    corrector.replace(
      node,
      [
        "if #{node.condition.source}",
        node.if_branch.source,
        "else",
        node.else_branch.source,
        "end"
      ].join("\n")
    )
  end
end