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
-
(Array
- an array of branch nodes)
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
def each_branch(&block)
- Use `branches.each`
def each_branch(&block) return branches.to_enum(__method__) unless block branches.each(&block) end
def else?
-
(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
-
(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?
-
(Boolean)
- whether the node is an `elsif`
def elsif? keyword == 'elsif' end
def elsif_conditional?
-
(Boolean)
- whether the `if` node has at least one `elsif` branch
def elsif_conditional? else_branch&.if_type? && else_branch.elsif? end
def if?
-
(Boolean)
- whether the node is an `if` statement
def if? keyword == 'if' end
def if_branch
-
(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
-
(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
-
(String)
- the keyword of the `if` statement
def keyword ternary? ? '' : loc.keyword.source end
def modifier_form?
-
(Boolean)
- whether the `if` node is a modifier
def modifier_form? (if? || unless?) && super end
def nested_conditional?
-
(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
-
(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?
-
(Boolean)
- whether the `if` node is a ternary operator
def ternary? loc.respond_to?(:question) end
def then?
-
(Boolean)
- whether the node has an `then` clause
def then? loc_is?(:begin, 'then') end
def unless?
-
(Boolean)
- whether the node is an `unless` statement
def unless? keyword == 'unless' end