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, #pretty_print, #to_json

Constructor Details

#initialize(exceptions:, variable:, location:) ⇒ RescueEx

Returns a new instance of RescueEx.



9196
9197
9198
9199
9200
9201
# File 'lib/syntax_tree/node.rb', line 9196

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



9194
9195
9196
# File 'lib/syntax_tree/node.rb', line 9194

def comments
  @comments
end

#exceptionsObject (readonly)

untyped

the list of exceptions being rescued



9187
9188
9189
# File 'lib/syntax_tree/node.rb', line 9187

def exceptions
  @exceptions
end

#variableObject (readonly)

nil | Field | VarField

the expression being used to capture the raised

exception



9191
9192
9193
# File 'lib/syntax_tree/node.rb', line 9191

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



9248
9249
9250
9251
# File 'lib/syntax_tree/node.rb', line 9248

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

#accept(visitor) ⇒ Object



9203
9204
9205
# File 'lib/syntax_tree/node.rb', line 9203

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

#child_nodesObject Also known as: deconstruct



9207
9208
9209
# File 'lib/syntax_tree/node.rb', line 9207

def child_nodes
  [*exceptions, variable]
end

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



9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
# File 'lib/syntax_tree/node.rb', line 9211

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



9225
9226
9227
9228
9229
9230
9231
9232
# File 'lib/syntax_tree/node.rb', line 9225

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

#format(q) ⇒ Object



9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
# File 'lib/syntax_tree/node.rb', line 9234

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

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