From ea17ab2aac02e65978e425fdab1b20258a4b6da1 Mon Sep 17 00:00:00 2001 From: Sergei Stropysh Date: Fri, 14 Mar 2025 00:17:51 +0300 Subject: [PATCH] bugfix ISSUE_801; Remove all comments when only comments --- sqlparse/filters/others.py | 1 + tests/test_format.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/sqlparse/filters/others.py b/sqlparse/filters/others.py index 40a808df..cdb63f0d 100644 --- a/sqlparse/filters/others.py +++ b/sqlparse/filters/others.py @@ -65,6 +65,7 @@ def _get_insert_token(token): if prev_ is not None and not prev_.match(T.Punctuation, '('): tlist.tokens.insert(tidx, _get_insert_token(token)) tlist.tokens.remove(token) + tidx -= 1 else: tlist.tokens[tidx] = _get_insert_token(token) diff --git a/tests/test_format.py b/tests/test_format.py index 4dae299d..0cdbcf88 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -61,6 +61,12 @@ def test_strip_comments_single(self): 'from foo--comment\nf' res = sqlparse.format(sql, strip_comments=True) assert res == 'select a\nfrom foo\nf' + sql = '--A;--B;' + res = '' + assert res == sqlparse.format(sql, strip_comments=True) + sql = '--A;\n--B;' + res = '' + assert res == sqlparse.format(sql, strip_comments=True) def test_strip_comments_invalid_option(self): sql = 'select-- foo\nfrom -- bar\nwhere' @@ -77,6 +83,12 @@ def test_strip_comments_multi(self): sql = '/*\n * sql starts here\n */\nselect' res = sqlparse.format(sql, strip_comments=True) assert res == 'select' + sql = '/* sql starts here */' + res = sqlparse.format(sql, strip_comments=True) + assert res == '' + sql = '/* sql starts here */\n/* or here */' + res = sqlparse.format(sql, strip_comments=True, strip_whitespace=True) + assert res == '' sql = 'select (/* sql starts here */ select 2)' res = sqlparse.format(sql, strip_comments=True, strip_whitespace=True) assert res == 'select (select 2)'