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.



7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
# File 'lib/syntax_tree/node.rb', line 7646

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



7644
7645
7646
# File 'lib/syntax_tree/node.rb', line 7644

def comments
  @comments
end

#consequent ⇒ Object (readonly)

[nil | Rescue] the optional next clause in the chain



7641
7642
7643
# File 'lib/syntax_tree/node.rb', line 7641

def consequent
  @consequent
end

#exception ⇒ Object (readonly)

[RescueEx] the exceptions being rescued



7635
7636
7637
# File 'lib/syntax_tree/node.rb', line 7635

def exception
  @exception
end

#keyword ⇒ Object (readonly)

[Kw] the rescue keyword



7632
7633
7634
# File 'lib/syntax_tree/node.rb', line 7632

def keyword
  @keyword
end

#statements ⇒ Object (readonly)

[Statements] the expressions to evaluate when an error is rescued



7638
7639
7640
# File 'lib/syntax_tree/node.rb', line 7638

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



7684
7685
7686
# File 'lib/syntax_tree/node.rb', line 7684

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

#bind_end(end_char, end_column) ⇒ Object



7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
# File 'lib/syntax_tree/node.rb', line 7662

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_nodes ⇒ Object Also known as: deconstruct



7688
7689
7690
# File 'lib/syntax_tree/node.rb', line 7688

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

#deconstruct_keys(_keys) ⇒ Object



7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
# File 'lib/syntax_tree/node.rb', line 7694

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

#format(q) ⇒ Object



7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
# File 'lib/syntax_tree/node.rb', line 7705

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