Skip to content

Commit

Permalink
Descend into subgroups when removing whitespace (fixes #782).
Browse files Browse the repository at this point in the history
  • Loading branch information
andialbrecht committed Jul 15, 2024
1 parent a8de06e commit 0c4902f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Bug Fixes
* Fix error when splitting statements that contain multiple CASE clauses
within a BEGIN block (issue784).

* Fix whitespace removal with nested expressions (issue782).


Release 0.5.0 (Apr 13, 2024)
----------------------------
Expand Down
4 changes: 4 additions & 0 deletions sqlparse/filters/others.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def _stripws_parenthesis(self, tlist):
tlist.tokens.pop(1)
while tlist.tokens[-2].is_whitespace:
tlist.tokens.pop(-2)
if tlist.tokens[-2].is_group:
# save to remove the last whitespace
while tlist.tokens[-2].tokens[-1].is_whitespace:
tlist.tokens[-2].tokens.pop(-1)
self._stripws_default(tlist)

def process(self, stmt, depth=0):
Expand Down
13 changes: 10 additions & 3 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_strip_comments_preserves_linebreak(self):
sql = 'select * -- a comment\n\nfrom foo'
res = sqlparse.format(sql, strip_comments=True)
assert res == 'select *\n\nfrom foo'

def test_strip_comments_preserves_whitespace(self):
sql = 'SELECT 1/*bar*/ AS foo' # see issue772
res = sqlparse.format(sql, strip_comments=True)
Expand Down Expand Up @@ -734,8 +734,8 @@ def test_format_json_ops(): # issue542
"select foo->'bar', foo->'bar';", reindent=True)
expected = "select foo->'bar',\n foo->'bar';"
assert formatted == expected


@pytest.mark.parametrize('sql, expected_normal, expected_compact', [
('case when foo then 1 else bar end',
'case\n when foo then 1\n else bar\nend',
Expand All @@ -745,3 +745,10 @@ def test_compact(sql, expected_normal, expected_compact): # issue783
formatted_compact = sqlparse.format(sql, reindent=True, compact=True)
assert formatted_normal == expected_normal
assert formatted_compact == expected_compact


def test_strip_ws_removes_trailing_ws_in_groups(): # issue782
formatted = sqlparse.format('( where foo = bar ) from',
strip_whitespace=True)
expected = '(where foo = bar) from'
assert formatted == expected

0 comments on commit 0c4902f

Please sign in to comment.