Class: SyntaxTree::RescueMod

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

RescueMod represents the use of the modifier form of a rescue clause.

expression rescue value

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(statement:, value:, location:) ⇒ RescueMod

Returns a new instance of RescueMod.



9393
9394
9395
9396
9397
9398
# File 'lib/syntax_tree/node.rb', line 9393

def initialize(statement:, value:, location:)
  @statement = statement
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9391
9392
9393
# File 'lib/syntax_tree/node.rb', line 9391

def comments
  @comments
end

#statementObject (readonly)

untyped

the expression to execute



9385
9386
9387
# File 'lib/syntax_tree/node.rb', line 9385

def statement
  @statement
end

#valueObject (readonly)

untyped

the value to use if the executed expression raises an error



9388
9389
9390
# File 'lib/syntax_tree/node.rb', line 9388

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



9449
9450
9451
9452
# File 'lib/syntax_tree/node.rb', line 9449

def ===(other)
  other.is_a?(RescueMod) && statement === other.statement &&
    value === other.value
end

#accept(visitor) ⇒ Object



9400
9401
9402
# File 'lib/syntax_tree/node.rb', line 9400

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

#child_nodesObject Also known as: deconstruct



9404
9405
9406
# File 'lib/syntax_tree/node.rb', line 9404

def child_nodes
  [statement, value]
end

#copy(statement: nil, value: nil, location: nil) ⇒ Object



9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
# File 'lib/syntax_tree/node.rb', line 9408

def copy(statement: nil, value: nil, location: nil)
  node =
    RescueMod.new(
      statement: statement || self.statement,
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



9422
9423
9424
9425
9426
9427
9428
9429
# File 'lib/syntax_tree/node.rb', line 9422

def deconstruct_keys(_keys)
  {
    statement: statement,
    value: value,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
# File 'lib/syntax_tree/node.rb', line 9431

def format(q)
  q.text("begin")
  q.group do
    q.indent do
      q.breakable_force
      q.format(statement)
    end
    q.breakable_force
    q.text("rescue StandardError")
    q.indent do
      q.breakable_force
      q.format(value)
    end
    q.breakable_force
  end
  q.text("end")
end