Skip to content

Commit bfc6880

Browse files
committed
Fix wrong fix-it in modifier for variable decl
1 parent fcc4880 commit bfc6880

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,40 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
15251525
if shouldSkip(node) {
15261526
return .skipChildren
15271527
}
1528+
1529+
if let modifiers = node.modifiers, modifiers.hasError {
1530+
for modifier in modifiers {
1531+
if let detail = modifier.detail, detail.detail != .keyword(.set) {
1532+
let unexpectedBetweenLeftParenAndDetail = detail.unexpectedBetweenLeftParenAndDetail?.tokens(viewMode: .all)
1533+
let unexpectedBetweenDetailAndRightParen = detail.unexpectedBetweenDetailAndRightParen?.tokens(viewMode: .all)
1534+
let unexpectedTokens = (unexpectedBetweenLeftParenAndDetail?.map { $0 } ?? []) + (unexpectedBetweenDetailAndRightParen?.map { $0 } ?? [])
1535+
1536+
// If there is no unexpected tokens it means we miss a paren or set keyword.
1537+
// So we just skip the handling here
1538+
guard !unexpectedTokens.isEmpty else {
1539+
return .visitChildren
1540+
}
1541+
1542+
addDiagnostic(
1543+
unexpectedTokens.first!,
1544+
MissingNodesError(missingNodes: [Syntax(detail.detail)]),
1545+
fixIts: [
1546+
FixIt(
1547+
message: ReplaceTokensFixIt(
1548+
replaceTokens: unexpectedTokens.map { $0 },
1549+
replacements: [detail.detail]
1550+
),
1551+
changes: [
1552+
FixIt.MultiNodeChange.makePresent(detail.detail)
1553+
] + unexpectedTokens.map { FixIt.MultiNodeChange.makeMissing($0) }
1554+
)
1555+
],
1556+
handledNodes: [detail.id] + unexpectedTokens.map(\.id)
1557+
)
1558+
}
1559+
}
1560+
}
1561+
15281562
let missingTries = node.bindings.compactMap({
15291563
return $0.initializer?.value.as(TryExprSyntax.self)?.tryKeyword
15301564
})

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,10 @@ final class DeclarationTests: XCTestCase {
343343
private(1️⃣get) var a = 0
344344
""",
345345
diagnostics: [
346-
DiagnosticSpec(message: "expected 'set' in modifier", fixIts: ["insert 'set'"]),
347-
DiagnosticSpec(message: "unexpected code 'get' in modifier"),
346+
DiagnosticSpec(message: "expected 'set' in modifier", fixIts: ["replace 'get' with 'set'"])
348347
],
349348
fixedSource: """
350-
private(setget) var a = 0
349+
private(set) var a = 0
351350
"""
352351
)
353352

@@ -374,17 +373,29 @@ final class DeclarationTests: XCTestCase {
374373
private(1️⃣get, set) var a = 0
375374
""",
376375
diagnostics: [
377-
DiagnosticSpec(message: "unexpected code 'get,' in modifier")
378-
]
376+
DiagnosticSpec(
377+
message: "expected 'set' in modifier",
378+
fixIts: ["replace 'get,' with 'set'"]
379+
)
380+
],
381+
fixedSource: """
382+
private(set) var a = 0
383+
"""
379384
)
380385

381386
assertParse(
382387
"""
383388
private(1️⃣get: set) var a = 0
384389
""",
385390
diagnostics: [
386-
DiagnosticSpec(message: "unexpected code 'get:' in modifier")
387-
]
391+
DiagnosticSpec(
392+
message: "expected 'set' in modifier",
393+
fixIts: ["replace 'get:' with 'set'"]
394+
)
395+
],
396+
fixedSource: """
397+
private(set) var a = 0
398+
"""
388399
)
389400

390401
assertParse(
@@ -410,12 +421,17 @@ final class DeclarationTests: XCTestCase {
410421

411422
assertParse(
412423
"""
413-
private(1️⃣get, set2️⃣, didSet) var a = 0
424+
private(1️⃣get, set, didSet) var a = 0
414425
""",
415426
diagnostics: [
416-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'get,' in modifier"),
417-
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code ', didSet' in modifier"),
418-
]
427+
DiagnosticSpec(
428+
message: "expected 'set' in modifier",
429+
fixIts: ["replace 'get, , didSet' with 'set'"]
430+
)
431+
],
432+
fixedSource: """
433+
private(set) var a = 0
434+
"""
419435
)
420436

421437
assertParse(

0 commit comments

Comments
 (0)