Skip to content

Improve diagnostic if colon and expression are missing in ternary expression #958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,22 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .skipChildren
}
if node.colonMark.presence == .missing {
addDiagnostic(node.colonMark, .missingColonInTernaryExprDiagnostic, handledNodes: [node.colonMark.id])
if let siblings = node.parent?.children(viewMode: .all),
let nextSibling = siblings[siblings.index(after: node.index)...].first,
nextSibling.is(MissingExprSyntax.self) {
addDiagnostic(node.colonMark, .missingColonAndExprInTernaryExpr, fixIts: [
FixIt(message: InsertTokenFixIt(missingNodes: [Syntax(node.colonMark), Syntax(nextSibling)]), changes: [
.makePresent(node: node.colonMark),
.makePresent(node: nextSibling),
])
], handledNodes: [node.colonMark.id, nextSibling.id])
} else {
addDiagnostic(node.colonMark, .missingColonInTernaryExpr, fixIts: [
FixIt(message: InsertTokenFixIt(missingNodes: [Syntax(node.colonMark)]), changes: [
.makePresent(node: node.colonMark),
])
], handledNodes: [node.colonMark.id])
}
}
return .visitChildren
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public enum StaticParserError: String, DiagnosticMessage {
case editorPlaceholderInSourceFile = "editor placeholder in source file"
case expectedExpressionAfterTry = "expected expression after 'try'"
case invalidFlagAfterPrecedenceGroupAssignment = "expected 'true' or 'false' after 'assignment'"
case missingColonInTernaryExprDiagnostic = "expected ':' after '? ...' in ternary expression"
case missingColonAndExprInTernaryExpr = "expected ':' and expression after '? ...' in ternary expression"
case missingColonInTernaryExpr = "expected ':' after '? ...' in ternary expression"
case operatorShouldBeDeclaredWithoutBody = "operator should not be declared with body"
case standaloneSemicolonStatement = "standalone ';' statements are not allowed"
case subscriptsCannotHaveNames = "subscripts cannot have a name"
Expand Down
3 changes: 1 addition & 2 deletions Tests/SwiftParserTest/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ final class DeclarationTests: XCTestCase {
AssertParse(
"_ = foo/* */?.description1️⃣",
diagnostics: [
DiagnosticSpec(message: "expected ':' after '? ...' in ternary expression"),
DiagnosticSpec(message: "expected expression"),
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression"),
]
)

Expand Down
3 changes: 1 addition & 2 deletions Tests/SwiftParserTest/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,7 @@ final class ExpressionTests: XCTestCase {
AssertParse(
"foo ? 11️⃣",
diagnostics: [
DiagnosticSpec(message: "expected ':' after '? ...' in ternary expression"),
DiagnosticSpec(message: "expected expression"),
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression"),
]
)
}
Expand Down
60 changes: 38 additions & 22 deletions Tests/SwiftParserTest/translated/InvalidIfExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,46 @@ final class InvalidIfExprTests: XCTestCase {
func testInvalidIfExpr1() {
AssertParse(
"""
func unbalanced_question(a: Bool, b: Bool, c: Bool, d: Bool) {
(a ? b1️⃣)
(a ? b : c ? d2️⃣)
(a ? b ? c : d3️⃣)
(a ? b ? c4️⃣)
}
(a ? b1️⃣)
""",
diagnostics: [
// TODO: Old parser expected error on line 2: expected ':' after '? ...' in ternary
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ':' after '? ...' in ternary expression"),
DiagnosticSpec(locationMarker: "1️⃣", message: "expected expression in tuple"),
// TODO: Old parser expected error on line 3: expected ':' after '? ...' in ternary
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ':' after '? ...' in ternary expression"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected expression in tuple"),
// TODO: Old parser expected error on line 4: expected ':' after '? ...' in ternary
DiagnosticSpec(locationMarker: "3️⃣", message: "expected ':' after '? ...' in ternary expression"),
DiagnosticSpec(locationMarker: "3️⃣", message: "expected expression in tuple"),
// TODO: Old parser expected error on line 5: expected ':' after '? ...' in ternary
// TODO: Old parser expected error on line 5: expected ':' after '? ...' in ternary
DiagnosticSpec(locationMarker: "4️⃣", message: "expected ':' after '? ...' in ternary expression"),
DiagnosticSpec(locationMarker: "4️⃣", message: "expected expression in tuple"),
DiagnosticSpec(locationMarker: "4️⃣", message: "expected ':' after '? ...' in ternary expression"),
DiagnosticSpec(locationMarker: "4️⃣", message: "expected expression in tuple"),
]
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"]),
], fixedSource: "(a ? b: <#expression#>)"
)
}

func testInvalidIfExpr2() {
AssertParse(
"""
(a ? b : c ? d1️⃣)
""",
diagnostics: [
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"]),
], fixedSource: "(a ? b : c ? d: <#expression#>)"
)
}

func testInvalidIfExpr3() {
AssertParse(
"""
(a ? b ? c : d1️⃣
""",
diagnostics: [
DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"]),
DiagnosticSpec(message: "expected ')' to end tuple")
], fixedSource: "(a ? b ? c : d: <#expression#>)"
)
}

func testInvalidIfExpr4() {
AssertParse(
"""
(a ? b ? c1️⃣)
""",
diagnostics: [
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#>)"
)
}

Expand Down