File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,13 @@ def get_next_comment():
2626 if (prev_ is None or next_ is None
2727 or prev_ .is_whitespace or prev_ .match (T .Punctuation , '(' )
2828 or next_ .is_whitespace or next_ .match (T .Punctuation , ')' )):
29+ # Insert a whitespace to ensure the following SQL produces
30+ # a valid SQL (see #425). For example:
31+ #
32+ # Before: select a--comment\nfrom foo
33+ # After: select a from foo
34+ if prev_ is not None and next_ is None :
35+ tlist .tokens .insert (tidx , sql .Token (T .Whitespace , ' ' ))
2936 tlist .tokens .remove (token )
3037 else :
3138 tlist .tokens [tidx ] = sql .Token (T .Whitespace , ' ' )
Original file line number Diff line number Diff line change @@ -50,6 +50,19 @@ def test_strip_comments_single(self):
5050 sql = 'select-- foo\n from -- bar\n where'
5151 res = sqlparse .format (sql , strip_comments = True )
5252 assert res == 'select from where'
53+ sql = 'select *-- statement starts here\n \n from foo'
54+ res = sqlparse .format (sql , strip_comments = True )
55+ assert res == 'select * from foo'
56+ sql = 'select * from foo-- statement starts here\n where'
57+ res = sqlparse .format (sql , strip_comments = True )
58+ assert res == 'select * from foo where'
59+ sql = 'select a-- statement starts here\n from foo'
60+ res = sqlparse .format (sql , strip_comments = True )
61+ assert res == 'select a from foo'
62+ sql = '--comment\n select a-- statement starts here\n ' \
63+ 'from foo--comment\n f'
64+ res = sqlparse .format (sql , strip_comments = True )
65+ assert res == 'select a from foo f'
5366
5467 def test_strip_comments_invalid_option (self ):
5568 sql = 'select-- foo\n from -- bar\n where'
You can’t perform that action at this time.
0 commit comments