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.



16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
# File 'lib/prism/node.rb', line 16157

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



16212
16213
16214
# File 'lib/prism/node.rb', line 16212

def conditions
  @conditions
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



16228
16229
16230
# File 'lib/prism/node.rb', line 16228

def statements
  @statements
end

Class Method Details

.typeObject

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



16251
16252
16253
# File 'lib/prism/node.rb', line 16251

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.



16257
16258
16259
16260
16261
16262
16263
16264
# File 'lib/prism/node.rb', line 16257

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



16169
16170
16171
# File 'lib/prism/node.rb', line 16169

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



16174
16175
16176
# File 'lib/prism/node.rb', line 16174

def child_nodes
  [*conditions, statements]
end

#comment_targetsObject

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



16187
16188
16189
# File 'lib/prism/node.rb', line 16187

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



16179
16180
16181
16182
16183
16184
# File 'lib/prism/node.rb', line 16179

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



16192
16193
16194
# File 'lib/prism/node.rb', line 16192

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



16200
16201
16202
# File 'lib/prism/node.rb', line 16200

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



16241
16242
16243
# File 'lib/prism/node.rb', line 16241

def inspect
  InspectVisitor.compose(self)
end

#keywordObject

def keyword: () -> String



16231
16232
16233
# File 'lib/prism/node.rb', line 16231

def keyword
  keyword_loc.slice
end

#keyword_locObject

attr_reader keyword_loc: Location



16205
16206
16207
16208
16209
# File 'lib/prism/node.rb', line 16205

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

#then_keywordObject

def then_keyword: () -> String?



16236
16237
16238
# File 'lib/prism/node.rb', line 16236

def then_keyword
  then_keyword_loc&.slice
end

#then_keyword_locObject

attr_reader then_keyword_loc: Location?



16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
# File 'lib/prism/node.rb', line 16215

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`.



16246
16247
16248
# File 'lib/prism/node.rb', line 16246

def type
  :when_node
end