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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Formatting Changes and Bug Fixes

- 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)!).
- 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)!).

## [0.27.0] - 2025-07-28

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.PHONY: check
check:
ruff format .
ruff check . --fix
pytest
mypy
poetry run ruff format .
poetry run ruff check . --fix
poetry run pytest
poetry run mypy

.PHONY: unit
unit:
Expand Down
5 changes: 4 additions & 1 deletion src/sqlfmt/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ def render_with_comments(self, max_length: int) -> str:

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

return "".join(rendered_lines)
Expand Down
6 changes: 3 additions & 3 deletions src/sqlfmt_primer/primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_projects() -> List[SQLProject]:
SQLProject(
name="gitlab",
git_url="https://github.com/tconbeer/gitlab-analytics-sqlfmt.git",
git_ref="5cd49f6", # sqlfmt aed0f39
git_ref="f859b79", # sqlfmt bc43911
expected_changed=1,
expected_unchanged=2416,
expected_errored=0,
Expand All @@ -39,7 +39,7 @@ def get_projects() -> List[SQLProject]:
SQLProject(
name="rittman",
git_url="https://github.com/tconbeer/rittman_ra_data_warehouse.git",
git_ref="418af64", # sqlfmt cd38a6c
git_ref="5fe702b", # sqlfmt bc43911
expected_changed=0,
expected_unchanged=307,
expected_errored=4, # true mismatching brackets
Expand All @@ -48,7 +48,7 @@ def get_projects() -> List[SQLProject]:
SQLProject(
name="http_archive",
git_url="https://github.com/tconbeer/http_archive_almanac.git",
git_ref="414b535", # sqlfmt faaf71b
git_ref="37302f3", # sqlfmt bc43911
expected_changed=0,
expected_unchanged=1729,
expected_errored=0,
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/data/preformatted/010_comment_only.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- a comment
2 changes: 2 additions & 0 deletions tests/functional_tests/test_general_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"preformatted/006_fmt_off_447.sql",
"preformatted/007_fmt_off_comments.sql",
"preformatted/008_reserved_names.sql",
"preformatted/009_empty.sql",
"preformatted/010_comment_only.sql",
"preformatted/301_multiline_jinjafmt.sql",
"preformatted/302_jinjafmt_multiline_str.sql",
"preformatted/303_jinjafmt_more_mutliline_str.sql",
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from sqlfmt.analyzer import Analyzer


Expand All @@ -6,3 +8,17 @@ def test_whitespace_formatting(default_analyzer: Analyzer) -> None:
expected_string = "select 1\nfrom my_table\nwhere true\n"
q = default_analyzer.parse_query(source_string=source_string)
assert str(q) == expected_string


def test_only_comment_formatting(default_analyzer: Analyzer) -> None:
source_string = "-- a comment"
expected_string = "-- a comment\n"
q = default_analyzer.parse_query(source_string=source_string)
assert str(q) == expected_string


@pytest.mark.parametrize("source_string", ["", "\n"])
def test_empty_formatting(default_analyzer: Analyzer, source_string: str) -> None:
expected_string = ""
q = default_analyzer.parse_query(source_string=source_string)
assert str(q) == expected_string
Loading