Skip to content

Commit 3ccb6cd

Browse files
authored
fix: make bracket comparison case insensitive (#401)
* fix: make bracket comparison case insensitive * chore: update changelog
1 parent 196ee41 commit 3ccb6cd

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
- fixed a bug where format-off tokens could cause sqlfmt to raise a bracket mismatch error ([#395](https://github.com/tconbeer/sqlfmt/issues/395) - thank you, [@AndrewLaneAtPowerSchool](https://github.com/AndrewLaneAtPowerSchool)!).
8+
79
## [0.17.0] - 2023-02-24
810

911
- sqlfmt now defaults to reading and writing files using the `utf-8` encoding. Previously, we used Python's default behavior of using the encoding from the host machine's locale. However, as `utf-8` becomes a de-facto standard, this was causing issues for some Windows users, whose locale was set to use older encodings. You can use the `--encoding` option to specify a different encoding. Setting encoding to `inherit`, e.g., `sqlfmt --encoding inherit foo.sql` will revert to the old behavior of using the host's locale. sqlfmt will detect and preserve a UTF BOM if it is present. If you specify `--encoding utf-8-sig`, sqlfmt will always write a UTF-8 BOM in the formatted file. ([#350](https://github.com/tconbeer/sqlfmt/issues/350), [#381]((https://github.com/tconbeer/sqlfmt/issues/381)), [#383]((https://github.com/tconbeer/sqlfmt/issues/383)) - thank you [@profesia-company](https://github.com/profesia-company), [@cmcnicoll](https://github.com/cmcnicoll), [@aersam](https://github.com/aersam), and [@ryanmeekins](https://github.com/ryanmeekins)!)

src/sqlfmt/node_manager.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ def raise_on_mismatched_bracket(self, token: Token, last_bracket: Node) -> None:
5757
"table<": ">",
5858
"struct<": ">",
5959
}
60+
last_bracket_value = last_bracket.value.lower()
6061
if (
6162
last_bracket.token.type
6263
not in (TokenType.BRACKET_OPEN, TokenType.STATEMENT_START)
63-
or last_bracket.value not in matches
64-
or matches[last_bracket.value] != token.token.lower()
64+
or last_bracket_value not in matches
65+
or matches[last_bracket_value] != token.token.lower()
6566
):
6667
raise SqlfmtBracketError(
6768
f"Closing bracket '{token.token}' found at {token.spos} does not "
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- source: https://github.com/tconbeer/sqlfmt/issues/395
2+
-- for now this should no-op
3+
CREATE OR REPLACE VIEW someview AS (
4+
WITH some_cte AS (SELECT CASE WHEN foo = bar THEN 1 ELSE 0 END AS biz FROM BAT)
5+
select * from some_cte
6+
)

tests/functional_tests/test_general_formatting.py

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"unformatted/409_create_external_function.sql",
7474
"unformatted/410_create_warehouse.sql",
7575
"unformatted/411_create_clone.sql",
76+
"unformatted/900_create_view.sql",
7677
"unformatted/999_unsupported_ddl.sql",
7778
],
7879
)

tests/unit_tests/test_analyzer.py

+6
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,12 @@ def test_unmatched_bracket_error(default_analyzer: Analyzer) -> None:
413413
assert "Closing bracket ')'" in str(excinfo.value)
414414

415415

416+
def test_case_insensitive_bracket_matching(default_analyzer: Analyzer) -> None:
417+
source_string = "--fmt: off\n select CASE when true then 1 end\n"
418+
q = default_analyzer.parse_query(source_string=source_string)
419+
assert q.nodes[-2].token.type is TokenType.STATEMENT_END
420+
421+
416422
@pytest.mark.parametrize(
417423
"source_string",
418424
[

0 commit comments

Comments
 (0)