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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Rescue.



8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
# File 'lib/syntax_tree/node.rb', line 8857

def initialize(
  exception:,
  statements:,
  consequent:,
  location:,
  comments: []
)
  @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



8855
8856
8857
# File 'lib/syntax_tree/node.rb', line 8855

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



8849
8850
8851
# File 'lib/syntax_tree/node.rb', line 8849

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



8843
8844
8845
# File 'lib/syntax_tree/node.rb', line 8843

def exception
  @exception
end

#locationObject (readonly)

Location

the location of this node



8852
8853
8854
# File 'lib/syntax_tree/node.rb', line 8852

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



8846
8847
8848
# File 'lib/syntax_tree/node.rb', line 8846

def statements
  @statements
end

Instance Method Details

#bind_end(end_char) ⇒ Object



8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
# File 'lib/syntax_tree/node.rb', line 8871

def bind_end(end_char)
  @location =
    Location.new(
      start_line: location.start_line,
      start_char: location.start_char,
      end_line: location.end_line,
      end_char: end_char
    )

  if consequent
    consequent.bind_end(end_char)
    statements.bind_end(consequent.location.start_char)
  else
    statements.bind_end(end_char)
  end
end

#child_nodesObject Also known as: deconstruct



8888
8889
8890
# File 'lib/syntax_tree/node.rb', line 8888

def child_nodes
  [exception, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



8894
8895
8896
8897
8898
8899
8900
8901
8902
# File 'lib/syntax_tree/node.rb', line 8894

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

#format(q) ⇒ Object



8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
# File 'lib/syntax_tree/node.rb', line 8904

def format(q)
  q.group do
    q.text("rescue")

    if exception
      q.nest("rescue ".length) { 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

#pretty_print(q) ⇒ Object



8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
# File 'lib/syntax_tree/node.rb', line 8928

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("rescue")

    if exception
      q.breakable
      q.pp(exception)
    end

    q.breakable
    q.pp(statements)

    if consequent
      q.breakable
      q.pp(consequent)
    end

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
# File 'lib/syntax_tree/node.rb', line 8949

def to_json(*opts)
  {
    type: :rescue,
    extn: exception,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end