Skip to content

Commit c9cdd12

Browse files
committed
Special case error messages if a single keyword is unexpected
1 parent 8198297 commit c9cdd12

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

Sources/SwiftParser/Diagnostics/SyntaxExtensions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ extension SyntaxProtocol {
8383
} else {
8484
return "braces"
8585
}
86+
} else if let token = Syntax(self).as(UnexpectedNodesSyntax.self)?.onlyTokens(satisfying: { $0.tokenKind.isKeyword })?.only {
87+
return "'\(token.text)' keyword"
88+
} else if let token = Syntax(self).as(TokenSyntax.self) {
89+
return "'\(token.text)' keyword"
8690
} else if contentWithoutTrivia.contains("\n") || contentWithoutTrivia.count > 100 {
8791
return "code"
8892
} else {

Tests/SwiftParserTest/Types.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ final class TypeTests: XCTestCase {
5555
print("closure with empty capture list")
5656
}
5757
""")
58-
58+
5959
AssertParse("""
6060
{ ()
6161
throws -> Void in }
@@ -71,7 +71,7 @@ final class TypeTests: XCTestCase {
7171
{ $0.parseClosureExpression() },
7272
diagnostics: [
7373
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in closure capture item"),
74-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'class' in closure capture signature"),
74+
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected 'class' keyword in closure capture signature"),
7575
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end closure"),
7676
])
7777

Tests/SwiftParserTest/translated/EnumTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ final class EnumTests: XCTestCase {
385385
""",
386386
diagnostics: [
387387
DiagnosticSpec(locationMarker: "1️⃣", message: "keyword 'Self' cannot be used as an identifier here"),
388-
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code 'Self' in enum"),
388+
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected 'Self' keyword in enum"),
389389
]
390390
)
391391
}

Tests/SwiftParserTest/translated/ErrorsTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ final class ErrorsTests: XCTestCase {
187187
diagnostics: [
188188
// TODO: Old parser expected error on line 1: 'rethrows' may only occur before '->', Fix-It replacements: 43 - 43 = 'rethrows ', 46 - 55 = ''
189189
DiagnosticSpec(locationMarker: "1️⃣", message: "expected 'rethrows' in function signature"),
190-
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code 'rethrows' in function signature"),
190+
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected 'rethrows' keyword in function signature"),
191191
]
192192
)
193193
}
@@ -263,7 +263,7 @@ final class ErrorsTests: XCTestCase {
263263
diagnostics: [
264264
// TODO: Old parser expected error on line 3: consecutive statements on a line must be separated by ';'
265265
// TODO: Old parser expected error on line 3: expected expression
266-
DiagnosticSpec(message: "unexpected code 'throws' in function"),
266+
DiagnosticSpec(message: "unexpected 'throws' keyword in function"),
267267
]
268268
)
269269
}

Tests/SwiftParserTest/translated/IfconfigExprTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ final class IfconfigExprTests: XCTestCase {
142142
}
143143
""",
144144
diagnostics: [
145-
DiagnosticSpec(message: "unexpected code 'return' in conditional compilation block"),
145+
DiagnosticSpec(message: "unexpected 'return' keyword in conditional compilation block"),
146146
]
147147
)
148148
}

0 commit comments

Comments
 (0)