Class: SyntaxTree::Rescue

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

Overview

Rescue represents the use of the rescue keyword inside of a BodyStmt node.

begin
rescue
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(keyword:, exception:, statements:, consequent:, location:) ⇒ Rescue

Returns a new instance of Rescue.



9276
9277
9278
9279
9280
9281
9282
9283
# File 'lib/syntax_tree/node.rb', line 9276

def initialize(keyword:, exception:, statements:, consequent:, location:)
  @keyword = keyword
  @exception = exception
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9274
9275
9276
# File 'lib/syntax_tree/node.rb', line 9274

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



9271
9272
9273
# File 'lib/syntax_tree/node.rb', line 9271

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



9265
9266
9267
# File 'lib/syntax_tree/node.rb', line 9265

def exception
  @exception
end

#keywordObject (readonly)

Kw

the rescue keyword



9262
9263
9264
# File 'lib/syntax_tree/node.rb', line 9262

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



9268
9269
9270
# File 'lib/syntax_tree/node.rb', line 9268

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



9372
9373
9374
9375
9376
# File 'lib/syntax_tree/node.rb', line 9372

def ===(other)
  other.is_a?(Rescue) && keyword === other.keyword &&
    exception === other.exception && statements === other.statements &&
    consequent === other.consequent
end

#accept(visitor) ⇒ Object



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

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

#bind_end(end_char, end_column) ⇒ Object



9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
# File 'lib/syntax_tree/node.rb', line 9285

def bind_end(end_char, end_column)
  @location =
    Location.new(
      start_line: location.start_line,
      start_char: location.start_char,
      start_column: location.start_column,
      end_line: location.end_line,
      end_char: end_char,
      end_column: end_column
    )

  if consequent
    consequent.bind_end(end_char, end_column)
    statements.bind_end(
      consequent.location.start_char,
      consequent.location.start_column
    )
  else
    statements.bind_end(end_char, end_column)
  end
end

#child_nodesObject Also known as: deconstruct



9311
9312
9313
# File 'lib/syntax_tree/node.rb', line 9311

def child_nodes
  [keyword, exception, statements, consequent]
end

#copy(keyword: nil, exception: nil, statements: nil, consequent: nil, location: nil) ⇒ Object



9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
# File 'lib/syntax_tree/node.rb', line 9315

def copy(
  keyword: nil,
  exception: nil,
  statements: nil,
  consequent: nil,
  location: nil
)
  node =
    Rescue.new(
      keyword: keyword || self.keyword,
      exception: exception || self.exception,
      statements: statements || self.statements,
      consequent: consequent || self.consequent,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



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

def deconstruct_keys(_keys)
  {
    keyword: keyword,
    exception: exception,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
# File 'lib/syntax_tree/node.rb', line 9348

def format(q)
  q.group do
    q.format(keyword)

    if exception
      q.nest(keyword.value.length + 1) { q.format(exception) }
    else
      q.text(" StandardError")
    end

    unless statements.empty?
      q.indent do
        q.breakable_force
        q.format(statements)
      end
    end

    if consequent
      q.breakable_force
      q.format(consequent)
    end
  end
end