Preserve shebang line when source file contains no imports#1207
Open
itsybitsybootsy wants to merge 1 commit into
Open
Preserve shebang line when source file contains no imports#1207itsybitsybootsy wants to merge 1 commit into
itsybitsybootsy wants to merge 1 commit into
Conversation
The rule walks the source file's top-level statements, reorders any imports, and rebuilds the surrounding trivia. When there are no imports to reorder it still runs the rebuild step, which can collapse the trailing newline that separates a `#!` shebang line from the first statement's leading comment. The CLI output then renders the shebang fused with that comment on a single line. When the rule finds no import statements the result is the unchanged input. Returning the original `CodeBlockItemListSyntax` in that case preserves shebang and file-header trivia exactly and avoids the fusion. Adds GarbageText tests for shebang followed by a leading comment and for shebang separated from a leading comment by a blank line, exercised through both the pretty-print path and the full SwiftFormatter pipeline so the regression doesn't slip back through one of them.
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
OrderedImportsrewrites every source file, including ones with no imports at all. For an input likethe rule's
convertToCodeBlockItemspass rebuilds the AST and ends up gluing the comment onto the shebang line:which is no longer a valid shebang. The variant with a blank line between the shebang and the comment is also broken, and the output is non-idempotent (the first format pass deletes the blank line, the second pass produces the collapsed version above).
The root cause is that
orderImports(in:)is called unconditionally, even on files where the rule has nothing to do. The rebuild then loses the trivia attached to the shebang because the shebang has no syntax node of its own.Resolves #1194.
Fix
Short-circuit
orderImports(in:)at the top: if none of theLines the generator produced are of any import type (.regularImport,.declImport,.implementationOnlyImport,.testableImport), return the originalCodeBlockItemListSyntaxunchanged. The rule has nothing to reorder, so its output should equal its input down to the trivia.After the fix, both reproducers in the issue format to themselves:
and
Scope note
This PR fixes the no-imports cases from the issue. The mixed case where a file has both a shebang and imports is a separate, broader bug in how
OrderedImportsrebuilds trivia around the file header and is intentionally not addressed here.Tests
Added two tests to
GarbageTextTests.swift:testHashBangFollowedByLineComment— covers the first reproducer; runs both through the pretty-printer and through the fullSwiftFormatterpipeline (the bug only surfaced via the full pipeline, not the pretty-printer alone).testHashBangFollowedByBlankLineAndComment— covers the second reproducer.swift testlocally: 915 tests, 1 skipped, 0 failures.This contribution was developed with the help of Claude Code. The fix, regression tests, and the full local test suite were reviewed manually before submitting.