class RuboCop::Cop::Style::NestedModifier

Checks for nested use of if, unless, while and until in their modifier form.

@example

# bad
something if a if b

# good
something if b && a

Constants

MSG

Public Instance Methods

on_if(node)
Alias for: on_while
on_until(node)
Alias for: on_while
on_while(node) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 22
def on_while(node)
  check(node)
end
Also aliased as: on_until, on_if

Private Instance Methods

add_parentheses_to_method_arguments(send_node) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 85
def add_parentheses_to_method_arguments(send_node)
  expr = +''
  expr << "#{send_node.receiver.source}." if send_node.receiver
  expr << send_node.method_name.to_s
  expr << "(#{send_node.arguments.map(&:source).join(', ')})"

  expr
end
autocorrect(corrector, node) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 42
def autocorrect(corrector, node)
  return unless node.if_type? && node.parent.if_type?

  range = range_between(node.loc.keyword.begin_pos,
                        node.parent.condition.source_range.end_pos)

  corrector.replace(range, new_expression(node))
end
check(node) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 30
def check(node)
  return if part_of_ignored_node?(node)
  return unless modifier?(node) && modifier?(node.parent)

  add_offense(node.loc.keyword) { |corrector| autocorrect(corrector, node) }
  ignore_node(node)
end
left_hand_operand(node, operator) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 65
def left_hand_operand(node, operator)
  expr = node.condition.source
  expr = "(#{expr})" if node.condition.or_type? && operator == '&&'
  expr
end
modifier?(node) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 38
def modifier?(node)
  node&.basic_conditional? && node.modifier_form?
end
new_expression(inner_node) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 51
def new_expression(inner_node)
  outer_node = inner_node.parent

  operator = replacement_operator(outer_node.keyword)
  lh_operand = left_hand_operand(outer_node, operator)
  rh_operand = right_hand_operand(inner_node, outer_node.keyword)

  "#{outer_node.keyword} #{lh_operand} #{operator} #{rh_operand}"
end
replacement_operator(keyword) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 61
def replacement_operator(keyword)
  keyword == 'if' ? '&&' : '||'
end
requires_parens?(node) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 94
def requires_parens?(node)
  node.or_type? || !(RuboCop::AST::Node::COMPARISON_OPERATORS & node.children).empty?
end
right_hand_operand(node, left_hand_keyword) click to toggle source
# File lib/rubocop/cop/style/nested_modifier.rb, line 71
def right_hand_operand(node, left_hand_keyword)
  condition = node.condition

  expr = if condition.send_type? && !condition.arguments.empty? &&
            !condition.operator_method?
           add_parentheses_to_method_arguments(condition)
         else
           condition.source
         end
  expr = "(#{expr})" if requires_parens?(condition)
  expr = "!#{expr}" unless left_hand_keyword == node.keyword
  expr
end