Skip to content

Commit d938507

Browse files
snrswtconbeer
authored andcommitted
feat: ensure trailing newline with comment-only queries
1 parent c17447d commit d938507

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/sqlfmt/query.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ def nodes(self) -> List[Node]:
3232
return nodes
3333

3434
def __str__(self) -> str:
35+
if not self.lines:
36+
return ""
37+
3538
draft = [line.render_with_comments(self.line_length) for line in self.lines]
36-
return "".join(draft)
39+
return "".join(draft).rstrip("\n") + "\n"

tests/unit_tests/test_query.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ def test_whitespace_formatting(default_analyzer: Analyzer) -> None:
66
expected_string = "select 1\nfrom my_table\nwhere true\n"
77
q = default_analyzer.parse_query(source_string=source_string)
88
assert str(q) == expected_string
9+
10+
11+
def test_only_comment_formatting(default_analyzer: Analyzer) -> None:
12+
source_string = "-- a comment"
13+
expected_string = "-- a comment\n"
14+
q = default_analyzer.parse_query(source_string=source_string)
15+
assert str(q) == expected_string

0 commit comments

Comments
 (0)