Skip to content

Commit 3e11365

Browse files
authored
Merge pull request swiftlang#26 from allevato/sr-11202
Prevent breaking at the space in `else {` of a `guard` statement.
2 parents db972aa + cb4e334 commit 3e11365

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
410410
func visit(_ node: GuardStmtSyntax) -> SyntaxVisitorContinueKind {
411411
after(node.guardKeyword, tokens: .break)
412412
before(node.elseKeyword, tokens: .break(.reset), .open)
413-
after(node.elseKeyword, tokens: .break)
413+
after(node.elseKeyword, tokens: .space)
414414
before(node.body.leftBrace, tokens: .close)
415415

416416
arrangeBracesAndContents(

Tests/SwiftFormatPrettyPrintTests/GuardStmtTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,27 @@ public class GuardStmtTests: PrettyPrintTestCase {
8484

8585
assertPrettyPrintEqual(input: input, expected: expected, linelength: 35)
8686
}
87+
88+
public func testOpenBraceIsGluedToElseKeyword() {
89+
let input =
90+
"""
91+
guard let foo = something,
92+
let bar = somethingElse else
93+
{
94+
body()
95+
}
96+
"""
97+
98+
let expected =
99+
"""
100+
guard let foo = something,
101+
let bar = somethingElse
102+
else {
103+
body()
104+
}
105+
106+
"""
107+
108+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 60)
109+
}
87110
}

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ extension GuardStmtTests {
242242
static let __allTests__GuardStmtTests = [
243243
("testGuardStatement", testGuardStatement),
244244
("testGuardWithFuncCall", testGuardWithFuncCall),
245+
("testOpenBraceIsGluedToElseKeyword", testOpenBraceIsGluedToElseKeyword),
245246
]
246247
}
247248

0 commit comments

Comments
 (0)