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.



5812
5813
5814
5815
5816
5817
# File 'lib/syntax_tree.rb', line 5812

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



5810
5811
5812
# File 'lib/syntax_tree.rb', line 5810

def comments
  @comments
end

#keywordObject (readonly)

Kw

the ensure keyword that began this node



5801
5802
5803
# File 'lib/syntax_tree.rb', line 5801

def keyword
  @keyword
end

#locationObject (readonly)

Location

the location of this node



5807
5808
5809
# File 'lib/syntax_tree.rb', line 5807

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



5804
5805
5806
# File 'lib/syntax_tree.rb', line 5804

def statements
  @statements
end

Instance Method Details

#child_nodesObject



5819
5820
5821
# File 'lib/syntax_tree.rb', line 5819

def child_nodes
  [keyword, statements]
end

#format(q) ⇒ Object



5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
# File 'lib/syntax_tree.rb', line 5823

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



5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
# File 'lib/syntax_tree.rb', line 5834

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



5845
5846
5847
5848
5849
5850
5851
5852
5853
# File 'lib/syntax_tree.rb', line 5845

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