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


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

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

#if_else_clause(node) ⇒ Object


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

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


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

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


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

def on_if(node)
  invoke_hook_for_normal_if_unless(node)
end