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.



16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
# File 'lib/prism/node.rb', line 16876

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



16952
16953
16954
# File 'lib/prism/node.rb', line 16952

def exceptions
  @exceptions
end

#referenceObject (readonly)

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



16974
16975
16976
# File 'lib/prism/node.rb', line 16974

def reference
  @reference
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



16996
16997
16998
# File 'lib/prism/node.rb', line 16996

def statements
  @statements
end

#subsequentObject (readonly)

attr_reader subsequent: RescueNode?



16999
17000
17001
# File 'lib/prism/node.rb', line 16999

def subsequent
  @subsequent
end

Class Method Details

.typeObject

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



17027
17028
17029
# File 'lib/prism/node.rb', line 17027

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.



17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
# File 'lib/prism/node.rb', line 17033

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



16891
16892
16893
# File 'lib/prism/node.rb', line 16891

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



16896
16897
16898
# File 'lib/prism/node.rb', line 16896

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

#comment_targetsObject

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



16921
16922
16923
# File 'lib/prism/node.rb', line 16921

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



16911
16912
16913
16914
16915
16916
16917
16918
# File 'lib/prism/node.rb', line 16911

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



16926
16927
16928
# File 'lib/prism/node.rb', line 16926

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



16934
16935
16936
# File 'lib/prism/node.rb', line 16934

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

#each_child_node {|reference| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



16901
16902
16903
16904
16905
16906
16907
16908
# File 'lib/prism/node.rb', line 16901

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  exceptions.each { |node| yield node }
  yield reference if reference
  yield statements if statements
  yield subsequent if subsequent
end

#inspectObject

def inspect -> String



17017
17018
17019
# File 'lib/prism/node.rb', line 17017

def inspect
  InspectVisitor.compose(self)
end

#keywordObject

def keyword: () -> String



17002
17003
17004
# File 'lib/prism/node.rb', line 17002

def keyword
  keyword_loc.slice
end

#keyword_locObject

attr_reader keyword_loc: Location



16939
16940
16941
16942
16943
# File 'lib/prism/node.rb', line 16939

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?



17007
17008
17009
# File 'lib/prism/node.rb', line 17007

def operator
  operator_loc&.slice
end

#operator_locObject

attr_reader operator_loc: Location?



16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
# File 'lib/prism/node.rb', line 16955

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.



16947
16948
16949
# File 'lib/prism/node.rb', line 16947

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.



16969
16970
16971
# File 'lib/prism/node.rb', line 16969

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.



16991
16992
16993
# File 'lib/prism/node.rb', line 16991

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?



17012
17013
17014
# File 'lib/prism/node.rb', line 17012

def then_keyword
  then_keyword_loc&.slice
end

#then_keyword_locObject

attr_reader then_keyword_loc: Location?



16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
# File 'lib/prism/node.rb', line 16977

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



17022
17023
17024
# File 'lib/prism/node.rb', line 17022

def type
  :rescue_node
end