Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add convenient Formatter.format
  • Loading branch information
vinistock committed Apr 20, 2022
commit f1253f5b43cbf022fd7f77b8e7a1d9f47193a71c
7 changes: 7 additions & 0 deletions lib/syntax_tree/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/formatting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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