Skip to content

Commit c08d37d

Browse files
authored
Merge pull request swiftlang#147 from google/fix-as-punctuation
Fix breaking around `as` when followed by `?` or `!`.
2 parents c4ef0f5 + 22db84b commit c08d37d

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
526526

527527
override func visit(_ node: AsExprSyntax) {
528528
before(node.asTok, tokens: .space)
529-
after(node.asTok, tokens: .break)
529+
before(node.typeName.firstToken, tokens: .break)
530530
super.visit(node)
531531
}
532532

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
public class AsExprTests: PrettyPrintTestCase {
2+
public func testWithoutPunctuation() {
3+
let input =
4+
"""
5+
func foo() {
6+
let a = b as Int
7+
let c = d
8+
as
9+
Int
10+
let reallyLongVariableName = x as ReallyLongTypeName
11+
}
12+
"""
13+
14+
let expected =
15+
"""
16+
func foo() {
17+
let a = b as Int
18+
let c = d as Int
19+
let reallyLongVariableName =
20+
x as ReallyLongTypeName
21+
}
22+
23+
"""
24+
25+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
26+
}
27+
28+
public func testWithPunctuation() {
29+
let input =
30+
"""
31+
func foo() {
32+
let a = b as? Int
33+
let c = d
34+
as!
35+
Int
36+
let reallyLongVariableName = x as? ReallyLongTypeName
37+
}
38+
"""
39+
40+
let expected =
41+
"""
42+
func foo() {
43+
let a = b as? Int
44+
let c = d as! Int
45+
let reallyLongVariableName =
46+
x as? ReallyLongTypeName
47+
}
48+
49+
"""
50+
51+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
52+
}
53+
}

0 commit comments

Comments
 (0)