Skip to content

Commit 0cd98af

Browse files
authored
Merge pull request #910 from ahoppen/ahoppen/semi-statements
Diagnose ';' statements
2 parents a412b9b + 24a690d commit 0cd98af

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

Sources/SwiftParser/Diagnostics/DiagnosticExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension FixIt.Changes {
3737
newNode: Syntax(TokenSyntax(node.tokenKind, leadingTrivia: [], trailingTrivia: [], presence: .missing))
3838
)
3939
]
40-
if !node.leadingTrivia.isEmpty, let nextToken = node.nextToken(viewMode: .sourceAccurate) {
40+
if !node.leadingTrivia.isEmpty, let nextToken = node.nextToken(viewMode: .sourceAccurate), !nextToken.leadingTrivia.contains(where: { $0.isNewline }) {
4141
changes.append(.replaceLeadingTrivia(token: nextToken, newTrivia: node.leadingTrivia))
4242
}
4343
return FixIt.Changes(changes: changes)

Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
232232
handledNodes.append(semicolon.id)
233233
}
234234
}
235+
if let semicolon = node.semicolon, semicolon.presence == .present, node.item.isMissingAllTokens {
236+
addDiagnostic(node, .stanaloneSemicolonStatement, fixIts: [
237+
FixIt(message: RemoveTokensFixIt(tokensToRemove: [semicolon]), changes: [
238+
.makeMissing(node: semicolon)
239+
])
240+
], handledNodes: [node.item.id])
241+
}
235242
return .visitChildren
236243
}
237244

Sources/SwiftParser/Diagnostics/ParserDiagnosticMessages.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public enum StaticParserError: String, DiagnosticMessage {
7272
case editorPlaceholderInSourceFile = "editor placeholder in source file"
7373
case missingColonInTernaryExprDiagnostic = "expected ':' after '? ...' in ternary expression"
7474
case missingFunctionParameterClause = "expected argument list in function declaration"
75+
case stanaloneSemicolonStatement = "';' statements are not allowed"
7576
case throwsInReturnPosition = "'throws' may only occur before '->'"
7677
case tryMustBePlacedOnReturnedExpr = "'try' must be placed on the returned expression"
7778
case tryMustBePlacedOnThrownExpr = "'try' must be placed on the thrown expression"

Tests/SwiftParserTest/translated/SwitchTests.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,22 @@ final class SwitchTests: XCTestCase {
228228
func testSwitch15() {
229229
AssertParse(
230230
"""
231-
switch x {
232-
case 0:1️⃣
233-
;
231+
switch x {
232+
case 0:
233+
1️⃣;
234234
case 1:
235235
x = 0
236236
}
237237
""",
238238
diagnostics: [
239-
DiagnosticSpec(message: "expected expression in switch case"),
240-
// TODO: Old parser expected error on line 3: ';' statements are not allowed, Fix-It replacements: 3 - 5 = ''
241-
]
239+
DiagnosticSpec(message: "';' statements are not allowed", fixIts: ["remove ';'"]),
240+
], fixedSource: """
241+
switch x {
242+
case 0:
243+
case 1:
244+
x = 0
245+
}
246+
"""
242247
)
243248
}
244249

0 commit comments

Comments
 (0)