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

#pretty_print, #to_json

Constructor Details

#initialize(statements:, rescue_clause:, else_keyword:, else_clause:, ensure_clause:, location:, comments: []) ⇒ BodyStmt

Returns a new instance of BodyStmt.



1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
# File 'lib/syntax_tree/node.rb', line 1713

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



1711
1712
1713
# File 'lib/syntax_tree/node.rb', line 1711

def comments
  @comments
end

#else_clauseObject (readonly)

nil | Statements

the optional set of statements inside the else clause



1705
1706
1707
# File 'lib/syntax_tree/node.rb', line 1705

def else_clause
  @else_clause
end

#else_keywordObject (readonly)

nil | Kw

the optional else keyword



1702
1703
1704
# File 'lib/syntax_tree/node.rb', line 1702

def else_keyword
  @else_keyword
end

#ensure_clauseObject (readonly)

nil | Ensure

the optional ensure clause



1708
1709
1710
# File 'lib/syntax_tree/node.rb', line 1708

def ensure_clause
  @ensure_clause
end

#rescue_clauseObject (readonly)

nil | Rescue

the optional rescue chain attached to the begin clause



1699
1700
1701
# File 'lib/syntax_tree/node.rb', line 1699

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Statements

the list of statements inside the begin clause



1696
1697
1698
# File 'lib/syntax_tree/node.rb', line 1696

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



1767
1768
1769
# File 'lib/syntax_tree/node.rb', line 1767

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

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



1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
# File 'lib/syntax_tree/node.rb', line 1731

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
    )

  parts = [rescue_clause, else_clause, ensure_clause]

  # Here we're going to determine the bounds for the statements
  consequent = parts.compact.first
  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 = parts.drop(1).compact.first
    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



1771
1772
1773
# File 'lib/syntax_tree/node.rb', line 1771

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

#deconstruct_keys(keys) ⇒ Object



1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
# File 'lib/syntax_tree/node.rb', line 1777

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

Returns:

  • (Boolean)


1763
1764
1765
# File 'lib/syntax_tree/node.rb', line 1763

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

#format(q) ⇒ Object



1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
# File 'lib/syntax_tree/node.rb', line 1788

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

    if rescue_clause
      q.nest(-2) do
        q.breakable(force: true)
        q.format(rescue_clause)
      end
    end

    if else_clause
      q.nest(-2) do
        q.breakable(force: true)
        q.format(else_keyword)
      end

      unless else_clause.empty?
        q.breakable(force: true)
        q.format(else_clause)
      end
    end

    if ensure_clause
      q.nest(-2) do
        q.breakable(force: true)
        q.format(ensure_clause)
      end
    end
  end
end