Skip to content

Commit 192d7af

Browse files
committed
Fix diagnostic of ternary operator missing only colon
1 parent 319e9c2 commit 192d7af

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,8 @@ extension Parser {
318318
)
319319

320320
let rhs: RawExprSyntax?
321-
if colon.isMissing {
322-
// If the colon is missing there's not much more structure we can
323-
// expect out of this expression sequence. Emit a missing expression
324-
// to end the parsing here.
321+
if colon.isMissing,
322+
currentToken.rawTokenKind != currentToken.cursor.previousTokenKind {
325323
rhs = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
326324
} else {
327325
rhs = nil

Tests/SwiftParserTest/translated/InvalidIfExprTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,15 @@ 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+
}
7283
}

0 commit comments

Comments
 (0)