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.



9524
9525
9526
9527
9528
9529
# File 'lib/syntax_tree/node.rb', line 9524

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



9522
9523
9524
# File 'lib/syntax_tree/node.rb', line 9522

def comments
  @comments
end

#statementObject (readonly)

Node

the expression to execute



9516
9517
9518
# File 'lib/syntax_tree/node.rb', line 9516

def statement
  @statement
end

#valueObject (readonly)

Node

the value to use if the executed expression raises an error



9519
9520
9521
# File 'lib/syntax_tree/node.rb', line 9519

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



9580
9581
9582
9583
# File 'lib/syntax_tree/node.rb', line 9580

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

#accept(visitor) ⇒ Object



9531
9532
9533
# File 'lib/syntax_tree/node.rb', line 9531

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [statement, value]
end

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



9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
# File 'lib/syntax_tree/node.rb', line 9539

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



9553
9554
9555
9556
9557
9558
9559
9560
# File 'lib/syntax_tree/node.rb', line 9553

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

#format(q) ⇒ Object



9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
# File 'lib/syntax_tree/node.rb', line 9562

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