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

#pretty_print, #to_json

Constructor Details

#initialize(keyword:, exception:, statements:, consequent:, location:, comments: []) ⇒ Rescue

Returns a new instance of Rescue.



7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
# File 'lib/syntax_tree/node.rb', line 7438

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7436
7437
7438
# File 'lib/syntax_tree/node.rb', line 7436

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



7433
7434
7435
# File 'lib/syntax_tree/node.rb', line 7433

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



7427
7428
7429
# File 'lib/syntax_tree/node.rb', line 7427

def exception
  @exception
end

#keywordObject (readonly)

Kw

the rescue keyword



7424
7425
7426
# File 'lib/syntax_tree/node.rb', line 7424

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



7430
7431
7432
# File 'lib/syntax_tree/node.rb', line 7430

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



7473
7474
7475
# File 'lib/syntax_tree/node.rb', line 7473

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

#bind_end(end_char, end_column) ⇒ Object



7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
# File 'lib/syntax_tree/node.rb', line 7454

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



7477
7478
7479
# File 'lib/syntax_tree/node.rb', line 7477

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

#deconstruct_keys(keys) ⇒ Object



7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
# File 'lib/syntax_tree/node.rb', line 7483

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

#format(q) ⇒ Object



7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
# File 'lib/syntax_tree/node.rb', line 7494

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: true)
        q.format(statements)
      end
    end

    if consequent
      q.breakable(force: true)
      q.format(consequent)
    end
  end
end