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:, comments: []) ⇒ Rescue

Returns a new instance of Rescue.



7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
# File 'lib/syntax_tree/node.rb', line 7535

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



7533
7534
7535
# File 'lib/syntax_tree/node.rb', line 7533

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



7530
7531
7532
# File 'lib/syntax_tree/node.rb', line 7530

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



7524
7525
7526
# File 'lib/syntax_tree/node.rb', line 7524

def exception
  @exception
end

#keywordObject (readonly)

Kw

the rescue keyword



7521
7522
7523
# File 'lib/syntax_tree/node.rb', line 7521

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



7527
7528
7529
# File 'lib/syntax_tree/node.rb', line 7527

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



7573
7574
7575
# File 'lib/syntax_tree/node.rb', line 7573

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

#bind_end(end_char, end_column) ⇒ Object



7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
# File 'lib/syntax_tree/node.rb', line 7551

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



7577
7578
7579
# File 'lib/syntax_tree/node.rb', line 7577

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

#deconstruct_keys(_keys) ⇒ Object



7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
# File 'lib/syntax_tree/node.rb', line 7583

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

#format(q) ⇒ Object



7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
# File 'lib/syntax_tree/node.rb', line 7594

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