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 `reference` 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, then_keyword_loc, statements, subsequent) ⇒ RescueNode

Initialize a new RescueNode node.



15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
# File 'lib/prism/node.rb', line 15889

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

Instance Attribute Details

#exceptionsObject (readonly)

attr_reader exceptions: Array



15955
15956
15957
# File 'lib/prism/node.rb', line 15955

def exceptions
  @exceptions
end

#referenceObject (readonly)

attr_reader reference: LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | BackReferenceReadNode | NumberedReferenceReadNode | MissingNode | nil



15977
15978
15979
# File 'lib/prism/node.rb', line 15977

def reference
  @reference
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



15999
16000
16001
# File 'lib/prism/node.rb', line 15999

def statements
  @statements
end

#subsequentObject (readonly)

attr_reader subsequent: RescueNode?



16002
16003
16004
# File 'lib/prism/node.rb', line 16002

def subsequent
  @subsequent
end

Class Method Details

.typeObject

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



16030
16031
16032
# File 'lib/prism/node.rb', line 16030

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.



16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
# File 'lib/prism/node.rb', line 16036

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) &&
    (then_keyword_loc.nil? == other.then_keyword_loc.nil?) &&
    (statements === other.statements) &&
    (subsequent === other.subsequent)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



15904
15905
15906
# File 'lib/prism/node.rb', line 15904

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



15909
15910
15911
# File 'lib/prism/node.rb', line 15909

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

#comment_targetsObject

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



15924
15925
15926
# File 'lib/prism/node.rb', line 15924

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



15914
15915
15916
15917
15918
15919
15920
15921
# File 'lib/prism/node.rb', line 15914

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.



497
498
499
500
# File 'lib/prism/node_ext.rb', line 497

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, then_keyword_loc: self.then_keyword_loc, 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: LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | BackReferenceReadNode | NumberedReferenceReadNode | MissingNode | nil, ?then_keyword_loc: Location?, ?statements: StatementsNode?, ?subsequent: RescueNode?) -> RescueNode



15929
15930
15931
# File 'lib/prism/node.rb', line 15929

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, then_keyword_loc: self.then_keyword_loc, statements: self.statements, subsequent: self.subsequent)
  RescueNode.new(source, node_id, location, flags, keyword_loc, exceptions, operator_loc, reference, then_keyword_loc, 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: LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | BackReferenceReadNode | NumberedReferenceReadNode | MissingNode | nil, then_keyword_loc: Location?, statements: StatementsNode?, subsequent: RescueNode? }



15937
15938
15939
# File 'lib/prism/node.rb', line 15937

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

#inspectObject

def inspect -> String



16020
16021
16022
# File 'lib/prism/node.rb', line 16020

def inspect
  InspectVisitor.compose(self)
end

#keywordObject

def keyword: () -> String



16005
16006
16007
# File 'lib/prism/node.rb', line 16005

def keyword
  keyword_loc.slice
end

#keyword_locObject

attr_reader keyword_loc: Location



15942
15943
15944
15945
15946
# File 'lib/prism/node.rb', line 15942

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?



16010
16011
16012
# File 'lib/prism/node.rb', line 16010

def operator
  operator_loc&.slice
end

#operator_locObject

attr_reader operator_loc: Location?



15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
# File 'lib/prism/node.rb', line 15958

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

#save_keyword_loc(repository) ⇒ Object

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



15950
15951
15952
# File 'lib/prism/node.rb', line 15950

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

#save_operator_loc(repository) ⇒ Object

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



15972
15973
15974
# File 'lib/prism/node.rb', line 15972

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc) unless @operator_loc.nil?
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.



15994
15995
15996
# File 'lib/prism/node.rb', line 15994

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?



16015
16016
16017
# File 'lib/prism/node.rb', line 16015

def then_keyword
  then_keyword_loc&.slice
end

#then_keyword_locObject

attr_reader then_keyword_loc: Location?



15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
# File 'lib/prism/node.rb', line 15980

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



16025
16026
16027
# File 'lib/prism/node.rb', line 16025

def type
  :rescue_node
end