class RuboCop::AST::IfNode

to all ‘if` nodes within RuboCop.
node when the builder constructs the AST, making its methods available
A node extension for `if` nodes. This will be used in place of a plain

def branches

Returns:
  • (Array) - an array of branch nodes
def branches
  branches = [if_branch]
  return branches unless else?
  other_branches = if elsif_conditional?
                     else_branch.branches
                   else
                     [else_branch]
                   end
  branches.concat(other_branches)
end

def each_branch(&block)

Deprecated:
  • Use `branches.each`
def each_branch(&block)
  return branches.to_enum(__method__) unless block_given?
  branches.each(&block)
end

def else?

Returns:
  • (Boolean) - whether the node has an `else` clause

Other tags:
    Note: - This returns `true` for nodes containing an `elsif` clause.
def else?
  loc.respond_to?(:else) && loc.else
end

def else_branch

Returns:
  • (nil) - when there is no else branch
  • (Node) - the falsey branch node of the `if` node

Other tags:
    Note: - This is normalized for `unless` nodes.
def else_branch
  node_parts[2]
end

def elsif?

Returns:
  • (Boolean) - whether the node is an `elsif`
def elsif?
  keyword == 'elsif'
end

def elsif_conditional?

Returns:
  • (Boolean) - whether the `if` node has at least one `elsif` branch
def elsif_conditional?
  else_branch&.if_type? && else_branch&.elsif?
end

def if?

Returns:
  • (Boolean) - whether the node is an `if` statement
def if?
  keyword == 'if'
end

def if_branch

Returns:
  • (nil) - if the truthy branch is empty
  • (Node) - the truthy branch node of the `if` node

Other tags:
    Note: - This is normalized for `unless` nodes.
def if_branch
  node_parts[1]
end

def inverse_keyword

Returns:
  • (String) - the inverse keyword of the `if` statement
def inverse_keyword
  case keyword
  when 'if' then 'unless'
  when 'unless' then 'if'
  else
    ''
  end
end

def keyword

Returns:
  • (String) - the keyword of the `if` statement
def keyword
  ternary? ? '' : loc.keyword.source
end

def modifier_form?

Returns:
  • (Boolean) - whether the `if` node is a modifier
def modifier_form?
  (if? || unless?) && super
end

def nested_conditional?

Returns:
  • (Boolean) - whether the `if` node contains nested conditionals

Other tags:
    Note: - This performs a shallow search.
def nested_conditional?
  node_parts[1..2].compact.each do |branch|
    branch.each_node(:if) do |nested|
      return true unless nested.elsif?
    end
  end
  false
end

def node_parts

Returns:
  • (Array) - the different parts of the `if` statement
def node_parts
  if unless?
    condition, false_branch, true_branch = *self
  else
    condition, true_branch, false_branch = *self
  end
  [condition, true_branch, false_branch]
end

def ternary?

Returns:
  • (Boolean) - whether the `if` node is a ternary operator
def ternary?
  loc.respond_to?(:question)
end

def unless?

Returns:
  • (Boolean) - whether the node is an `unless` statement
def unless?
  keyword == 'unless'
end