Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make bracket comparison case insensitive #401

Merged
merged 2 commits into from
Apr 12, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

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

## [0.17.0] - 2023-02-24

- 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)!)
Expand Down
5 changes: 3 additions & 2 deletions src/sqlfmt/node_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ def raise_on_mismatched_bracket(self, token: Token, last_bracket: Node) -> None:
"table<": ">",
"struct<": ">",
}
last_bracket_value = last_bracket.value.lower()
if (
last_bracket.token.type
not in (TokenType.BRACKET_OPEN, TokenType.STATEMENT_START)
or last_bracket.value not in matches
or matches[last_bracket.value] != token.token.lower()
or last_bracket_value not in matches
or matches[last_bracket_value] != token.token.lower()
):
raise SqlfmtBracketError(
f"Closing bracket '{token.token}' found at {token.spos} does not "
Expand Down
6 changes: 6 additions & 0 deletions tests/data/unformatted/900_create_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- source: https://github.com/tconbeer/sqlfmt/issues/395
-- for now this should no-op
CREATE OR REPLACE VIEW someview AS (
WITH some_cte AS (SELECT CASE WHEN foo = bar THEN 1 ELSE 0 END AS biz FROM BAT)
select * from some_cte
)
1 change: 1 addition & 0 deletions tests/functional_tests/test_general_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"unformatted/409_create_external_function.sql",
"unformatted/410_create_warehouse.sql",
"unformatted/411_create_clone.sql",
"unformatted/900_create_view.sql",
"unformatted/999_unsupported_ddl.sql",
],
)
Expand Down
6 changes: 6 additions & 0 deletions tests/unit_tests/test_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ def test_unmatched_bracket_error(default_analyzer: Analyzer) -> None:
assert "Closing bracket ')'" in str(excinfo.value)


def test_case_insensitive_bracket_matching(default_analyzer: Analyzer) -> None:
source_string = "--fmt: off\n select CASE when true then 1 end\n"
q = default_analyzer.parse_query(source_string=source_string)
assert q.nodes[-2].token.type is TokenType.STATEMENT_END


@pytest.mark.parametrize(
"source_string",
[
Expand Down