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



2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
# File 'lib/syntax_tree/node.rb', line 2014

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