Skip to content

Check parents for childNameForDiagnostics #964

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 1 commit into from
Oct 17, 2022
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
16 changes: 12 additions & 4 deletions Sources/SwiftParser/Diagnostics/MissingNodesError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ fileprivate enum NodesDescriptionPart {
}
return token.tokenKind.decomposeToRaw().rawKind.nameForDiagnostics
case .node(let node):
if let childName = node.childNameInParent {
return childName
} else {
return node.nodeTypeNameForDiagnostics(allowBlockNames: true)
var walk: Syntax = node
while true {
if let childName = walk.childNameInParent {
return childName
}
if let parent = walk.parent, parent.children(viewMode: .all).count == 1 {
// If walk is the only node in its parent, check if that parent has a childNameForDiagnostics
walk = parent
} else {
break
}
}
return node.nodeTypeNameForDiagnostics(allowBlockNames: true)
}
}

Expand Down
21 changes: 13 additions & 8 deletions Tests/SwiftParserTest/translated/GuardTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ final class GuardTests: XCTestCase {
func noConditionNoElse() {
guard {} 1️⃣
}
func noCondition() {
guard 2️⃣else {}
}3️⃣
""",
diagnostics: [
// TODO: Old parser expected error on line 2: missing condition in 'guard' statement
// TODO: Old parser expected error on line 2: expected 'else' after 'guard' condition
DiagnosticSpec(locationMarker: "1️⃣", message: "expected 'else' and body in 'guard' statement"),
// TODO: Old parser expected error on line 5: missing condition in 'guard' statement
DiagnosticSpec(locationMarker: "2️⃣", message: "expected expression in 'guard' statement"),
DiagnosticSpec(message: "expected 'else' and body in 'guard' statement"),
]
)
}

func testGuard2() {
AssertParse(
"""
func noCondition() {
guard 1️⃣else {}
}
""",
diagnostics: [
DiagnosticSpec(message: "expected conditions in 'guard' statement"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we allow "conditions" but IMO "condition" makes more sense here, ie. we expect at least one condition, not necessarily multiple.

]
)
}
}