Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sources/SwiftBasicFormat/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ open class BasicFormat: SyntaxRewriter {
(.singleQuote, .rawStringDelimiter), // closing raw string delimiter should never be separate by a space
(.stringQuote, .rawStringDelimiter), // closing raw string delimiter should never be separate by a space
(.stringSegment, _),
(_, .colon),
(_, .comma),
(_, .ellipsis),
(_, .eof),
Expand All @@ -237,6 +236,12 @@ open class BasicFormat: SyntaxRewriter {
(_, nil),
(nil, _):
return false
case (_, .colon):
if second?.keyPathInParent != \TernaryExprSyntax.colonMark
&& second?.keyPathInParent != \UnresolvedTernaryExprSyntax.colonMark
{
return false
}
case (.leftAngle, _) where second?.tokenKind != .rightAngle: // `<` and `>` need to be separated by a space because otherwise they become an operator
return false
case (_, .rightAngle) where first?.tokenKind != .leftAngle: // `<` and `>` need to be separated by a space because otherwise they become an operator
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ final class DeclarationTests: XCTestCase {
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"])
],
fixedSource: """
_ = foo/* */?.description: <#expression#>
_ = foo/* */?.description : <#expression#>
"""
)

Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ final class ExpressionTests: XCTestCase {
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"])
],
fixedSource: """
foo ? 1: <#expression#>
foo ? 1 : <#expression#>
"""
)
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftParserTest/translated/InvalidIfExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class InvalidIfExprTests: XCTestCase {
diagnostics: [
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"])
],
fixedSource: "(a ? b: <#expression#>)"
fixedSource: "(a ? b : <#expression#>)"
)
}

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

Expand All @@ -52,7 +52,7 @@ final class InvalidIfExprTests: XCTestCase {
fixIts: ["insert ')'"]
),
],
fixedSource: "(a ? b ? c : d: <#expression#>)"
fixedSource: "(a ? b ? c : d : <#expression#>)"
)
}

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

Expand Down
30 changes: 29 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/TernaryExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import SwiftSyntax
import SwiftSyntaxBuilder

final class TernaryExprTests: XCTestCase {
func testTernaryExpr() {
func testStringLiteralTernaryExpr() {
let buildable = ExprSyntax("true ? a : b")
assertBuildResult(
buildable,
Expand All @@ -24,4 +24,32 @@ final class TernaryExprTests: XCTestCase {
"""
)
}

func testTernarySequenceExpr() {
let buildable = SequenceExprSyntax {
BooleanLiteralExprSyntax(true)
UnresolvedTernaryExprSyntax(firstChoice: IntegerLiteralExprSyntax(1))
IntegerLiteralExprSyntax(0)
}
assertBuildResult(
buildable,
"""
true ? 1 : 0
"""
)
}

func testTernaryExpr() {
let buildable = TernaryExprSyntax(
conditionExpression: BooleanLiteralExprSyntax(true),
firstChoice: IntegerLiteralExprSyntax(1),
secondChoice: IntegerLiteralExprSyntax(0)
)
assertBuildResult(
buildable,
"""
true ? 1 : 0
"""
)
}
}