Preserve trailing comments in OrderedImports rule#1205
Merged
allevato merged 2 commits intoMay 15, 2026
Merged
Conversation
The `OrderedImports` rule runs every source file through a `convertToCodeBlockItems` pass that accumulates trivia in `pendingLeadingTrivia` and flushes it onto each successive syntax node. When the input ends with trivia after the last syntax node — typically a comment that import reordering pushed past the final import — the accumulator is silently dropped at the end of the loop. Append any leftover trivia containing comments to the trailing trivia of the last code block item before returning. Trailing newlines that the loop synthesised for separation before nonexistent following lines are stripped first so the output doesn't gain spurious blank lines. Resolves swiftlang#1080.
allevato
reviewed
May 15, 2026
Comment on lines
+489
to
+497
| let hasComment = pendingLeadingTrivia.contains { piece in | ||
| switch piece { | ||
| case .lineComment, .blockComment, .docLineComment, .docBlockComment: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
| if hasComment, !output.isEmpty { |
Member
There was a problem hiding this comment.
Suggested change
| let hasComment = pendingLeadingTrivia.contains { piece in | |
| switch piece { | |
| case .lineComment, .blockComment, .docLineComment, .docBlockComment: | |
| return true | |
| default: | |
| return false | |
| } | |
| } | |
| if hasComment, !output.isEmpty { | |
| if pendingLeadingTrivia.hasAnyComments, !output.isEmpty { |
We already have a helper for this in Trivia+Convenience.swift.
Contributor
Author
There was a problem hiding this comment.
Done in a7dc786. Wrapped in Trivia(pieces:) since pendingLeadingTrivia is [TriviaPiece].
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.
Problem
OrderedImportsruns every source file through aconvertToCodeBlockItemspass that walks the categorisedLinearray and flushes accumulated trivia onto each syntax node viapendingLeadingTrivia. When the input ends with trivia after the last syntax node — typically a free-standing comment that import reordering pushed past the final import — the accumulator is silently dropped at the end of the loop.becomes
The
// Commentis silently lost.Fix
Append any leftover trivia containing comments to the trailing trivia of the last code block item before
convertToCodeBlockItemsreturns. The loop synthesised a trailing run of newlines after every iteration to separate the current line from the next; those are meaningless when no following line exists and are stripped first so the output doesn't gain spurious blank lines.After the fix:
Tests
Added
testCommentBetweenImportGroupsIsPreservedtoOrderedImportsTests.swiftcovering the reproducer from the issue.swift testlocally: 913 tests, 1 skipped, 0 failures.Resolves #1080.
This contribution was developed with the help of Claude Code. The fix, regression test, and the full local test suite were reviewed manually before submitting.