Skip to content

Commit 8b03427

Browse files
committed
Fix grouping of comments (fixes andialbrecht#772).
The grouping of comments was a bit too greedy by also consuming whitespaces at the end.
1 parent 5a24e36 commit 8b03427

5 files changed

Lines changed: 18 additions & 13 deletions

File tree

CHANGELOG

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
Development Version
22
-------------------
33

4-
Nothing yet.
4+
Bug Fixes
5+
6+
* The strip comments filter was a bit greedy and removed too much
7+
whitespace (issue772).
8+
Note: In some cases you might want to add `strip_whitespace=True` where you
9+
previously used just `strip_comments=True`. `strip_comments` did some of the
10+
work that `strip_whitespace` should do.
511

612

713
Release 0.5.0 (Apr 13, 2024)

sqlparse/engine/grouping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def group_comments(tlist):
314314
tidx, token = tlist.token_next_by(t=T.Comment)
315315
while token:
316316
eidx, end = tlist.token_not_matching(
317-
lambda tk: imt(tk, t=T.Comment) or tk.is_whitespace, idx=tidx)
317+
lambda tk: imt(tk, t=T.Comment) or tk.is_newline, idx=tidx)
318318
if end is not None:
319319
eidx, end = tlist.token_prev(eidx, skip_ws=False)
320320
tlist.group_tokens(sql.Comment, tidx, eidx)

sqlparse/sql.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Token:
4646
"""
4747

4848
__slots__ = ('value', 'ttype', 'parent', 'normalized', 'is_keyword',
49-
'is_group', 'is_whitespace')
49+
'is_group', 'is_whitespace', 'is_newline')
5050

5151
def __init__(self, ttype, value):
5252
value = str(value)
@@ -56,6 +56,7 @@ def __init__(self, ttype, value):
5656
self.is_group = False
5757
self.is_keyword = ttype in T.Keyword
5858
self.is_whitespace = self.ttype in T.Whitespace
59+
self.is_newline = self.ttype in T.Newline
5960
self.normalized = value.upper() if self.is_keyword else value
6061

6162
def __str__(self):

tests/test_format.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ def test_strip_comments_multi(self):
7373
assert res == 'select'
7474
sql = '/* sql starts here */ select'
7575
res = sqlparse.format(sql, strip_comments=True)
76-
assert res == 'select'
76+
assert res == ' select' # note whitespace is preserved, see issue 772
7777
sql = '/*\n * sql starts here\n */\nselect'
7878
res = sqlparse.format(sql, strip_comments=True)
7979
assert res == 'select'
8080
sql = 'select (/* sql starts here */ select 2)'
81-
res = sqlparse.format(sql, strip_comments=True)
81+
res = sqlparse.format(sql, strip_comments=True, strip_whitespace=True)
8282
assert res == 'select (select 2)'
8383
sql = 'select (/* sql /* starts here */ select 2)'
84-
res = sqlparse.format(sql, strip_comments=True)
84+
res = sqlparse.format(sql, strip_comments=True, strip_whitespace=True)
8585
assert res == 'select (select 2)'
8686

8787
def test_strip_comments_preserves_linebreak(self):
@@ -100,6 +100,11 @@ def test_strip_comments_preserves_linebreak(self):
100100
sql = 'select * -- a comment\n\nfrom foo'
101101
res = sqlparse.format(sql, strip_comments=True)
102102
assert res == 'select *\n\nfrom foo'
103+
104+
def test_strip_comments_preserves_whitespace(self):
105+
sql = 'SELECT 1/*bar*/ AS foo' # see issue772
106+
res = sqlparse.format(sql, strip_comments=True)
107+
assert res == 'SELECT 1 AS foo'
103108

104109
def test_strip_ws(self):
105110
f = lambda sql: sqlparse.format(sql, strip_whitespace=True)

tests/test_grouping.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ def test_grouping_parenthesis():
1717
assert len(parsed.tokens[2].tokens[3].tokens) == 3
1818

1919

20-
def test_grouping_comments():
21-
s = '/*\n * foo\n */ \n bar'
22-
parsed = sqlparse.parse(s)[0]
23-
assert str(parsed) == s
24-
assert len(parsed.tokens) == 2
25-
26-
2720
@pytest.mark.parametrize('s', ['foo := 1;', 'foo := 1'])
2821
def test_grouping_assignment(s):
2922
parsed = sqlparse.parse(s)[0]

0 commit comments

Comments
 (0)