Skip to content

Commit 9f61d4a

Browse files
authored
Merge pull request #1641 from gibachan/fix-ternary-expression
Insert a space before colon in ternary expression in Fix-It
2 parents ad86d77 + 4174e18 commit 9f61d4a

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 6 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,12 @@ open class BasicFormat: SyntaxRewriter {
237236
(_, nil),
238237
(nil, _):
239238
return false
239+
case (_, .colon):
240+
if second?.keyPathInParent != \TernaryExprSyntax.colonMark
241+
&& second?.keyPathInParent != \UnresolvedTernaryExprSyntax.colonMark
242+
{
243+
return false
244+
}
240245
case (.leftAngle, _) where second?.tokenKind != .rightAngle: // `<` and `>` need to be separated by a space because otherwise they become an operator
241246
return false
242247
case (_, .rightAngle) where first?.tokenKind != .leftAngle: // `<` and `>` need to be separated by a space because otherwise they become an operator

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ final class DeclarationTests: XCTestCase {
256256
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"])
257257
],
258258
fixedSource: """
259-
_ = foo/* */?.description: <#expression#>
259+
_ = foo/* */?.description : <#expression#>
260260
"""
261261
)
262262

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ final class ExpressionTests: XCTestCase {
836836
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"])
837837
],
838838
fixedSource: """
839-
foo ? 1: <#expression#>
839+
foo ? 1 : <#expression#>
840840
"""
841841
)
842842
}

Tests/SwiftParserTest/translated/InvalidIfExprTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class InvalidIfExprTests: XCTestCase {
2323
diagnostics: [
2424
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"])
2525
],
26-
fixedSource: "(a ? b: <#expression#>)"
26+
fixedSource: "(a ? b : <#expression#>)"
2727
)
2828
}
2929

@@ -35,7 +35,7 @@ final class InvalidIfExprTests: XCTestCase {
3535
diagnostics: [
3636
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"])
3737
],
38-
fixedSource: "(a ? b : c ? d: <#expression#>)"
38+
fixedSource: "(a ? b : c ? d : <#expression#>)"
3939
)
4040
}
4141

@@ -52,7 +52,7 @@ final class InvalidIfExprTests: XCTestCase {
5252
fixIts: ["insert ')'"]
5353
),
5454
],
55-
fixedSource: "(a ? b ? c : d: <#expression#>)"
55+
fixedSource: "(a ? b ? c : d : <#expression#>)"
5656
)
5757
}
5858

@@ -65,7 +65,7 @@ final class InvalidIfExprTests: XCTestCase {
6565
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"]),
6666
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"]),
6767
],
68-
fixedSource: "(a ? b ? c: <#expression#>: <#expression#>)"
68+
fixedSource: "(a ? b ? c : <#expression#> : <#expression#>)"
6969
)
7070
}
7171

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)