mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Fix conditional import parsing with access modifiers (#2494)
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
committed by
Cal Stephens
co-authored by
Codex
parent
78836b9959
commit
4ace97e09d
@@ -2496,8 +2496,10 @@ extension Formatter {
|
||||
}
|
||||
let nextToken = tokens[nextTokenIndex]
|
||||
let isImportKeyword = nextToken == .keyword("import")
|
||||
// Access modifier (e.g. "public") can precede "import" on the same line (Swift 6)
|
||||
let isAccessModifierBeforeImport = nextToken.isKeyword && _FormatRules.aclModifiers.contains(nextToken.string)
|
||||
// Access modifiers only continue the import block when they are immediately followed by import.
|
||||
let isAccessModifierBeforeImport = nextToken.isKeyword &&
|
||||
_FormatRules.aclModifiers.contains(nextToken.string) &&
|
||||
next(.nonSpaceOrComment, after: nextTokenIndex) == .keyword("import")
|
||||
if !isImportKeyword, !isAccessModifierBeforeImport {
|
||||
// End of imports
|
||||
pushStack()
|
||||
|
||||
@@ -409,6 +409,20 @@ final class SortImportsTests: XCTestCase {
|
||||
testFormatting(for: input, output, rule: .sortImports)
|
||||
}
|
||||
|
||||
func testNoMangleConditionalImportsFollowedByPrivateDeclaration() {
|
||||
let input = """
|
||||
#if canImport(UIKit)
|
||||
import UIKit
|
||||
private struct Foo {}
|
||||
#elseif canImport(AppKit)
|
||||
import AppKit
|
||||
private struct Foo {}
|
||||
#endif
|
||||
"""
|
||||
let options = FormatOptions(swiftVersion: "6.1")
|
||||
testFormatting(for: input, rule: .sortImports, options: options, exclude: [.blankLineAfterImports, .indent])
|
||||
}
|
||||
|
||||
func testNoMoveSwiftToolsVersionLine() {
|
||||
let input = """
|
||||
// swift-tools-version: 6.2
|
||||
|
||||
Reference in New Issue
Block a user