Class: SyntaxTree::BodyStmt

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

Overview

bodystmt can’t actually determine its bounds appropriately because it doesn’t necessarily know where it started. So the parent node needs to report back down into this one where it goes.

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(statements:, rescue_clause:, else_keyword:, else_clause:, ensure_clause:, location:, comments: []) ⇒ BodyStmt



1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
# File 'lib/syntax_tree/node.rb', line 1854

def initialize(
  statements:,
  rescue_clause:,
  else_keyword:,
  else_clause:,
  ensure_clause:,
  location:,
  comments: []
)
  @statements = statements
  @rescue_clause = rescue_clause
  @else_keyword = else_keyword
  @else_clause = else_clause
  @ensure_clause = ensure_clause
  @location = location
  @comments = comments
end

Instance Attribute Details

#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

#else_clauseObject (readonly)

nil | Statements

the optional set of statements inside the else clause



1846
1847
1848
# File 'lib/syntax_tree/node.rb', line 1846

def else_clause
  @else_clause
end

#else_keywordObject (readonly)

nil | Kw

the optional else keyword



1843
1844
1845
# File 'lib/syntax_tree/node.rb', line 1843

def else_keyword
  @else_keyword
end

#ensure_clauseObject (readonly)

nil | Ensure

the optional ensure clause



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

def ensure_clause
  @ensure_clause
end

#rescue_clauseObject (readonly)

nil | Rescue

the optional rescue chain attached to the begin clause



1840
1841
1842
# File 'lib/syntax_tree/node.rb', line 1840

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Statements

the list of statements inside the begin clause



1837
1838
1839
# File 'lib/syntax_tree/node.rb', line 1837

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



1906
1907
1908
# File 'lib/syntax_tree/node.rb', line 1906

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

#bind(start_char, start_column, end_char, end_column) ⇒ Object



1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
# File 'lib/syntax_tree/node.rb', line 1872

def bind(start_char, start_column, end_char, end_column)
  @location =
    Location.new(
      start_line: location.start_line,
      start_char: start_char,
      start_column: start_column,
      end_line: location.end_line,
      end_char: end_char,
      end_column: end_column
    )

  # Here we're going to determine the bounds for the statements
  consequent = rescue_clause || else_clause || ensure_clause
  statements.bind(
    start_char,
    start_column,
    consequent ? consequent.location.start_char : end_char,
    consequent ? consequent.location.start_column : end_column
  )

  # Next we're going to determine the rescue clause if there is one
  if rescue_clause
    consequent = else_clause || ensure_clause
    rescue_clause.bind_end(
      consequent ? consequent.location.start_char : end_char,
      consequent ? consequent.location.start_column : end_column
    )
  end
end

#child_nodesObject Also known as: deconstruct



1910
1911
1912
# File 'lib/syntax_tree/node.rb', line 1910

def child_nodes
  [statements, rescue_clause, else_keyword, else_clause, ensure_clause]
end

#deconstruct_keys(_keys) ⇒ Object



1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
# File 'lib/syntax_tree/node.rb', line 1916

def deconstruct_keys(_keys)
  {
    statements: statements,
    rescue_clause: rescue_clause,
    else_clause: else_clause,
    ensure_clause: ensure_clause,
    location: location,
    comments: comments
  }
end

#empty?Boolean



1902
1903
1904
# File 'lib/syntax_tree/node.rb', line 1902

def empty?
  statements.empty? && !rescue_clause && !else_clause && !ensure_clause
end

#format(q) ⇒ Object



1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
# File 'lib/syntax_tree/node.rb', line 1927

def format(q)
  q.group do
    q.format(statements) unless statements.empty?

    if rescue_clause
      q.nest(-2) do
        q.breakable_force
        q.format(rescue_clause)
      end
    end

    if else_clause
      q.nest(-2) do
        q.breakable_force
        q.format(else_keyword)
      end

      unless else_clause.empty?
        q.breakable_force
        q.format(else_clause)
      end
    end

    if ensure_clause
      q.nest(-2) do
        q.breakable_force
        q.format(ensure_clause)
      end
    end
  end
end