From 6b33c6698a8bf40edd7a9f32e8a3410d06c9e374 Mon Sep 17 00:00:00 2001 From: Shawn Hyam Date: Thu, 25 Jul 2024 13:09:07 -0400 Subject: [PATCH] Fix missing break in nested IfConfig decls. Fixes #779. --- .../PrettyPrint/TokenStreamCreator.swift | 2 ++ .../PrettyPrint/IfConfigTests.swift | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift b/Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift index 8e7dc617b..6cf77877a 100644 --- a/Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift +++ b/Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift @@ -1453,6 +1453,8 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { } override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind { + // there has to be a break after an #endif + after(node.poundEndif, tokens: .break(.same, size: 0)) return .visitChildren } diff --git a/Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift b/Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift index 5a6113529..4f4a85b3b 100644 --- a/Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift +++ b/Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift @@ -531,4 +531,30 @@ final class IfConfigTests: PrettyPrintTestCase { """ assertPrettyPrintEqual(input: input, expected: input, linelength: 45) } + + func testNestedPoundIfInSwitchStatement() { + let input = + """ + switch self { + #if os(iOS) || os(tvOS) || os(watchOS) + case .a: + return 40 + #if os(iOS) || os(tvOS) + case .e: + return 30 + #endif + #if os(iOS) + case .g: + return 2 + #endif + #endif + default: + return nil + } + + """ + var configuration = Configuration.forTesting + configuration.indentConditionalCompilationBlocks = false + assertPrettyPrintEqual(input: input, expected: input, linelength: 45, configuration: configuration) + } }