Module: RuboCop::Cop::OnNormalIfUnless

Includes:
IfNode
Included in:
Style::EmptyElse, Style::IfWithSemicolon, Style::MissingElse, Style::MultilineIfThen, Style::OneLineConditional
Defined in:
lib/rubocop/cop/mixin/on_normal_if_unless.rb

Overview

Common functionality for cops checking if and unless expressions.

Instance Method Summary collapse

Methods included from IfNode

#elsif?, #if_else?, #if_node_parts, #modifier_if?, #ternary_op?

Instance Method Details

#case_else_clause(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/mixin/on_normal_if_unless.rb', line 33

def case_else_clause(node)
  node.children.last if node.case_type?
end

#if_else_clause(node) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/mixin/on_normal_if_unless.rb', line 20

def if_else_clause(node)
  return unless node.if_type?

  keyword = node.loc.keyword
  if keyword.is?('if')
    node.children.last
  elsif keyword.is?('elsif')
    node.children.last
  elsif keyword.is?('unless')
    node.children[1]
  end
end

#invoke_hook_for_normal_if_unless(node) ⇒ Object



14
15
16
17
18
# File 'lib/rubocop/cop/mixin/on_normal_if_unless.rb', line 14

def invoke_hook_for_normal_if_unless(node)
  # We won't check modifier or ternary conditionals.
  return if modifier_if?(node) || ternary_op?(node)
  on_normal_if_unless(node)
end

#on_if(node) ⇒ Object



10
11
12
# File 'lib/rubocop/cop/mixin/on_normal_if_unless.rb', line 10

def on_if(node)
  invoke_hook_for_normal_if_unless(node)
end