Skip to content

Commit bc43911

Browse files
committed
fix: tweak solution, add more tests, update changelog
1 parent d938507 commit bc43911

7 files changed

Lines changed: 18 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77
### Formatting Changes and Bug Fixes
88

99
- sqlfmt now lexes and formats BigQuery hexadecimal literals as numbers, and no longer adds a space between `0` and `x` ([#696](https://github.com/tconbeer/sqlfmt/issues/696) - thank you [@KMontag42](https://github.com/KMontag42) and [@snrsw](https://github.com/snrsw)!).
10+
- sqlfmt no longer adds a second blank newline to the end of a query that only contains a comment (with no sql code) ([#686](https://github.com/tconbeer/sqlfmt/issues/686) - thank you [@nevdelap](https://github.com/nevdelap) and [@snrsw](https://github.com/snrsw)!).
1011

1112
## [0.27.0] - 2025-07-28
1213

src/sqlfmt/line.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ def render_with_comments(self, max_length: int) -> str:
105105

106106
if inline_comments:
107107
rendered_lines.append(f"{content}{''.join(inline_comments)}\n")
108-
else:
108+
# if content is empty AND we've already rendered a standalone or multiline
109+
# comment, skip rendering the line itself, since that just adds a duplicate
110+
# newline.
111+
elif content or not self.comments:
109112
rendered_lines.append(f"{self}")
110113

111114
return "".join(rendered_lines)

src/sqlfmt/query.py

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

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

tests/data/preformatted/009_empty.sql

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- a comment

tests/functional_tests/test_general_formatting.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"preformatted/006_fmt_off_447.sql",
1717
"preformatted/007_fmt_off_comments.sql",
1818
"preformatted/008_reserved_names.sql",
19+
"preformatted/009_empty.sql",
20+
"preformatted/010_comment_only.sql",
1921
"preformatted/301_multiline_jinjafmt.sql",
2022
"preformatted/302_jinjafmt_multiline_str.sql",
2123
"preformatted/303_jinjafmt_more_mutliline_str.sql",

tests/unit_tests/test_query.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from sqlfmt.analyzer import Analyzer
24

35

@@ -13,3 +15,10 @@ def test_only_comment_formatting(default_analyzer: Analyzer) -> None:
1315
expected_string = "-- a comment\n"
1416
q = default_analyzer.parse_query(source_string=source_string)
1517
assert str(q) == expected_string
18+
19+
20+
@pytest.mark.parametrize("source_string", ["", "\n"])
21+
def test_empty_formatting(default_analyzer: Analyzer, source_string: str) -> None:
22+
expected_string = ""
23+
q = default_analyzer.parse_query(source_string=source_string)
24+
assert str(q) == expected_string

0 commit comments

Comments
 (0)