Module: RuboCop::Cop::IfNode

Overview

Common functionality for checking if nodes.

Instance Method Summary collapse

Instance Method Details

#elsif?(node) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/rubocop/cop/mixin/if_node.rb', line 18

def elsif?(node)
  node.loc.respond_to?(:keyword) && node.loc.keyword &&
    node.loc.keyword.is?('elsif')
end

#if_else?(node) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rubocop/cop/mixin/if_node.rb', line 23

def if_else?(node)
  node.loc.respond_to?(:else) && node.loc.else
end

#if_node_parts(node) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/rubocop/cop/mixin/if_node.rb', line 27

def if_node_parts(node)
  case node.loc.keyword.source
  when 'if', 'elsif' then condition, body, else_clause = *node
  when 'unless'      then condition, else_clause, body = *node
  else                    condition, body = *node
  end

  [condition, body, else_clause]
end

#modifier_if?(node) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
# File 'lib/rubocop/cop/mixin/if_node.rb', line 8

def modifier_if?(node)
  node.loc.respond_to?(:keyword) &&
    %w(if unless).include?(node.loc.keyword.source) &&
    node.loc.respond_to?(:end) && node.loc.end.nil?
end

#ternary_op?(node) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rubocop/cop/mixin/if_node.rb', line 14

def ternary_op?(node)
  node.loc.respond_to?(:question)
end