Class: RuboCop::AST::IfNode

Inherits:
Node
  • Object
show all
Includes:
ConditionalNode, ModifierNode
Defined in:
lib/rubocop/ast/node/if_node.rb

Overview

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.

Constant Summary

Constants inherited from Node

Node::ARGUMENT_TYPES, Node::ASSIGNMENTS, Node::BASIC_CONDITIONALS, Node::BASIC_LITERALS, Node::COMPARISON_OPERATORS, Node::COMPOSITE_LITERALS, Node::CONDITIONALS, Node::EQUALS_ASSIGNMENTS, Node::FALSEY_LITERALS, Node::IMMUTABLE_LITERALS, Node::KEYWORDS, Node::LITERALS, Node::LOOP_TYPES, Node::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::POST_CONDITION_LOOP_TYPES, Node::REFERENCES, Node::SHORTHAND_ASSIGNMENTS, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES

Instance Method Summary collapse

Methods included from ConditionalNode

#body, #condition, #multiline_condition?, #single_line_condition?

Methods inherited from Node

#ancestors, #argument?, #argument_type?, #assignment?, #assignment_or_similar?, #basic_conditional?, #basic_literal?, #boolean_type?, #call_type?, #chained?, #class_constructor?, #class_definition?, #complete!, #complete?, #conditional?, #const_name, #defined_module, #defined_module_name, #each_ancestor, #empty_source?, #equals_asgn?, #falsey_literal?, #first_line, #global_const?, #guard_clause?, #immutable_literal?, #initialize, #keyword?, #lambda?, #lambda_or_proc?, #last_line, #left_sibling, #left_siblings, #line_count, #literal?, #loc_is?, #loop_keyword?, #match_guard_clause?, #module_definition?, #multiline?, #mutable_literal?, #nonempty_line_count, #numeric_type?, #operator_keyword?, #parent, #parent?, #parent_module_name, #parenthesized_call?, #post_condition_loop?, #proc?, #pure?, #range_type?, #receiver, #recursive_basic_literal?, #recursive_literal?, #reference?, #right_sibling, #right_siblings, #root?, #send_type?, #shorthand_asgn?, #sibling_index, #single_line?, #source, #source_length, #source_range, #special_keyword?, #str_content, #struct_constructor?, #truthy_literal?, #type?, #updated, #value_used?, #variable?

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search

Methods included from Descendence

#child_nodes, #descendants, #each_child_node, #each_descendant, #each_node

Methods included from Sexp

#s

Constructor Details

This class inherits a constructor from RuboCop::AST::Node

Instance Method Details

#branchesArray<Node>

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

Returns:

  • (Array<Node>)

    an array of branch nodes



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rubocop/ast/node/if_node.rb', line 154

def branches
  if ternary?
    [if_branch, else_branch]
  elsif !else?
    [if_branch]
  else
    branches = [if_branch]
    other_branches = if elsif_conditional?
                       else_branch.branches
                     else
                       [else_branch]
                     end
    branches.concat(other_branches)
  end
end

#each_branch(&block) ⇒ Object

Deprecated.

Use ‘branches.each`



171
172
173
174
175
# File 'lib/rubocop/ast/node/if_node.rb', line 171

def each_branch(&block)
  return branches.to_enum(__method__) unless block

  branches.each(&block)
end

#else?Boolean

Note:

This returns ‘true` for nodes containing an `elsif` clause. This is legacy behavior, and many cops rely on it.

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

Returns:

  • (Boolean)

    whether the node has an ‘else` clause



49
50
51
# File 'lib/rubocop/ast/node/if_node.rb', line 49

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

#else_branchNode?

Note:

This is normalized for ‘unless` nodes.

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

Returns:

  • (Node)

    the falsey branch node of the ‘if` node

  • (nil)

    when there is no else branch



133
134
135
# File 'lib/rubocop/ast/node/if_node.rb', line 133

def else_branch
  node_parts[2]
end

#elsif?Boolean

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

Returns:

  • (Boolean)

    whether the node is an ‘elsif`



39
40
41
# File 'lib/rubocop/ast/node/if_node.rb', line 39

def elsif?
  keyword == 'elsif'
end

#elsif_conditional?Boolean

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

Returns:

  • (Boolean)

    whether the ‘if` node has at least one `elsif` branch



111
112
113
# File 'lib/rubocop/ast/node/if_node.rb', line 111

def elsif_conditional?
  else_branch&.if_type? && else_branch.elsif?
end

#if?Boolean

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

Returns:

  • (Boolean)

    whether the node is an ‘if` statement



16
17
18
# File 'lib/rubocop/ast/node/if_node.rb', line 16

def if?
  keyword == 'if'
end

#if_branchNode?

Note:

This is normalized for ‘unless` nodes.

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

Returns:

  • (Node)

    the truthy branch node of the ‘if` node

  • (nil)

    if the truthy branch is empty



122
123
124
# File 'lib/rubocop/ast/node/if_node.rb', line 122

def if_branch
  node_parts[1]
end

#inverse_keywordString

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.

Returns:

  • (String)

    the inverse keyword of the ‘if` statement



73
74
75
76
77
78
79
80
# File 'lib/rubocop/ast/node/if_node.rb', line 73

def inverse_keyword
  case keyword
  when 'if' then 'unless'
  when 'unless' then 'if'
  else
    ''
  end
end

#keywordString

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

Returns:

  • (String)

    the keyword of the ‘if` statement



64
65
66
# File 'lib/rubocop/ast/node/if_node.rb', line 64

def keyword
  ternary? ? '' : loc.keyword.source
end

#modifier_form?Boolean

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.

Returns:

  • (Boolean)

    whether the ‘if` node is a modifier



87
88
89
# File 'lib/rubocop/ast/node/if_node.rb', line 87

def modifier_form?
  (if? || unless?) && super
end

#nested_conditional?Boolean

Note:

This performs a shallow search.

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

Returns:

  • (Boolean)

    whether the ‘if` node contains nested conditionals



97
98
99
100
101
102
103
104
105
# File 'lib/rubocop/ast/node/if_node.rb', line 97

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_partsArray<Node>

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

Returns:

  • (Array<Node>)

    the different parts of the ‘if` statement



141
142
143
144
145
146
147
148
149
# File 'lib/rubocop/ast/node/if_node.rb', line 141

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?Boolean

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

Returns:

  • (Boolean)

    whether the ‘if` node is a ternary operator



56
57
58
# File 'lib/rubocop/ast/node/if_node.rb', line 56

def ternary?
  loc.respond_to?(:question)
end

#then?Boolean

Checks whether the ‘if` node has an `then` clause.

Returns:

  • (Boolean)

    whether the node has an ‘then` clause



31
32
33
# File 'lib/rubocop/ast/node/if_node.rb', line 31

def then?
  loc_is?(:begin, 'then')
end

#unless?Boolean

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

Returns:

  • (Boolean)

    whether the node is an ‘unless` statement



24
25
26
# File 'lib/rubocop/ast/node/if_node.rb', line 24

def unless?
  keyword == 'unless'
end