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.



7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
# File 'lib/syntax_tree/node.rb', line 7657

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



7655
7656
7657
# File 'lib/syntax_tree/node.rb', line 7655

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



7652
7653
7654
# File 'lib/syntax_tree/node.rb', line 7652

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



7646
7647
7648
# File 'lib/syntax_tree/node.rb', line 7646

def exception
  @exception
end

#keywordObject (readonly)

Kw

the rescue keyword



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

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



7649
7650
7651
# File 'lib/syntax_tree/node.rb', line 7649

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#bind_end(end_char, end_column) ⇒ Object



7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
# File 'lib/syntax_tree/node.rb', line 7673

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



7699
7700
7701
# File 'lib/syntax_tree/node.rb', line 7699

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

#deconstruct_keys(_keys) ⇒ Object



7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
# File 'lib/syntax_tree/node.rb', line 7705

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

#format(q) ⇒ Object



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

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