Skip to content

Commit

Permalink
fix: support else in for loops (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
tconbeer authored Jan 22, 2024
1 parent ac1fd64 commit ef85182
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Bug Fixes

- `{% for %}...{% else %}...{% endfor %}` loops are now supported. Previously, a BracketError was raised if a `for` loop included an `else` tag ([#549](https://github.com/tconbeer/sqlfmt/issues/549) - thank you, [@yassun7010](https://github.com/yassun7010)!).

## [0.21.1] - 2023-12-19

### Bug Fixes
Expand Down
7 changes: 5 additions & 2 deletions src/sqlfmt/node_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ def raise_on_mismatched_jinja_tags(self, token: Token, start_tag: Node) -> None:
closes start_tag
"""
try:
if any(s in token.token.lower() for s in ["endif", "else", "elif"]):
if not any(s in start_tag.value for s in ["if", "else"]):
if "endif" in token.token.lower():
if not any(s in start_tag.value for s in ["if", "elif", "else"]):
raise ValueError
elif "endfor" in token.token.lower():
if not any(s in start_tag.value for s in ["for", "else"]):
raise ValueError
else:
end_text, _ = re.subn(r"[{}%\-\s]", "", token.token.lower())
Expand Down
7 changes: 7 additions & 0 deletions tests/data/unformatted/133_for_else.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% for i in range(5) %}
select 1
{% else %}
select 2
{% endfor %}
)))))__SQLFMT_OUTPUT__(((((
{% for i in range(5) %} select 1{% else %} select 2 {% endfor %}
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 @@ -49,6 +49,7 @@
"unformatted/129_duckdb_joins.sql",
"unformatted/130_athena_data_types.sql",
"unformatted/131_assignment_statement.sql",
"unformatted/133_for_else.sql",
"unformatted/200_base_model.sql",
"unformatted/201_basic_snapshot.sql",
"unformatted/202_unpivot_macro.sql",
Expand Down

0 comments on commit ef85182

Please sign in to comment.