diff --git a/lib/syntax_tree/formatter.rb b/lib/syntax_tree/formatter.rb index 643030ac..95c2adaf 100644 --- a/lib/syntax_tree/formatter.rb +++ b/lib/syntax_tree/formatter.rb @@ -17,6 +17,13 @@ def initialize(source, ...) @quote = "\"" end + def self.format(source, node) + formatter = new(source, []) + node.format(formatter) + formatter.flush + formatter.output.join + end + def format(node, stackable: true) stack << node if stackable doc = nil diff --git a/test/formatting_test.rb b/test/formatting_test.rb index 25059658..5f51d471 100644 --- a/test/formatting_test.rb +++ b/test/formatting_test.rb @@ -9,5 +9,10 @@ class FormattingTest < Minitest::Test assert_equal(fixture.formatted, SyntaxTree.format(fixture.source)) end end + + def test_format_class_level + source = "1+1" + assert_equal("1 + 1\n", SyntaxTree::Formatter.format(source, SyntaxTree.parse(source))) + end end end