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 1 commit
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
Next Next commit
fix: make bracket comparison case insensitive
  • Loading branch information
tconbeer committed Apr 12, 2023
commit ca87b783c8499c3ca05ce730e2728ba62d239590
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