class RuboCop::AST::IfNode

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

Public Instance Methods

branches() click to toggle source

Returns an array of all the branches in the conditional statement.

@return [Array<Node>] an array of branch nodes

# File lib/rubocop/ast/node/if_node.rb, line 148
def branches
  branches = [if_branch]

  return branches unless else_branch

  other_branches = if elsif_conditional?
                     else_branch.branches
                   else
                     [else_branch]
                   end
  branches.concat(other_branches)
end
each_branch() { |branch| ... } click to toggle source

Calls the given block for each branch node in the conditional statement. If no block is given, an `Enumerator` is returned.

@return [self] if a block is given @return [Enumerator] if no block is given

# File lib/rubocop/ast/node/if_node.rb, line 166
def each_branch
  return branches.to_enum(__method__) unless block_given?

  branches.each do |branch|
    yield branch
  end
end
else?() click to toggle source

Checks whether the `if` node has an `else` clause.

@note This returns `true` for nodes containing an `elsif` clause.

This is legacy behavior, and many cops rely on it.

@return [Boolean] whether the node has an `else` clause

# File lib/rubocop/ast/node/if_node.rb, line 42
def else?
  loc.respond_to?(:else) && loc.else
end
else_branch() click to toggle source

Returns the branch of the `if` node that gets evaluated when its condition is falsey.

@note This is normalized for `unless` nodes.

@return [Node] the falsey branch node of the `if` node @return [nil] when there is no else branch

# File lib/rubocop/ast/node/if_node.rb, line 127
def else_branch
  node_parts[2]
end
elsif?() click to toggle source

Checks whether the `if` is an `elsif`. Parser handles these by nesting `if` nodes in the `else` branch.

@return [Boolean] whether the node is an `elsif`

# File lib/rubocop/ast/node/if_node.rb, line 32
def elsif?
  keyword == 'elsif'
end
elsif_conditional?() click to toggle source

Checks whether the `if` node has at least one `elsif` branch. Returns true if this `if` node itself is an `elsif`.

@return [Boolean] whether the `if` node has at least one `elsif` branch

# File lib/rubocop/ast/node/if_node.rb, line 105
def elsif_conditional?
  else_branch&.if_type? && else_branch&.elsif?
end
if?() click to toggle source

Checks whether this node is an `if` statement. (This is not true of ternary operators and `unless` statements.)

@return [Boolean] whether the node is an `if` statement

# File lib/rubocop/ast/node/if_node.rb, line 16
def if?
  keyword == 'if'
end
if_branch() click to toggle source

Returns the branch of the `if` node that gets evaluated when its condition is truthy.

@note This is normalized for `unless` nodes.

@return [Node] the truthy branch node of the `if` node @return [nil] if the truthy branch is empty

# File lib/rubocop/ast/node/if_node.rb, line 116
def if_branch
  node_parts[1]
end
inverse_keyword() click to toggle source

Returns the inverse keyword of the `if` node as a string. Returns `if` for `unless` nodes and vice versa. Returns an empty string for ternary operators.

@return [String] the inverse keyword of the `if` statement

# File lib/rubocop/ast/node/if_node.rb, line 66
def inverse_keyword
  if keyword == 'if'
    'unless'
  elsif keyword == 'unless'
    'if'
  else
    ''
  end
end
keyword() click to toggle source

Returns the keyword of the `if` statement as a string. Returns an empty string for ternary operators.

@return [String] the keyword of the `if` statement

# File lib/rubocop/ast/node/if_node.rb, line 57
def keyword
  ternary? ? '' : loc.keyword.source
end
modifier_form?() click to toggle source

Checks whether the `if` node is in a modifier form, i.e. a condition trailing behind an expression. Only `if` and `unless` nodes without other branches can be modifiers.

@return [Boolean] whether the `if` node is a modifier

# File lib/rubocop/ast/node/if_node.rb, line 81
def modifier_form?
  (if? || unless?) && super
end
nested_conditional?() click to toggle source

Chacks whether the `if` node has nested `if` nodes in any of its branches.

@note This performs a shallow search.

@return [Boolean] whether the `if` node contains nested conditionals

# File lib/rubocop/ast/node/if_node.rb, line 91
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
node_parts() click to toggle source

Custom destructuring method. This is used to normalize the branches for `if` and `unless` nodes, to aid comparisons and conversions.

@return [Array<Node>] the different parts of the `if` statement

# File lib/rubocop/ast/node/if_node.rb, line 135
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
ternary?() click to toggle source

Checks whether the `if` node is a ternary operator.

@return [Boolean] whether the `if` node is a ternary operator

# File lib/rubocop/ast/node/if_node.rb, line 49
def ternary?
  loc.respond_to?(:question)
end
unless?() click to toggle source

Checks whether this node is an `unless` statement. (This is not true of ternary operators and `if` statements.)

@return [Boolean] whether the node is an `unless` statement

# File lib/rubocop/ast/node/if_node.rb, line 24
def unless?
  keyword == 'unless'
end