Skip to content

Commit 79f2795

Browse files
committed
Check parents for childNameForDiagnostics
1 parent 8d5a984 commit 79f2795

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Sources/SwiftParser/Diagnostics/MissingNodesError.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ fileprivate enum NodesDescriptionPart {
4242
}
4343
return token.tokenKind.decomposeToRaw().rawKind.nameForDiagnostics
4444
case .node(let node):
45-
if let childName = node.childNameInParent {
46-
return childName
47-
} else {
48-
return node.nodeTypeNameForDiagnostics(allowBlockNames: true)
45+
var walk: Syntax = node
46+
while true {
47+
if let childName = walk.childNameInParent {
48+
return childName
49+
}
50+
if let parent = walk.parent, parent.children(viewMode: .all).count == 1 {
51+
// If walk is the only node in its parent, check if that parent has a childNameForDiagnostics
52+
walk = parent
53+
} else {
54+
break
55+
}
4956
}
57+
return node.nodeTypeNameForDiagnostics(allowBlockNames: true)
5058
}
5159
}
5260

Tests/SwiftParserTest/translated/GuardTests.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@ final class GuardTests: XCTestCase {
99
func noConditionNoElse() {
1010
guard {} 1️⃣
1111
}
12-
func noCondition() {
13-
guard 2️⃣else {}
14-
}3️⃣
1512
""",
1613
diagnostics: [
17-
// TODO: Old parser expected error on line 2: missing condition in 'guard' statement
18-
// TODO: Old parser expected error on line 2: expected 'else' after 'guard' condition
19-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected 'else' and body in 'guard' statement"),
20-
// TODO: Old parser expected error on line 5: missing condition in 'guard' statement
21-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected expression in 'guard' statement"),
14+
DiagnosticSpec(message: "expected 'else' and body in 'guard' statement"),
2215
]
2316
)
2417
}
2518

19+
func testGuard2() {
20+
AssertParse(
21+
"""
22+
func noCondition() {
23+
guard 1️⃣else {}
24+
}
25+
""",
26+
diagnostics: [
27+
DiagnosticSpec(message: "expected conditions in 'guard' statement"),
28+
]
29+
)
30+
}
2631
}

0 commit comments

Comments
 (0)