Class: SyntaxTree::Ensure

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

Overview

Ensure represents the use of the ensure keyword and its subsequent statements.

begin
ensure
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword:, statements:, location:, comments: []) ⇒ Ensure



5342
5343
5344
5345
5346
5347
# File 'lib/syntax_tree.rb', line 5342

def initialize(keyword:, statements:, location:, comments: [])
  @keyword = keyword
  @statements = statements
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5340
5341
5342
# File 'lib/syntax_tree.rb', line 5340

def comments
  @comments
end

#keywordObject (readonly)

Kw

the ensure keyword that began this node



5331
5332
5333
# File 'lib/syntax_tree.rb', line 5331

def keyword
  @keyword
end

#locationObject (readonly)

Location

the location of this node



5337
5338
5339
# File 'lib/syntax_tree.rb', line 5337

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



5334
5335
5336
# File 'lib/syntax_tree.rb', line 5334

def statements
  @statements
end

Instance Method Details

#child_nodesObject



5349
5350
5351
# File 'lib/syntax_tree.rb', line 5349

def child_nodes
  [keyword, statements]
end

#format(q) ⇒ Object



5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
# File 'lib/syntax_tree.rb', line 5353

def format(q)
  q.format(keyword)

  unless statements.empty?
    q.indent do
      q.breakable(force: true)
      q.format(statements)
    end
  end
end

#pretty_print(q) ⇒ Object



5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
# File 'lib/syntax_tree.rb', line 5364

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

    q.breakable
    q.pp(statements)

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

#to_json(*opts) ⇒ Object



5375
5376
5377
5378
5379
5380
5381
5382
5383
# File 'lib/syntax_tree.rb', line 5375

def to_json(*opts)
  {
    type: :ensure,
    keyword: keyword,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end