Skip to content

Commit 1499cff

Browse files
committed
Preserve line breaks when removing comments (fixes andialbrecht#484).
1 parent ca6d149 commit 1499cff

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Notable Changes
1414
Bug Fixes
1515

1616
* Improved parsing of IN(...) statements (issue566, pr567 by hurcy).
17+
* Preserve line breaks when removing comments (issue484).
1718

1819

1920
Release 0.3.1 (Feb 29, 2020)

sqlparse/filters/others.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,29 @@
55
# This module is part of python-sqlparse and is released under
66
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
77

8+
import re
9+
810
from sqlparse import sql, tokens as T
911
from sqlparse.utils import split_unquoted_newlines
1012

1113

1214
class StripCommentsFilter:
15+
1316
@staticmethod
1417
def _process(tlist):
1518
def get_next_comment():
1619
# TODO(andi) Comment types should be unified, see related issue38
1720
return tlist.token_next_by(i=sql.Comment, t=T.Comment)
1821

22+
def _get_insert_token(token):
23+
"""Returns either a whitespace or the line breaks from token."""
24+
# See issue484 why line breaks should be preserved.
25+
m = re.search(r'((\r\n|\r|\n)+) *$', token.value)
26+
if m is not None:
27+
return sql.Token(T.Whitespace.Newline, m.groups()[0])
28+
else:
29+
return sql.Token(T.Whitespace, ' ')
30+
1931
tidx, token = get_next_comment()
2032
while token:
2133
pidx, prev_ = tlist.token_prev(tidx, skip_ws=False)
@@ -26,15 +38,12 @@ def get_next_comment():
2638
or prev_.is_whitespace or prev_.match(T.Punctuation, '(')
2739
or next_.is_whitespace or next_.match(T.Punctuation, ')')):
2840
# Insert a whitespace to ensure the following SQL produces
29-
# a valid SQL (see #425). For example:
30-
#
31-
# Before: select a--comment\nfrom foo
32-
# After: select a from foo
33-
if prev_ is not None and next_ is None:
34-
tlist.tokens.insert(tidx, sql.Token(T.Whitespace, ' '))
41+
# a valid SQL (see #425).
42+
if prev_ is not None and not prev_.match(T.Punctuation, '('):
43+
tlist.tokens.insert(tidx, _get_insert_token(token))
3544
tlist.tokens.remove(token)
3645
else:
37-
tlist.tokens[tidx] = sql.Token(T.Whitespace, ' ')
46+
tlist.tokens[tidx] = _get_insert_token(token)
3847

3948
tidx, token = get_next_comment()
4049

tests/test_format.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@ def test_identifiercase_quotes(self):
4141
def test_strip_comments_single(self):
4242
sql = 'select *-- statement starts here\nfrom foo'
4343
res = sqlparse.format(sql, strip_comments=True)
44-
assert res == 'select * from foo'
44+
assert res == 'select *\nfrom foo'
4545
sql = 'select * -- statement starts here\nfrom foo'
4646
res = sqlparse.format(sql, strip_comments=True)
47-
assert res == 'select * from foo'
47+
assert res == 'select *\nfrom foo'
4848
sql = 'select-- foo\nfrom -- bar\nwhere'
4949
res = sqlparse.format(sql, strip_comments=True)
50-
assert res == 'select from where'
50+
assert res == 'select\nfrom\nwhere'
5151
sql = 'select *-- statement starts here\n\nfrom foo'
5252
res = sqlparse.format(sql, strip_comments=True)
53-
assert res == 'select * from foo'
53+
assert res == 'select *\n\nfrom foo'
5454
sql = 'select * from foo-- statement starts here\nwhere'
5555
res = sqlparse.format(sql, strip_comments=True)
56-
assert res == 'select * from foo where'
56+
assert res == 'select * from foo\nwhere'
5757
sql = 'select a-- statement starts here\nfrom foo'
5858
res = sqlparse.format(sql, strip_comments=True)
59-
assert res == 'select a from foo'
59+
assert res == 'select a\nfrom foo'
6060
sql = '--comment\nselect a-- statement starts here\n' \
6161
'from foo--comment\nf'
6262
res = sqlparse.format(sql, strip_comments=True)
63-
assert res == 'select a from foo f'
63+
assert res == 'select a\nfrom foo\nf'
6464

6565
def test_strip_comments_invalid_option(self):
6666
sql = 'select-- foo\nfrom -- bar\nwhere'

0 commit comments

Comments
 (0)