Skip to content

Commit a076b16

Browse files
committed
Fix OrderedImports to treat file ignore directive comment as file header
1 parent 3b423c6 commit a076b16

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Sources/SwiftFormat/Rules/OrderedImports.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ public final class OrderedImports: SyntaxFormatRule {
8686
if atStartOfFile {
8787
switch line.type {
8888
case .comment:
89-
commentBuffer.append(line)
89+
if line.description.contains("swift-format-ignore-file") {
90+
fileHeader.append(line)
91+
} else {
92+
commentBuffer.append(line)
93+
}
9094
continue
9195

9296
case .blankLine:
@@ -520,8 +524,8 @@ fileprivate class Line {
520524
}
521525
}
522526

523-
extension Line: CustomDebugStringConvertible {
524-
var debugDescription: String {
527+
extension Line: CustomStringConvertible {
528+
var description: String {
525529
var description = ""
526530
if !leadingTrivia.isEmpty {
527531
var newlinesCount = 0

Tests/SwiftFormatTests/Rules/OrderedImportsTests.swift

+32
Original file line numberDiff line numberDiff line change
@@ -651,4 +651,36 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
651651
]
652652
)
653653
}
654+
655+
func testImportsOrderWithFileIgnoreDirective() {
656+
assertFormatting(
657+
OrderedImports.self,
658+
input: """
659+
// swift-format-ignore-file: DoNotUseSemicolons, FullyIndirectEnum
660+
// Line comment for Zoo
661+
import Zoo
662+
// Line comment for Array
663+
1️⃣import Arrays
664+
665+
struct Foo {
666+
func foo() { bar();baz(); }
667+
}
668+
""",
669+
expected: """
670+
// swift-format-ignore-file: DoNotUseSemicolons, FullyIndirectEnum
671+
672+
// Line comment for Array
673+
import Arrays
674+
// Line comment for Zoo
675+
import Zoo
676+
677+
struct Foo {
678+
func foo() { bar();baz(); }
679+
}
680+
""",
681+
findings: [
682+
FindingSpec("1️⃣", message: "sort import statements lexicographically")
683+
]
684+
)
685+
}
654686
}

0 commit comments

Comments
 (0)