Skip to content

Commit 7488e49

Browse files
committed
Add contextual diagnostic message if colon is missing in ternary expression
rdar://98745337
1 parent f34aaed commit 7488e49

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,16 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
169169
}
170170
return .visitChildren
171171
}
172+
173+
public override func visit(_ node: UnresolvedTernaryExprSyntax) -> SyntaxVisitorContinueKind {
174+
if shouldSkip(node) {
175+
return .skipChildren
176+
}
177+
if node.colonMark.presence == .missing {
178+
addDiagnostic(node.colonMark, .missingColonInTernaryExprDiagnostic)
179+
markNodesAsHandled(node.colonMark.id)
180+
}
181+
return .visitChildren
182+
}
172183
}
173184

Sources/SwiftParser/Diagnostics/ParserDiagnosticMessages.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public extension ParserFixIt {
7676
/// Please order the cases in this enum alphabetically by case name.
7777
public enum StaticParserError: String, DiagnosticMessage {
7878
case cStyleForLoop = "C-style for statement has been removed in Swift 3"
79+
case missingColonInTernaryExprDiagnostic = "Expected ':' after '? ...' in ternary expression"
7980
case missingFunctionParameterClause = "Expected argument list in function declaration"
8081
case throwsInReturnPosition = "'throws' may only occur before '->'"
8182

Tests/SwiftParserTest/DiagnosticTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,16 @@ public class DiagnosticTests: XCTestCase {
9393

9494
XCTAssertSingleDiagnostic(in: classDecl, line: 2, column: 25, message: "Expected argument list in function declaration")
9595
}
96+
97+
func testMissingColonInTernary() throws {
98+
let source = """
99+
foo ? 1
100+
"""
101+
102+
let node = withParser(source: source) {
103+
Syntax(raw: $0.parseExpression().raw)
104+
}
105+
106+
XCTAssertSingleDiagnostic(in: node, line: 1, column: 8, message: "Expected ':' after '? ...' in ternary expression")
107+
}
96108
}

0 commit comments

Comments
 (0)