Repository navigation
Conversation
The splitter treated a newline after a terminating ";" as non-whitespace,
cutting a whitespace-only statement that was dropped at end of stream. A
trailing space or tab was kept but a trailing newline was lost, so
str(parse("select 1;\n")[0]) returned "select 1;" and round-tripping failed
for the common "...;\n" end-of-file case.
Treat newlines as the whitespace they are when deciding whether a finished
statement continues, so trailing whitespace (newlines included) stays with
its statement. Adds a regression test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sqlparseround-trips losslessly —''.join(str(s) for s in sqlparse.parse(sql))should equalsql(asserted intest_split_comment_end_of_line). But a trailing newline after the final;was dropped:str(sqlparse.parse("select 1;\n")[0])returned"select 1;", while a trailing space was kept — inconsistent. Reading a normal.sqlfile (which ends in;\n) and re-emitting it silently lost the final newline.Cause
In
StatementSplitter.process, the “statement finished” check usedttype not in (T.Whitespace, T.Comment.Single)— a tuple==test that doesn’t matchT.Whitespace.Newline(a subtype ofT.Whitespace). A trailing newline therefore started a new whitespace-only statement that the end-of-stream guard discarded.Fix
Treat newlines as the whitespace they are when deciding whether the finished statement continues, so trailing whitespace (newlines included) stays with its statement.
split()strips statements, so its output is unchanged.Tests
Adds
test_split_preserves_trailing_whitespace. Existing split/parse tests still pass; the fix was verified with the full suite green and a ~5,900-case round-trip fuzz clean.