Class: Prism::RescueModifierNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/parse_result/newlines.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/parse_result/newlines.rbs,
ext/prism/api_node.c

Overview

Represents an expression modified with a rescue.

foo rescue nil
^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, expression, keyword_loc, rescue_expression) ⇒ RescueModifierNode

Initialize a new RescueModifierNode node.

: (Source source, Integer node_id, Location location, Integer flags, Prism::node expression, Location keyword_loc, Prism::node rescue_expression) -> void

Parameters:

  • source (Source)
  • node_id (Integer)
  • location (Location)
  • flags (Integer)
  • expression (Prism::node)
  • keyword_loc (Location)
  • rescue_expression (Prism::node)


28765
28766
28767
28768
28769
28770
28771
28772
28773
# File 'lib/prism/node.rb', line 28765

def initialize(source, node_id, location, flags, expression, keyword_loc, rescue_expression)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @expression = expression
  @keyword_loc = keyword_loc
  @rescue_expression = rescue_expression
end

Class Method Details

.type ⇒ :rescue_modifier_node

See Node.type.

: () -> :rescue_modifier_node

Returns:

  • (:rescue_modifier_node)


28856
28857
28858
# File 'lib/prism/node.rb', line 28856

def self.type
  :rescue_modifier_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


28924
28925
28926
28927
28928
28929
# File 'lib/prism/node.rb', line 28924

def ===(other) # :nodoc:
  other.is_a?(RescueModifierNode) &&
    (expression === other.expression) &&
    (keyword_loc.nil? == other.keyword_loc.nil?) &&
    (rescue_expression === other.rescue_expression)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


28789
28790
28791
# File 'lib/prism/node.rb', line 28789

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


28796
28797
28798
# File 'lib/prism/node.rb', line 28796

def child_nodes
  [expression, rescue_expression]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


28814
28815
28816
# File 'lib/prism/node.rb', line 28814

def compact_child_nodes
  [expression, rescue_expression]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, expression: self.expression, keyword_loc: self.keyword_loc, rescue_expression: self.rescue_expression) ⇒ RescueModifierNode

:call-seq:

copy(**fields) -> RescueModifierNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?expression: Prism::node, ?keyword_loc: Location, ?rescue_expression: Prism::node) -> RescueModifierNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • expression: (Prism::node) (defaults to: self.expression)
  • keyword_loc: (Location) (defaults to: self.keyword_loc)
  • rescue_expression: (Prism::node) (defaults to: self.rescue_expression)

Returns:



28824
28825
28826
# File 'lib/prism/node.rb', line 28824

def copy(node_id: self.node_id, location: self.location, flags: self.flags, expression: self.expression, keyword_loc: self.keyword_loc, rescue_expression: self.rescue_expression)
  RescueModifierNode.new(source, node_id, location, flags, expression, keyword_loc, rescue_expression)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


28831
28832
28833
28834
28835
28836
28837
28838
28839
28840
28841
28842
28843
28844
# File 'lib/prism/node.rb', line 28831

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location expression keyword_loc rescue_expression keyword]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :expression              then deconstructed[:expression] = self.expression
    when :keyword_loc             then deconstructed[:keyword_loc] = self.keyword_loc
    when :rescue_expression       then deconstructed[:rescue_expression] = self.rescue_expression
    when :keyword                 then deconstructed[:keyword] = self.keyword
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


28804
28805
28806
28807
28808
28809
# File 'lib/prism/node.rb', line 28804

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  yield expression
  yield rescue_expression
end

#expression ⇒ Prism::node

:call-seq:

expression -> Node

Returns the expression attribute.

: () -> Prism::node

Returns:

  • (Prism::node)


28873
28874
28875
# File 'lib/prism/node.rb', line 28873

def expression
  @expression
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


28861
28862
28863
# File 'lib/prism/node.rb', line 28861

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#keyword ⇒ String

:call-seq:

keyword -> String

Slice the location of keyword_loc from the source.

: () -> String

Returns:

  • (String)


28917
28918
28919
# File 'lib/prism/node.rb', line 28917

def keyword
  keyword_loc.slice
end

#keyword_loc ⇒ Location

:category: Locations :call-seq:

keyword_loc -> Location

Returns the Location represented by keyword_loc.

: () -> Location

Returns:



28884
28885
28886
28887
28888
# File 'lib/prism/node.rb', line 28884

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

#newline_flag!(lines) ⇒ void

This method returns an undefined value.

: (Array lines) -> void

Parameters:

  • lines (Array[bool])


283
284
285
# File 'lib/prism/parse_result/newlines.rb', line 283

def newline_flag!(lines) # :nodoc:
  expression.newline_flag!(lines)
end

#rescue_expression ⇒ Prism::node

:call-seq:

rescue_expression -> Node

Returns the rescue_expression attribute.

: () -> Prism::node

Returns:

  • (Prism::node)


28905
28906
28907
# File 'lib/prism/node.rb', line 28905

def rescue_expression
  @rescue_expression
end

#save_keyword_loc(repository) ⇒ Relocation::Entry

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

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



28895
28896
28897
# File 'lib/prism/node.rb', line 28895

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

#type ⇒ :rescue_modifier_node

See Node#type.

: () -> :rescue_modifier_node

Returns:

  • (:rescue_modifier_node)


28849
28850
28851
# File 'lib/prism/node.rb', line 28849

def type
  :rescue_modifier_node
end