Class: RuboCop::AST::WhenNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rubocop/ast/node/when_node.rb

Overview

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

Constant Summary

Constants inherited from Node

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::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::REFERENCES, Node::SHORTHAND_ASSIGNMENTS, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES

Instance Method Summary collapse

Methods inherited from Node

#ancestors, #argument?, #assignment?, #basic_conditional?, #basic_literal?, #chained?, #child_nodes, #complete!, #complete?, #conditional?, #const_name, #defined_module, #defined_module_name, #descendants, #each_ancestor, #each_child_node, #each_descendant, #each_node, #empty_source?, #equals_asgn?, #falsey_literal?, #first_line, #immutable_literal?, #initialize, #keyword?, #last_line, #line_count, #literal?, #multiline?, #mutable_literal?, #node_parts, #nonempty_line_count, #numeric_type?, #operator_keyword?, #parent, #parent_module_name, #parenthesized_call?, #pure?, #receiver, #reference?, #shorthand_asgn?, #sibling_index, #single_line?, #source, #source_length, #source_range, #special_keyword?, #truthy_literal?, #updated, #value_used?, #variable?

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search, #node_search, #node_search_all, #node_search_body, #node_search_first

Methods included from Sexp

#s

Constructor Details

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

Instance Method Details

#bodyNode?

Returns the body of the ‘when` node.

Returns:

  • (Node, nil)

    the body of the ‘when` node



48
49
50
# File 'lib/rubocop/ast/node/when_node.rb', line 48

def body
  node_parts[-1]
end

#branch_indexInteger

Returns the index of the ‘when` branch within the `case` statement.

Returns:

  • (Integer)

    the index of the ‘when` branch



34
35
36
# File 'lib/rubocop/ast/node/when_node.rb', line 34

def branch_index
  parent.when_branches.index(self)
end

#conditionsArray<Node>

Returns an array of all the conditions in the ‘when` branch.

Returns:

  • (Array<Node>)

    an array of condition nodes



12
13
14
# File 'lib/rubocop/ast/node/when_node.rb', line 12

def conditions
  node_parts[0...-1]
end

#each_conditionself, Enumerator

Calls the given block for each condition node in the ‘when` branch. If no block is given, an `Enumerator` is returned.

Returns:

  • (self)

    if a block is given

  • (Enumerator)

    if no block is given



21
22
23
24
25
26
27
28
29
# File 'lib/rubocop/ast/node/when_node.rb', line 21

def each_condition
  return conditions.to_enum(__method__) unless block_given?

  conditions.each do |condition|
    yield condition
  end

  self
end

#then?Boolean

Checks whether the ‘when` node has a `then` keyword.

Returns:

  • (Boolean)

    whether the ‘when` node has a `then` keyword



41
42
43
# File 'lib/rubocop/ast/node/when_node.rb', line 41

def then?
  loc.begin && loc.begin.is?('then')
end