Class: SyntaxTree::Rescue

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
# File 'lib/syntax_tree.rb', line 10254

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



10252
10253
10254
# File 'lib/syntax_tree.rb', line 10252

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



10246
10247
10248
# File 'lib/syntax_tree.rb', line 10246

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



10240
10241
10242
# File 'lib/syntax_tree.rb', line 10240

def exception
  @exception
end

#locationObject (readonly)

Location

the location of this node



10249
10250
10251
# File 'lib/syntax_tree.rb', line 10249

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



10243
10244
10245
# File 'lib/syntax_tree.rb', line 10243

def statements
  @statements
end

Instance Method Details

#bind_end(end_char) ⇒ Object



10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
# File 'lib/syntax_tree.rb', line 10268

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



10285
10286
10287
# File 'lib/syntax_tree.rb', line 10285

def child_nodes
  [exception, statements, consequent]
end

#format(q) ⇒ Object



10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
# File 'lib/syntax_tree.rb', line 10289

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



10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
# File 'lib/syntax_tree.rb', line 10313

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



10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
# File 'lib/syntax_tree.rb', line 10334

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