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

Returns a new instance of Ensure.



5643
5644
5645
5646
5647
5648
# File 'lib/syntax_tree.rb', line 5643

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



5641
5642
5643
# File 'lib/syntax_tree.rb', line 5641

def comments
  @comments
end

#keywordObject (readonly)

Kw

the ensure keyword that began this node



5632
5633
5634
# File 'lib/syntax_tree.rb', line 5632

def keyword
  @keyword
end

#locationObject (readonly)

Location

the location of this node



5638
5639
5640
# File 'lib/syntax_tree.rb', line 5638

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



5635
5636
5637
# File 'lib/syntax_tree.rb', line 5635

def statements
  @statements
end

Instance Method Details

#child_nodesObject



5650
5651
5652
# File 'lib/syntax_tree.rb', line 5650

def child_nodes
  [keyword, statements]
end

#format(q) ⇒ Object



5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
# File 'lib/syntax_tree.rb', line 5654

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



5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
# File 'lib/syntax_tree.rb', line 5665

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



5676
5677
5678
5679
5680
5681
5682
5683
5684
# File 'lib/syntax_tree.rb', line 5676

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