From 53238b5358da1f52162ea0ade2b75ec2dc1bd576 Mon Sep 17 00:00:00 2001 From: Shawn Hyam Date: Mon, 29 Apr 2024 13:08:40 -0400 Subject: [PATCH] Make sure there is a break after an #endif. You wouldn't normally allow a line break just before the comma in a parameter list, but if the preceding line is an #endif, we have to include the break or we'll generate invalid code. --- .../PrettyPrint/TokenStreamCreator.swift | 1 + .../PrettyPrint/IfConfigTests.swift | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift b/Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift index 1023d886f..76592a48d 100644 --- a/Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift +++ b/Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift @@ -3911,6 +3911,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { before(clause.poundKeyword, tokens: .break(.contextual, size: 0)) } before(postfixIfExpr.config.poundEndif, tokens: .break(.contextual, size: 0)) + after(postfixIfExpr.config.poundEndif, tokens: .break(.same, size: 0)) return insertContextualBreaks(base, isTopLevel: false) } else if let callingExpr = expr.asProtocol(CallingExprSyntaxProtocol.self) { diff --git a/Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift b/Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift index 4f8026ab4..5a6113529 100644 --- a/Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift +++ b/Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift @@ -516,4 +516,19 @@ final class IfConfigTests: PrettyPrintTestCase { assertPrettyPrintEqual(input: input, expected: expected, linelength: 45) } + + func testPostfixPoundIfInParameterList() { + let input = + """ + print( + 32 + #if true + .foo + #endif + , 22 + ) + + """ + assertPrettyPrintEqual(input: input, expected: input, linelength: 45) + } }