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

#pretty_print, #to_json

Constructor Details

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



6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
# File 'lib/syntax_tree/node.rb', line 6896

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



6894
6895
6896
# File 'lib/syntax_tree/node.rb', line 6894

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



6891
6892
6893
# File 'lib/syntax_tree/node.rb', line 6891

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



6885
6886
6887
# File 'lib/syntax_tree/node.rb', line 6885

def exception
  @exception
end

#keywordObject (readonly)

Kw

the rescue keyword



6882
6883
6884
# File 'lib/syntax_tree/node.rb', line 6882

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



6888
6889
6890
# File 'lib/syntax_tree/node.rb', line 6888

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



6931
6932
6933
# File 'lib/syntax_tree/node.rb', line 6931

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

#bind_end(end_char, end_column) ⇒ Object



6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
# File 'lib/syntax_tree/node.rb', line 6912

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



6935
6936
6937
# File 'lib/syntax_tree/node.rb', line 6935

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

#deconstruct_keys(keys) ⇒ Object



6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
# File 'lib/syntax_tree/node.rb', line 6941

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

#format(q) ⇒ Object



6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
# File 'lib/syntax_tree/node.rb', line 6952

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