Skip to content

Commit

Permalink
Fix missing break in nested IfConfig decls. Fixes swiftlang#779.
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnhyam authored and ahoppen committed Jul 26, 2024
1 parent adccf1b commit 117277f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,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
}

Expand Down
26 changes: 26 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 117277f

Please sign in to comment.