Class: RuboCop::AST::IfNode
- 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::BASIC_LITERALS, Node::COMPARISON_OPERATORS, Node::COMPOSITE_LITERALS, Node::FALSEY_LITERALS, Node::IMMUTABLE_LITERALS, Node::KEYWORDS, Node::LITERALS, Node::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::REFERENCES, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES
Instance Method Summary collapse
-
#else? ⇒ Boolean
Checks whether the
ifnode has anelseclause. -
#else_branch ⇒ Node
Returns the branch of the
ifnode that gets evaluated when its condition is falsey. -
#elsif? ⇒ Boolean
Checks whether the
ifis anelsif. -
#if? ⇒ Boolean
Checks whether this node is an
ifstatement. -
#if_branch ⇒ Node
Returns the branch of the
ifnode that gets evaluated when its condition is truthy. -
#inverse_keyword ⇒ String
Returns the inverse keyword of the
ifnode as a string. -
#keyword ⇒ String
Returns the keyword of the
ifstatement as a string. -
#modifier_form? ⇒ Boolean
Checks whether the
ifnode is in a modifier form, i.e. -
#nested_conditional? ⇒ Boolean
Chackes whether the
ifnode has nestedifnodes in any of its branches. -
#node_parts ⇒ Array<Node>
Custom destructuring method.
-
#ternary? ⇒ Boolean
Checks whether the
ifnode is a ternary operator. -
#unless? ⇒ Boolean
Checks whether this node is an
unlessstatement.
Methods included from ConditionalNode
#body, #condition, #multiline_condition?, #single_line_condition?
Methods inherited from Node
#ancestors, #argument?, #asgn_method_call?, #basic_literal?, #binary_operation?, #chained?, #child_nodes, #complete!, #complete?, #const_name, def_matcher, #defined_module, #defined_module_name, #descendants, #each_ancestor, #each_child_node, #each_descendant, #each_node, #falsey_literal?, #immutable_literal?, #initialize, #keyword?, #keyword_bang?, #keyword_not?, #literal?, #multiline?, #mutable_literal?, #numeric_type?, #parent, #parent_module_name, #pure?, #receiver, #reference?, #sibling_index, #single_line?, #source, #source_range, #special_keyword?, #truthy_literal?, #unary_operation?, #updated, #value_used?, #variable?
Methods included from Sexp
Constructor Details
This class inherits a constructor from RuboCop::AST::Node
Instance Method Details
#else? ⇒ Boolean
This returns true for nodes containing an elsif clause. This is legacy behaviour, and many cops rely on it.
Checks whether the if node has an else clause.
42 43 44 |
# File 'lib/rubocop/ast/node/if_node.rb', line 42 def else? loc.respond_to?(:else) && loc.else end |
#else_branch ⇒ Node
This is normalized for unless nodes.
Returns the branch of the if node that gets evaluated when its condition is falsey.
117 118 119 |
# File 'lib/rubocop/ast/node/if_node.rb', line 117 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.
32 33 34 |
# File 'lib/rubocop/ast/node/if_node.rb', line 32 def elsif? keyword == 'elsif' end |
#if? ⇒ Boolean
Checks whether this node is an if statement. (This is not true of ternary operators and unless statements.)
16 17 18 |
# File 'lib/rubocop/ast/node/if_node.rb', line 16 def if? keyword == 'if' end |
#if_branch ⇒ Node
This is normalized for unless nodes.
Returns the branch of the if node that gets evaluated when its condition is truthy.
107 108 109 |
# File 'lib/rubocop/ast/node/if_node.rb', line 107 def if_branch node_parts[1] end |
#inverse_keyword ⇒ String
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.
66 67 68 69 70 71 72 73 74 |
# 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 ⇒ String
Returns the keyword of the if statement as a string. Returns an empty string for ternary operators.
57 58 59 |
# File 'lib/rubocop/ast/node/if_node.rb', line 57 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.
81 82 83 |
# File 'lib/rubocop/ast/node/if_node.rb', line 81 def modifier_form? (if? || unless?) && super end |
#nested_conditional? ⇒ Boolean
This is a shallow search.
Chackes whether the if node has nested if nodes in any of its branches.
91 92 93 94 95 96 97 98 99 |
# 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 ⇒ Array<Node>
Custom destructuring method. This is used to normalize the branches for if and unless nodes, to aid comparisons and conversions.
125 126 127 128 129 130 131 132 133 |
# File 'lib/rubocop/ast/node/if_node.rb', line 125 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.
49 50 51 |
# File 'lib/rubocop/ast/node/if_node.rb', line 49 def ternary? loc.respond_to?(:question) end |
#unless? ⇒ Boolean
Checks whether this node is an unless statement. (This is not true of ternary operators and if statements.)
24 25 26 |
# File 'lib/rubocop/ast/node/if_node.rb', line 24 def unless? keyword == 'unless' end |