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.



7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
# File 'lib/syntax_tree/node.rb', line 7699

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



7697
7698
7699
# File 'lib/syntax_tree/node.rb', line 7697

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



7694
7695
7696
# File 'lib/syntax_tree/node.rb', line 7694

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



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

def exception
  @exception
end

#keywordObject (readonly)

Kw

the rescue keyword



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

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



7691
7692
7693
# File 'lib/syntax_tree/node.rb', line 7691

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



7737
7738
7739
# File 'lib/syntax_tree/node.rb', line 7737

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

#bind_end(end_char, end_column) ⇒ Object



7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
# File 'lib/syntax_tree/node.rb', line 7715

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



7741
7742
7743
# File 'lib/syntax_tree/node.rb', line 7741

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

#deconstruct_keys(_keys) ⇒ Object



7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
# File 'lib/syntax_tree/node.rb', line 7747

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

#format(q) ⇒ Object



7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
# File 'lib/syntax_tree/node.rb', line 7758

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