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

Constructor Details

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

Returns a new instance of Rescue.



9440
9441
9442
9443
9444
9445
9446
9447
# File 'lib/syntax_tree/node.rb', line 9440

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



9438
9439
9440
# File 'lib/syntax_tree/node.rb', line 9438

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



9435
9436
9437
# File 'lib/syntax_tree/node.rb', line 9435

def consequent
  @consequent
end

#exceptionObject (readonly)

nil | RescueEx

the exceptions being rescued



9429
9430
9431
# File 'lib/syntax_tree/node.rb', line 9429

def exception
  @exception
end

#keywordObject (readonly)

Kw

the rescue keyword



9426
9427
9428
# File 'lib/syntax_tree/node.rb', line 9426

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



9432
9433
9434
# File 'lib/syntax_tree/node.rb', line 9432

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



9536
9537
9538
9539
9540
# File 'lib/syntax_tree/node.rb', line 9536

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

#accept(visitor) ⇒ Object



9471
9472
9473
# File 'lib/syntax_tree/node.rb', line 9471

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

#bind_end(end_char, end_column) ⇒ Object



9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
# File 'lib/syntax_tree/node.rb', line 9449

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 (next_node = consequent)
    next_node.bind_end(end_char, end_column)
    statements.bind_end(
      next_node.location.start_char,
      next_node.location.start_column
    )
  else
    statements.bind_end(end_char, end_column)
  end
end

#child_nodesObject Also known as: deconstruct



9475
9476
9477
# File 'lib/syntax_tree/node.rb', line 9475

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

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



9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
# File 'lib/syntax_tree/node.rb', line 9479

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



9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
# File 'lib/syntax_tree/node.rb', line 9501

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

#format(q) ⇒ Object



9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
# File 'lib/syntax_tree/node.rb', line 9512

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