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.



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

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



9325
9326
9327
# File 'lib/syntax_tree/node.rb', line 9325

def comments
  @comments
end

#exceptionsObject (readonly)

nil | Node

the list of exceptions being rescued



9318
9319
9320
# File 'lib/syntax_tree/node.rb', line 9318

def exceptions
  @exceptions
end

#variableObject (readonly)

nil | Field | VarField

the expression being used to capture the raised

exception



9322
9323
9324
# File 'lib/syntax_tree/node.rb', line 9322

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



9379
9380
9381
9382
# File 'lib/syntax_tree/node.rb', line 9379

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

#accept(visitor) ⇒ Object



9334
9335
9336
# File 'lib/syntax_tree/node.rb', line 9334

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

#child_nodesObject Also known as: deconstruct



9338
9339
9340
# File 'lib/syntax_tree/node.rb', line 9338

def child_nodes
  [*exceptions, variable]
end

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



9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
# File 'lib/syntax_tree/node.rb', line 9342

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



9356
9357
9358
9359
9360
9361
9362
9363
# File 'lib/syntax_tree/node.rb', line 9356

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

#format(q) ⇒ Object



9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
# File 'lib/syntax_tree/node.rb', line 9365

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

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