Skip to content

Commit f532d90

Browse files
committed
Add diagnostic for unexpected second identifier
1 parent 07c08da commit f532d90

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,24 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
878878
return .visitChildren
879879
}
880880

881+
public override func visit(_ node: PatternBindingSyntax) -> SyntaxVisitorContinueKind {
882+
if shouldSkip(node) {
883+
return .skipChildren
884+
}
885+
886+
if let unexpected = (node.pattern.as(TuplePatternSyntax.self)?.unexpectedBetweenElementsAndRightParen),
887+
let previousToken = node.previousToken(viewMode: .sourceAccurate) {
888+
addDiagnostic(unexpected, SpaceSeparatedIdentifiersError(firstToken: previousToken, additionalTokens: []))
889+
}
890+
891+
if let unexpected = node.typeAnnotation?.type, node.typeAnnotation?.colon.hasError == true,
892+
let previousToken = node.previousToken(viewMode: .sourceAccurate) {
893+
addDiagnostic(unexpected, SpaceSeparatedIdentifiersError(firstToken: previousToken, additionalTokens: []))
894+
}
895+
896+
return .visitChildren
897+
}
898+
881899
public override func visit(_ node: PrecedenceGroupAssignmentSyntax) -> SyntaxVisitorContinueKind {
882900
if shouldSkip(node) {
883901
return .skipChildren

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,9 @@ final class RecoveryTests: XCTestCase {
814814
diagnostics: [
815815
DiagnosticSpec(locationMarker: "1️⃣", message: "found an unexpected second identifier in struct"),
816816
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ':' in type annotation"),
817-
DiagnosticSpec(locationMarker: "3️⃣", message: #"unexpected code ': Int = ""' before function"#),
818817
// TODO: (good first issue) Old parser expected error on line 4: found an unexpected second identifier in variable declaration; is there an accidental break?
818+
DiagnosticSpec(locationMarker: "3️⃣", message: "found an unexpected second identifier in variable declaration; is there an accidental break?"),
819+
DiagnosticSpec(locationMarker: "3️⃣", message: #"unexpected code ': Int = ""' before function"#),
819820
DiagnosticSpec(locationMarker: "4️⃣", message: "expected ':' in type annotation"),
820821
]
821822
)
@@ -828,7 +829,7 @@ final class RecoveryTests: XCTestCase {
828829
""",
829830
diagnostics: [
830831
// TODO: (good first issue) Old parser expected error on line 1: found an unexpected second identifier in constant declaration; is there an accidental break?
831-
DiagnosticSpec(message: "unexpected code 'hij, foobar' in tuple pattern")
832+
DiagnosticSpec(message: "found an unexpected second identifier in constant declaration; is there an accidental break?")
832833
]
833834
)
834835
}

0 commit comments

Comments
 (0)