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.



7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
# File 'lib/syntax_tree/node.rb', line 7909

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



7907
7908
7909
# File 'lib/syntax_tree/node.rb', line 7907

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



7904
7905
7906
# File 'lib/syntax_tree/node.rb', line 7904

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



7898
7899
7900
# File 'lib/syntax_tree/node.rb', line 7898

def exception
  @exception
end

#keywordObject (readonly)

Kw

the rescue keyword



7895
7896
7897
# File 'lib/syntax_tree/node.rb', line 7895

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



7901
7902
7903
# File 'lib/syntax_tree/node.rb', line 7901

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



7947
7948
7949
# File 'lib/syntax_tree/node.rb', line 7947

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

#bind_end(end_char, end_column) ⇒ Object



7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
# File 'lib/syntax_tree/node.rb', line 7925

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



7951
7952
7953
# File 'lib/syntax_tree/node.rb', line 7951

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

#deconstruct_keys(_keys) ⇒ Object



7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
# File 'lib/syntax_tree/node.rb', line 7957

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

#format(q) ⇒ Object



7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
# File 'lib/syntax_tree/node.rb', line 7968

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
        q.format(statements)
      end
    end

    if consequent
      q.breakable_force
      q.format(consequent)
    end
  end
end