Class: SyntaxTree::RescueMod

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

RescueMod represents the use of the modifier form of a rescue clause.

expression rescue value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(statement:, value:, location:) ⇒ RescueMod

Returns a new instance of RescueMod.



9541
9542
9543
9544
9545
9546
# File 'lib/syntax_tree/node.rb', line 9541

def initialize(statement:, value:, location:)
  @statement = statement
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9539
9540
9541
# File 'lib/syntax_tree/node.rb', line 9539

def comments
  @comments
end

#statementObject (readonly)

Node

the expression to execute



9533
9534
9535
# File 'lib/syntax_tree/node.rb', line 9533

def statement
  @statement
end

#valueObject (readonly)

Node

the value to use if the executed expression raises an error



9536
9537
9538
# File 'lib/syntax_tree/node.rb', line 9536

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



9597
9598
9599
9600
# File 'lib/syntax_tree/node.rb', line 9597

def ===(other)
  other.is_a?(RescueMod) && statement === other.statement &&
    value === other.value
end

#accept(visitor) ⇒ Object



9548
9549
9550
# File 'lib/syntax_tree/node.rb', line 9548

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

#child_nodesObject Also known as: deconstruct



9552
9553
9554
# File 'lib/syntax_tree/node.rb', line 9552

def child_nodes
  [statement, value]
end

#copy(statement: nil, value: nil, location: nil) ⇒ Object



9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
# File 'lib/syntax_tree/node.rb', line 9556

def copy(statement: nil, value: nil, location: nil)
  node =
    RescueMod.new(
      statement: statement || self.statement,
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



9570
9571
9572
9573
9574
9575
9576
9577
# File 'lib/syntax_tree/node.rb', line 9570

def deconstruct_keys(_keys)
  {
    statement: statement,
    value: value,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
# File 'lib/syntax_tree/node.rb', line 9579

def format(q)
  q.text("begin")
  q.group do
    q.indent do
      q.breakable_force
      q.format(statement)
    end
    q.breakable_force
    q.text("rescue StandardError")
    q.indent do
      q.breakable_force
      q.format(value)
    end
    q.breakable_force
  end
  q.text("end")
end