Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f1253f5

Browse files
committed
Add convenient Formatter.format
1 parent 37ae880 commit f1253f5

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/syntax_tree/formatter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ def initialize(source, ...)
1717
@quote = "\""
1818
end
1919

20+
def self.format(source, node)
21+
formatter = new(source, [])
22+
node.format(formatter)
23+
formatter.flush
24+
formatter.output.join
25+
end
26+
2027
def format(node, stackable: true)
2128
stack << node if stackable
2229
doc = nil

test/formatting_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@ class FormattingTest < Minitest::Test
99
assert_equal(fixture.formatted, SyntaxTree.format(fixture.source))
1010
end
1111
end
12+
13+
def test_format_class_level
14+
source = "1+1"
15+
assert_equal("1 + 1\n", SyntaxTree::Formatter.format(source, SyntaxTree.parse(source)))
16+
end
1217
end
1318
end

0 commit comments

Comments
 (0)