Class: SyntaxTree::RescueEx

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

Overview

RescueEx represents the list of exceptions being rescued in a rescue clause.

begin
rescue Exception => exception
end

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(exceptions:, variable:, location:) ⇒ RescueEx

Returns a new instance of RescueEx.



9296
9297
9298
9299
9300
9301
# File 'lib/syntax_tree/node.rb', line 9296

def initialize(exceptions:, variable:, location:)
  @exceptions = exceptions
  @variable = variable
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9294
9295
9296
# File 'lib/syntax_tree/node.rb', line 9294

def comments
  @comments
end

#exceptionsObject (readonly)

nil | Node

the list of exceptions being rescued



9287
9288
9289
# File 'lib/syntax_tree/node.rb', line 9287

def exceptions
  @exceptions
end

#variableObject (readonly)

nil | Field | VarField

the expression being used to capture the raised

exception



9291
9292
9293
# File 'lib/syntax_tree/node.rb', line 9291

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



9348
9349
9350
9351
# File 'lib/syntax_tree/node.rb', line 9348

def ===(other)
  other.is_a?(RescueEx) && exceptions === other.exceptions &&
    variable === other.variable
end

#accept(visitor) ⇒ Object



9303
9304
9305
# File 'lib/syntax_tree/node.rb', line 9303

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

#child_nodesObject Also known as: deconstruct



9307
9308
9309
# File 'lib/syntax_tree/node.rb', line 9307

def child_nodes
  [*exceptions, variable]
end

#copy(exceptions: nil, variable: nil, location: nil) ⇒ Object



9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
# File 'lib/syntax_tree/node.rb', line 9311

def copy(exceptions: nil, variable: nil, location: nil)
  node =
    RescueEx.new(
      exceptions: exceptions || self.exceptions,
      variable: variable || self.variable,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



9325
9326
9327
9328
9329
9330
9331
9332
# File 'lib/syntax_tree/node.rb', line 9325

def deconstruct_keys(_keys)
  {
    exceptions: exceptions,
    variable: variable,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
# File 'lib/syntax_tree/node.rb', line 9334

def format(q)
  q.group do
    if exceptions
      q.text(" ")
      q.format(exceptions)
    end

    if variable
      q.text(" => ")
      q.format(variable)
    end
  end
end