Skip to content

Commit c819914

Browse files
committed
Fix to format ternary expression in BasicFormat
1 parent 77c39f9 commit c819914

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ open class BasicFormat: SyntaxRewriter {
223223
(.singleQuote, .rawStringDelimiter), // closing raw string delimiter should never be separate by a space
224224
(.stringQuote, .rawStringDelimiter), // closing raw string delimiter should never be separate by a space
225225
(.stringSegment, _),
226-
(_, .colon),
227226
(_, .comma),
228227
(_, .ellipsis),
229228
(_, .eof),
@@ -237,6 +236,13 @@ open class BasicFormat: SyntaxRewriter {
237236
(_, nil),
238237
(nil, _):
239238
return false
239+
case (_, .colon):
240+
let isTernaryExpr = second?.parent?.is(UnresolvedTernaryExprSyntax.self) == true
241+
|| second?.parent?.is(TernaryExprSyntax.self) == true
242+
243+
if !isTernaryExpr {
244+
return false
245+
}
240246
case (.leftAngle, _) where second?.tokenKind != .rightAngle: // `<` and `>` need to be separated by a space because otherwise they become an operator
241247
return false
242248
case (_, .rightAngle) where first?.tokenKind != .leftAngle: // `<` and `>` need to be separated by a space because otherwise they become an operator

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
14561456
FixIt(
14571457
message: InsertTokenFixIt(missingNodes: [Syntax(node.colonMark), Syntax(nextSibling)]),
14581458
changes: [
1459-
.makePresent(node.colonMark, leadingTrivia: .space),
1459+
.makePresent(node.colonMark),
14601460
.makePresent(nextSibling),
14611461
]
14621462
)

Tests/SwiftSyntaxBuilderTest/TernaryExprTests.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import SwiftSyntax
1515
import SwiftSyntaxBuilder
1616

1717
final class TernaryExprTests: XCTestCase {
18-
func testTernaryExpr() {
18+
func testStringLiteralTernaryExpr() {
1919
let buildable = ExprSyntax("true ? a : b")
2020
assertBuildResult(
2121
buildable,
@@ -24,4 +24,32 @@ final class TernaryExprTests: XCTestCase {
2424
"""
2525
)
2626
}
27+
28+
func testTernarySequenceExpr() {
29+
let buildable = SequenceExprSyntax {
30+
BooleanLiteralExprSyntax(true)
31+
UnresolvedTernaryExprSyntax(firstChoice: IntegerLiteralExprSyntax(1))
32+
IntegerLiteralExprSyntax(0)
33+
}
34+
assertBuildResult(
35+
buildable,
36+
"""
37+
true ? 1 : 0
38+
"""
39+
)
40+
}
41+
42+
func testTernaryExpr() {
43+
let buildable = TernaryExprSyntax(
44+
conditionExpression: BooleanLiteralExprSyntax(true),
45+
firstChoice: IntegerLiteralExprSyntax(1),
46+
secondChoice: IntegerLiteralExprSyntax(0)
47+
)
48+
assertBuildResult(
49+
buildable,
50+
"""
51+
true ? 1 : 0
52+
"""
53+
)
54+
}
2755
}

0 commit comments

Comments
 (0)