Module: SyntaxTree::RemoveBreaks

Defined in:
lib/syntax_tree/node.rb

Overview

This module will remove any breakables from the list of contents so that no newlines are present in the output.

Class Method Summary collapse

Class Method Details

.call(doc) ⇒ Object



1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
# File 'lib/syntax_tree/node.rb', line 1609

def call(doc)
  marker = Object.new
  stack = [doc]

  while stack.any?
    doc = stack.pop

    if doc == marker
      stack.pop
      next
    end

    stack += [doc, marker]

    case doc
    when PrettyPrint::Align, PrettyPrint::Indent, PrettyPrint::Group
      doc.contents.map! { |child| remove_breaks(child) }
      stack += doc.contents.reverse
    when PrettyPrint::IfBreak
      doc.flat_contents.map! { |child| remove_breaks(child) }
      stack += doc.flat_contents.reverse
    end
  end
end