Skip to content

Commit ecacd47

Browse files
committed
Add diagnostic for unexpected second identifier
1 parent ea09dab commit ecacd47

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
@@ -810,6 +810,24 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
810810
return .visitChildren
811811
}
812812

813+
public override func visit(_ node: PatternBindingSyntax) -> SyntaxVisitorContinueKind {
814+
if shouldSkip(node) {
815+
return .skipChildren
816+
}
817+
818+
if let unexpected = (node.pattern.as(TuplePatternSyntax.self)?.unexpectedBetweenElementsAndRightParen),
819+
let previousToken = node.previousToken(viewMode: .sourceAccurate) {
820+
addDiagnostic(unexpected, SpaceSeparatedIdentifiersError(firstToken: previousToken, additionalTokens: []))
821+
}
822+
823+
if let unexpected = node.typeAnnotation?.type, node.typeAnnotation?.colon.hasError == true,
824+
let previousToken = node.previousToken(viewMode: .sourceAccurate) {
825+
addDiagnostic(unexpected, SpaceSeparatedIdentifiersError(firstToken: previousToken, additionalTokens: []))
826+
}
827+
828+
return .visitChildren
829+
}
830+
813831
public override func visit(_ node: PrecedenceGroupAssignmentSyntax) -> SyntaxVisitorContinueKind {
814832
if shouldSkip(node) {
815833
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)