Skip to content

Commit bd92458

Browse files
committed
Preserve comments at start of conditionally compiled block in OrderedImports
1 parent db2958c commit bd92458

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

Sources/SwiftFormat/Rules/OrderedImports.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@ public final class OrderedImports: SyntaxFormatRule {
2727

2828
public override func visit(_ node: SourceFileSyntax) -> SourceFileSyntax {
2929
var newNode = node
30-
newNode.statements = orderImports(in: node.statements, atStartOfFile: true)
30+
newNode.statements = orderImports(in: node.statements)
3131
return newNode
3232
}
3333

34-
private func orderImports(
35-
in codeBlockItemList: CodeBlockItemListSyntax,
36-
atStartOfFile: Bool
37-
) -> CodeBlockItemListSyntax {
34+
private func orderImports(in codeBlockItemList: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax {
3835
let lines = generateLines(codeBlockItemList: codeBlockItemList, context: context)
3936

4037
// Stores the formatted and sorted lines that will be used to reconstruct the list of code block
@@ -47,7 +44,7 @@ public final class OrderedImports: SyntaxFormatRule {
4744
var testableImports: [Line] = []
4845
var codeBlocks: [Line] = []
4946
var fileHeader: [Line] = []
50-
var atStartOfFile = atStartOfFile
47+
var atStartOfFile = true
5148
var commentBuffer: [Line] = []
5249

5350
func formatAndAppend(linesSection: ArraySlice<Line>) {
@@ -56,6 +53,10 @@ public final class OrderedImports: SyntaxFormatRule {
5653
// Perform linting on the grouping of the imports.
5754
checkGrouping(linesSection)
5855

56+
if let firstLine = fileHeader.first, firstLine.type == .blankLine {
57+
fileHeader.removeFirst()
58+
}
59+
5960
if let lastLine = fileHeader.last, lastLine.type == .blankLine {
6061
fileHeader.removeLast()
6162
}
@@ -138,7 +139,7 @@ public final class OrderedImports: SyntaxFormatRule {
138139
return clause
139140
}
140141
var newClause = clause
141-
var newCodeBlockItemList = orderImports(in: codeBlockItemList, atStartOfFile: false)
142+
var newCodeBlockItemList = orderImports(in: codeBlockItemList)
142143
newCodeBlockItemList.leadingTrivia = .newline + newCodeBlockItemList.leadingTrivia
143144
newClause.elements = .statements(newCodeBlockItemList)
144145
return newClause

Tests/SwiftFormatTests/Rules/OrderedImportsTests.swift

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
706706
}
707707

708708
func testNestedConditionalImports() {
709-
var configuration = Configuration()
709+
var configuration = Configuration.forTesting
710710
configuration.orderedImports.includeConditionalImports = true
711711

712712
assertFormatting(
@@ -949,4 +949,73 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
949949
]
950950
)
951951
}
952+
953+
func testPreservesEmptyConditionalCompilationBlock() {
954+
var configuration = Configuration.forTesting
955+
configuration.orderedImports.includeConditionalImports = true
956+
957+
let code = """
958+
import Apples
959+
import Zebras
960+
961+
#if FOO
962+
#endif
963+
964+
foo()
965+
"""
966+
967+
assertFormatting(
968+
OrderedImports.self,
969+
input: code,
970+
expected: code,
971+
configuration: configuration
972+
)
973+
}
974+
975+
func testPreservesHeaderCommentInConditionalCompilationBlock() {
976+
var configuration = Configuration.forTesting
977+
configuration.orderedImports.includeConditionalImports = true
978+
979+
let code = """
980+
import Apples
981+
982+
#if FOO
983+
// Performing FOO-specific logic
984+
985+
import Foundation
986+
#endif
987+
988+
foo()
989+
"""
990+
991+
assertFormatting(
992+
OrderedImports.self,
993+
input: code,
994+
expected: code,
995+
configuration: configuration
996+
)
997+
}
998+
999+
func testPreservesCommentsOnlyInConditionalCompilationBlock() {
1000+
var configuration = Configuration.forTesting
1001+
configuration.orderedImports.includeConditionalImports = true
1002+
1003+
let code = """
1004+
import Apples
1005+
1006+
#if FOO
1007+
// Just a comment
1008+
// Another comment
1009+
#endif
1010+
1011+
foo()
1012+
"""
1013+
1014+
assertFormatting(
1015+
OrderedImports.self,
1016+
input: code,
1017+
expected: code,
1018+
configuration: configuration
1019+
)
1020+
}
9521021
}

0 commit comments

Comments
 (0)