Skip to content

Commit 5eb8f82

Browse files
authored
Merge pull request #1677 from TTOzzi/fix-diagnostic-of-ternary-operator-missing-only-colon
2 parents b3d0d88 + 9bf8ff2 commit 5eb8f82

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,7 @@ extension Parser {
316316
)
317317

318318
let rhs: RawExprSyntax?
319-
if colon.isMissing {
320-
// If the colon is missing there's not much more structure we can
321-
// expect out of this expression sequence. Emit a missing expression
322-
// to end the parsing here.
319+
if colon.isMissing, currentToken.isAtStartOfLine {
323320
rhs = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
324321
} else {
325322
rhs = nil

Tests/SwiftParserTest/translated/InvalidIfExprTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,43 @@ final class InvalidIfExprTests: XCTestCase {
6969
)
7070
}
7171

72+
func testInvalidIfExpr5() {
73+
assertParse(
74+
"""
75+
foo ? 1 1️⃣2
76+
""",
77+
diagnostics: [
78+
DiagnosticSpec(message: "expected ':' after '? ...' in ternary expression", fixIts: ["insert ':'"])
79+
],
80+
fixedSource: "foo ? 1 : 2"
81+
)
82+
}
83+
84+
func testInvalidIfExpr6() {
85+
assertParse(
86+
"""
87+
foo ? 1 1️⃣
88+
""",
89+
diagnostics: [
90+
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"])
91+
],
92+
fixedSource: "foo ? 1 : <#expression#>"
93+
)
94+
}
95+
96+
func testInvalidIfExpr7() {
97+
assertParse(
98+
"""
99+
condition ? 1 1️⃣
100+
someOtherVariable
101+
""",
102+
diagnostics: [
103+
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"])
104+
],
105+
fixedSource: """
106+
condition ? 1 : <#expression#>
107+
someOtherVariable
108+
"""
109+
)
110+
}
72111
}

0 commit comments

Comments
 (0)