Class: Prism::RescueNode

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

Overview

Represents a rescue statement.

begin
rescue Foo, *splat, Bar => ex
  foo
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
end

‘Foo, *splat, Bar` are in the `exceptions` field. `ex` is in the `exception` field.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, keyword_loc, exceptions, operator_loc, reference, statements, subsequent) ⇒ RescueNode

Initialize a new RescueNode node.



14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
# File 'lib/prism/node.rb', line 14188

def initialize(source, node_id, location, flags, keyword_loc, exceptions, operator_loc, reference, statements, subsequent)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @keyword_loc = keyword_loc
  @exceptions = exceptions
  @operator_loc = operator_loc
  @reference = reference
  @statements = statements
  @subsequent = subsequent
end

Instance Attribute Details

#exceptionsObject (readonly)

attr_reader exceptions: Array



14247
14248
14249
# File 'lib/prism/node.rb', line 14247

def exceptions
  @exceptions
end

#referenceObject (readonly)

attr_reader reference: Prism::node?



14263
14264
14265
# File 'lib/prism/node.rb', line 14263

def reference
  @reference
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



14266
14267
14268
# File 'lib/prism/node.rb', line 14266

def statements
  @statements
end

#subsequentObject (readonly)

attr_reader subsequent: RescueNode?



14269
14270
14271
# File 'lib/prism/node.rb', line 14269

def subsequent
  @subsequent
end

Class Method Details

.typeObject

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



14292
14293
14294
# File 'lib/prism/node.rb', line 14292

def self.type
  :rescue_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.



14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
# File 'lib/prism/node.rb', line 14298

def ===(other)
  other.is_a?(RescueNode) &&
    (keyword_loc.nil? == other.keyword_loc.nil?) &&
    (exceptions.length == other.exceptions.length) &&
    exceptions.zip(other.exceptions).all? { |left, right| left === right } &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (reference === other.reference) &&
    (statements === other.statements) &&
    (subsequent === other.subsequent)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



14202
14203
14204
# File 'lib/prism/node.rb', line 14202

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

#child_nodesObject Also known as: deconstruct

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



14207
14208
14209
# File 'lib/prism/node.rb', line 14207

def child_nodes
  [*exceptions, reference, statements, subsequent]
end

#comment_targetsObject

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



14222
14223
14224
# File 'lib/prism/node.rb', line 14222

def comment_targets
  [keyword_loc, *exceptions, *operator_loc, *reference, *statements, *subsequent] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



14212
14213
14214
14215
14216
14217
14218
14219
# File 'lib/prism/node.rb', line 14212

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

#consequentObject

Returns the subsequent rescue clause of the rescue node. This method is deprecated in favor of #subsequent.



494
495
496
497
# File 'lib/prism/node_ext.rb', line 494

def consequent
  deprecated("subsequent")
  subsequent
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, exceptions: self.exceptions, operator_loc: self.operator_loc, reference: self.reference, statements: self.statements, subsequent: self.subsequent) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?keyword_loc: Location, ?exceptions: Array, ?operator_loc: Location?, ?reference: Prism::node?, ?statements: StatementsNode?, ?subsequent: RescueNode?) -> RescueNode



14227
14228
14229
# File 'lib/prism/node.rb', line 14227

def copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, exceptions: self.exceptions, operator_loc: self.operator_loc, reference: self.reference, statements: self.statements, subsequent: self.subsequent)
  RescueNode.new(source, node_id, location, flags, keyword_loc, exceptions, operator_loc, reference, statements, subsequent)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, keyword_loc: Location, exceptions: Array, operator_loc: Location?, reference: Prism::node?, statements: StatementsNode?, subsequent: RescueNode? }



14235
14236
14237
# File 'lib/prism/node.rb', line 14235

def deconstruct_keys(keys)
  { node_id: node_id, location: location, keyword_loc: keyword_loc, exceptions: exceptions, operator_loc: operator_loc, reference: reference, statements: statements, subsequent: subsequent }
end

#inspectObject

def inspect -> String



14282
14283
14284
# File 'lib/prism/node.rb', line 14282

def inspect
  InspectVisitor.compose(self)
end

#keywordObject

def keyword: () -> String



14272
14273
14274
# File 'lib/prism/node.rb', line 14272

def keyword
  keyword_loc.slice
end

#keyword_locObject

attr_reader keyword_loc: Location



14240
14241
14242
14243
14244
# File 'lib/prism/node.rb', line 14240

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

#operatorObject

def operator: () -> String?



14277
14278
14279
# File 'lib/prism/node.rb', line 14277

def operator
  operator_loc&.slice
end

#operator_locObject

attr_reader operator_loc: Location?



14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
# File 'lib/prism/node.rb', line 14250

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

#typeObject

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



14287
14288
14289
# File 'lib/prism/node.rb', line 14287

def type
  :rescue_node
end