Skip to content
Open
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
6 changes: 5 additions & 1 deletion sqlparse/filters/aligned_indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ def _process_case(self, tlist):
cases = tlist.get_cases(skip_ws=True)
# align the end as well
end_token = tlist.token_next_by(m=(T.Keyword, 'END'))[1]
cases.append((None, [end_token]))
# A malformed CASE (e.g. "case,end", which groups as an IdentifierList)
# has no standalone END keyword; skip the end alignment rather than
# appending a (None, [None]) entry that later crashes insert_before.
if end_token is not None:
cases.append((None, [end_token]))

condition_width = [len(' '.join(map(str, cond))) if cond else 0
for cond, _ in cases]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,3 +784,14 @@ def test_strip_ws_removes_trailing_ws_in_groups(): # issue782
strip_whitespace=True)
expected = '(where foo = bar) from'
assert formatted == expected


def test_aligned_indent_case_without_end():
# A malformed CASE with no standalone END keyword (e.g. "case,end", which
# groups as an IdentifierList) must not crash the aligned-indent filter.
assert sqlparse.format('case,end', reindent_aligned=True) == 'case,end'
# A well-formed CASE is still aligned across its branches.
formatted = sqlparse.format(
'select case when 1 then 2 else 3 end from t', reindent_aligned=True)
assert 'when 1 then 2' in formatted
assert 'else 3' in formatted