class RuboCop::Cop::Style::IfInsideElse
end
action_b
elsif condition_b
action_a
if condition_a
# good
end
action_b if condition_b
else
action_a
if condition_a
# good
@example AllowIfModifier: true
end
action_b
elsif condition_b
action_a
if condition_a
# good
end
action_b if condition_b
else
action_a
if condition_a
# bad
@example AllowIfModifier: false (default)
end
action_c
else
action_b
elsif condition_b
action_a
if condition_a
# good
end
end
action_c
else
action_b
if condition_b
else
action_a
if condition_a
# bad
@example
This helps to keep the nesting level from getting too deep.
it can be combined with the ‘else` to become an `elsif`.
If the `else` branch of a conditional consists solely of an `if` node,
def allow_if_modifier?
def allow_if_modifier? cop_config['AllowIfModifier'] end
def allow_if_modifier_in_else_branch?(else_branch)
def allow_if_modifier_in_else_branch?(else_branch) allow_if_modifier? && else_branch&.modifier_form? end
def on_if(node)
def on_if(node) return if node.ternary? || node.unless? else_branch = node.else_branch return unless else_branch&.if_type? && else_branch&.if? return if allow_if_modifier_in_else_branch?(else_branch) add_offense(else_branch, location: :keyword) end