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.



16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
# File 'lib/prism/node.rb', line 16194

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



16249
16250
16251
# File 'lib/prism/node.rb', line 16249

def conditions
  @conditions
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



16265
16266
16267
# File 'lib/prism/node.rb', line 16265

def statements
  @statements
end

Class Method Details

.typeObject

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



16288
16289
16290
# File 'lib/prism/node.rb', line 16288

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.



16294
16295
16296
16297
16298
16299
16300
16301
# File 'lib/prism/node.rb', line 16294

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



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

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

#child_nodesObject Also known as: deconstruct

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



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

def child_nodes
  [*conditions, statements]
end

#comment_targetsObject

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



16224
16225
16226
# File 'lib/prism/node.rb', line 16224

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



16216
16217
16218
16219
16220
16221
# File 'lib/prism/node.rb', line 16216

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



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

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



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

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



16278
16279
16280
# File 'lib/prism/node.rb', line 16278

def inspect
  InspectVisitor.compose(self)
end

#keywordObject

def keyword: () -> String



16268
16269
16270
# File 'lib/prism/node.rb', line 16268

def keyword
  keyword_loc.slice
end

#keyword_locObject

attr_reader keyword_loc: Location



16242
16243
16244
16245
16246
# File 'lib/prism/node.rb', line 16242

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?



16273
16274
16275
# File 'lib/prism/node.rb', line 16273

def then_keyword
  then_keyword_loc&.slice
end

#then_keyword_locObject

attr_reader then_keyword_loc: Location?



16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
# File 'lib/prism/node.rb', line 16252

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



16283
16284
16285
# File 'lib/prism/node.rb', line 16283

def type
  :when_node
end