123456789_123456789_123456789_123456789_123456789_

Class: Prism::RescueNode

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Node
Instance Chain:
self, Node
Inherits: Prism::Node
Defined in: lib/prism/node.rb

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.

Class Method Summary

Node - Inherited

.fields

Returns a list of the fields that exist for this node class.

.type

Returns the type of the node as a symbol.

Instance Attribute Summary

Node - Inherited

#source

A pointer to the source that this node was created from.

#newline?

Instance Method Summary

Node - Inherited

#accept

Accepts a visitor and calls back into the specialized visit function.

#child_nodes

Returns an array of child nodes, including nils in the place of optional nodes that were not present.

#comment_targets

Returns an array of child nodes and locations that could potentially have comments attached to them.

#compact_child_nodes

Returns an array of child nodes, excluding any nils in the place of optional nodes that were not present.

#deconstruct

Alias for Node#child_nodes.

#end_offset

The end offset of the node in the source.

#inspect

Returns a string representation of the node.

#location

A Location instance that represents the location of this node in the source.

#pretty_print

Similar to inspect, but respects the current level of indentation given by the pretty print object.

#script_lines
#slice

Slice the location of the node from the source.

#slice_lines

Slice the location of the node from the source, starting at the beginning of the line that the location starts on, ending at the end of the line that the location ends on.

#source_lines

Returns all of the lines of the source code associated with this node.

#start_offset

The start offset of the node in the source.

#to_dot

Convert this node into a graphviz dot graph string.

#tunnel

Returns a list of nodes that are descendants of this node that contain the given line and column.

#type

Returns a symbol symbolizing the type of node that this represents.

#deprecated, #newline!

Constructor Details

.new(source, keyword_loc, exceptions, operator_loc, reference, statements, consequent, location) ⇒ RescueNode

def initialize: (Location keyword_loc, Array exceptions, Location? operator_loc, Prism.node? reference, StatementsNode? statements, RescueNode? consequent, Location location) -> void

[ GitHub ]

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

def initialize(source, keyword_loc, exceptions, operator_loc, reference, statements, consequent, location)
  @source = source
  @location = location
  @keyword_loc = keyword_loc
  @exceptions = exceptions
  @operator_loc = operator_loc
  @reference = reference
  @statements = statements
  @consequent = consequent
end

Class Method Details

.type

Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16369

def self.type
  :rescue_node
end

Instance Attribute Details

#consequent (readonly)

attr_reader consequent: RescueNode?

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16328

attr_reader :consequent

#exceptions (readonly)

attr_reader exceptions: Array

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16306

attr_reader :exceptions

#reference (readonly)

attr_reader reference: Prism.node?

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16322

attr_reader :reference

#statements (readonly)

attr_reader statements: StatementsNode?

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16325

attr_reader :statements

Instance Method Details

#===(other)

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

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16375

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) &&
    (consequent === other.consequent)
end

#accept(visitor)

def accept: (Visitor visitor) -> void

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16261

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

#child_nodes Also known as: #deconstruct

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

[ GitHub ]

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

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

#comment_targets

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

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16281

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

#compact_child_nodes

def compact_child_nodes: () -> Array

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16271

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

#copy(keyword_loc: self.keyword_loc, exceptions: self.exceptions, operator_loc: self.operator_loc, reference: self.reference, statements: self.statements, consequent: self.consequent, location: self.location)

def copy: (?keyword_loc: Location, ?exceptions: Array, ?operator_loc: Location?, ?reference: Prism.node?, ?statements: StatementsNode?, ?consequent: RescueNode?, ?location: Location) -> RescueNode

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16286

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

#deconstruct

Alias for #child_nodes.

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16291

alias deconstruct child_nodes

#deconstruct_keys(keys)

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

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16294

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

#inspect

def inspect -> String

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16341

def inspect
  InspectVisitor.compose(self)
end

#keyword

def keyword: () -> String

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16331

def keyword
  keyword_loc.slice
end

#keyword_loc

attr_reader keyword_loc: Location

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16299

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

#operator

def operator: () -> String?

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16336

def operator
  operator_loc&.slice
end

#operator_loc

attr_reader operator_loc: Location?

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16309

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

#type

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling [cls1, cls2].include?(node.class) or putting the node into a case statement and doing case node; when cls1; when cls2; end. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol

[ GitHub ]

  
# File 'lib/prism/node.rb', line 16359

def type
  :rescue_node
end