Class: Prism::WhenNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents the use of the ‘when` keyword within a case statement.

case true
when true
^^^^^^^^^
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements) ⇒ WhenNode

Initialize a new WhenNode node.



18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
# File 'lib/prism/node.rb', line 18054

def initialize(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @keyword_loc = keyword_loc
  @conditions = conditions
  @then_keyword_loc = then_keyword_loc
  @statements = statements
end

Instance Attribute Details

#conditionsObject (readonly)

attr_reader conditions: Array



18115
18116
18117
# File 'lib/prism/node.rb', line 18115

def conditions
  @conditions
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



18137
18138
18139
# File 'lib/prism/node.rb', line 18137

def statements
  @statements
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



18160
18161
18162
# File 'lib/prism/node.rb', line 18160

def self.type
  :when_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



18166
18167
18168
18169
18170
18171
18172
18173
# File 'lib/prism/node.rb', line 18166

def ===(other)
  other.is_a?(WhenNode) &&
    (keyword_loc.nil? == other.keyword_loc.nil?) &&
    (conditions.length == other.conditions.length) &&
    conditions.zip(other.conditions).all? { |left, right| left === right } &&
    (then_keyword_loc.nil? == other.then_keyword_loc.nil?) &&
    (statements === other.statements)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



18066
18067
18068
# File 'lib/prism/node.rb', line 18066

def accept(visitor)
  visitor.visit_when_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



18071
18072
18073
# File 'lib/prism/node.rb', line 18071

def child_nodes
  [*conditions, statements]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



18084
18085
18086
# File 'lib/prism/node.rb', line 18084

def comment_targets
  [keyword_loc, *conditions, *then_keyword_loc, *statements] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



18076
18077
18078
18079
18080
18081
# File 'lib/prism/node.rb', line 18076

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact.concat(conditions)
  compact << statements if statements
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, conditions: self.conditions, then_keyword_loc: self.then_keyword_loc, statements: self.statements) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?keyword_loc: Location, ?conditions: Array, ?then_keyword_loc: Location?, ?statements: StatementsNode?) -> WhenNode



18089
18090
18091
# File 'lib/prism/node.rb', line 18089

def copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, conditions: self.conditions, then_keyword_loc: self.then_keyword_loc, statements: self.statements)
  WhenNode.new(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, keyword_loc: Location, conditions: Array, then_keyword_loc: Location?, statements: StatementsNode? }



18097
18098
18099
# File 'lib/prism/node.rb', line 18097

def deconstruct_keys(keys)
  { node_id: node_id, location: location, keyword_loc: keyword_loc, conditions: conditions, then_keyword_loc: then_keyword_loc, statements: statements }
end

#inspectObject

def inspect -> String



18150
18151
18152
# File 'lib/prism/node.rb', line 18150

def inspect
  InspectVisitor.compose(self)
end

#keywordObject

def keyword: () -> String



18140
18141
18142
# File 'lib/prism/node.rb', line 18140

def keyword
  keyword_loc.slice
end

#keyword_locObject

attr_reader keyword_loc: Location



18102
18103
18104
18105
18106
# File 'lib/prism/node.rb', line 18102

def keyword_loc
  location = @keyword_loc
  return location if location.is_a?(Location)
  @keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#save_keyword_loc(repository) ⇒ Object

Save the keyword_loc location using the given saved source so that it can be retrieved later.



18110
18111
18112
# File 'lib/prism/node.rb', line 18110

def save_keyword_loc(repository)
  repository.enter(node_id, :keyword_loc)
end

#save_then_keyword_loc(repository) ⇒ Object

Save the then_keyword_loc location using the given saved source so that it can be retrieved later.



18132
18133
18134
# File 'lib/prism/node.rb', line 18132

def save_then_keyword_loc(repository)
  repository.enter(node_id, :then_keyword_loc) unless @then_keyword_loc.nil?
end

#then_keywordObject

def then_keyword: () -> String?



18145
18146
18147
# File 'lib/prism/node.rb', line 18145

def then_keyword
  then_keyword_loc&.slice
end

#then_keyword_locObject

attr_reader then_keyword_loc: Location?



18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
# File 'lib/prism/node.rb', line 18118

def then_keyword_loc
  location = @then_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @then_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



18155
18156
18157
# File 'lib/prism/node.rb', line 18155

def type
  :when_node
end