Class: SyntaxTree::Begin

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

Overview

Begin represents a begin..end chain.

begin
  value
end

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(bodystmt:, location:) ⇒ Begin



1854
1855
1856
1857
1858
# File 'lib/syntax_tree/node.rb', line 1854

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the bodystmt that contains the contents of this begin block



1849
1850
1851
# File 'lib/syntax_tree/node.rb', line 1849

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1852
1853
1854
# File 'lib/syntax_tree/node.rb', line 1852

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



1899
1900
1901
# File 'lib/syntax_tree/node.rb', line 1899

def ===(other)
  other.is_a?(Begin) && bodystmt === other.bodystmt
end

#accept(visitor) ⇒ Object



1860
1861
1862
# File 'lib/syntax_tree/node.rb', line 1860

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

#child_nodesObject Also known as: deconstruct



1864
1865
1866
# File 'lib/syntax_tree/node.rb', line 1864

def child_nodes
  [bodystmt]
end

#copy(bodystmt: nil, location: nil) ⇒ Object



1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
# File 'lib/syntax_tree/node.rb', line 1868

def copy(bodystmt: nil, location: nil)
  node =
    Begin.new(
      bodystmt: bodystmt || self.bodystmt,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



1881
1882
1883
# File 'lib/syntax_tree/node.rb', line 1881

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

#format(q) ⇒ Object



1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
# File 'lib/syntax_tree/node.rb', line 1885

def format(q)
  q.text("begin")

  unless bodystmt.empty?
    q.indent do
      q.breakable_force unless bodystmt.statements.empty?
      q.format(bodystmt)
    end
  end

  q.breakable_force
  q.text("end")
end