Skip to content

Commit a6b02db

Browse files
committed
Add newline fix-it after consecutive statements
1 parent 88172a5 commit a6b02db

14 files changed

+1231
-90
lines changed

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,16 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
719719
position: position,
720720
.consecutiveStatementsOnSameLine,
721721
fixIts: [
722-
FixIt(message: .insertSemicolon, changes: .makePresent(semicolon))
723-
],
722+
node.lastToken(viewMode: .sourceAccurate).map {
723+
FixIt(
724+
message: .insertNewline,
725+
changes: [
726+
.replaceTrailingTrivia(token: $0, newTrivia: $0.trailingTrivia + .newlines(1) + getIndentation(for: $0))
727+
]
728+
)
729+
},
730+
FixIt(message: .insertSemicolon, changes: .makePresent(semicolon)),
731+
].compactMap { $0 },
724732
handledNodes: [semicolon.id]
725733
)
726734
} else {

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ extension DiagnosticMessage where Self == StaticParserError {
109109
.init("consecutive declarations on a line must be separated by newline or ';'")
110110
}
111111
public static var consecutiveStatementsOnSameLine: Self {
112-
.init("consecutive statements on a line must be separated by ';'")
112+
.init("consecutive statements on a line must be separated by newline or ';'")
113113
}
114114
public static var cStyleForLoop: Self {
115115
.init("C-style for statement has been removed in Swift 3")

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,54 @@ final class DeclarationTests: XCTestCase {
328328
internal(set) var defaultProp = 0
329329
""",
330330
diagnostics: [
331-
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
332-
DiagnosticSpec(locationMarker: "2️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
331+
DiagnosticSpec(
332+
locationMarker: "1️⃣",
333+
message: "consecutive statements on a line must be separated by newline or ';'",
334+
fixIts: ["insert newline", "insert ';'"]
335+
),
336+
DiagnosticSpec(
337+
locationMarker: "2️⃣",
338+
message: "consecutive statements on a line must be separated by newline or ';'",
339+
fixIts: ["insert newline", "insert ';'"]
340+
),
341+
],
342+
applyFixIts: ["insert newline"],
343+
fixedSource: """
344+
open
345+
open(set)
346+
var openProp = 0
347+
public public(set) var publicProp = 0
348+
package package(set) var packageProp = 0
349+
internal internal(set) var internalProp = 0
350+
fileprivate fileprivate(set) var fileprivateProp = 0
351+
private private(set) var privateProp = 0
352+
internal(set) var defaultProp = 0
353+
"""
354+
)
355+
356+
assertParse(
357+
"""
358+
open1️⃣ open(set)2️⃣ var openProp = 0
359+
public public(set) var publicProp = 0
360+
package package(set) var packageProp = 0
361+
internal internal(set) var internalProp = 0
362+
fileprivate fileprivate(set) var fileprivateProp = 0
363+
private private(set) var privateProp = 0
364+
internal(set) var defaultProp = 0
365+
""",
366+
diagnostics: [
367+
DiagnosticSpec(
368+
locationMarker: "1️⃣",
369+
message: "consecutive statements on a line must be separated by newline or ';'",
370+
fixIts: ["insert newline", "insert ';'"]
371+
),
372+
DiagnosticSpec(
373+
locationMarker: "2️⃣",
374+
message: "consecutive statements on a line must be separated by newline or ';'",
375+
fixIts: ["insert newline", "insert ';'"]
376+
),
333377
],
378+
applyFixIts: ["insert ';'"],
334379
fixedSource: """
335380
open; open(set); var openProp = 0
336381
public public(set) var publicProp = 0
@@ -933,18 +978,74 @@ final class DeclarationTests: XCTestCase {
933978
)
934979
}
935980

936-
func testTextRecovery() {
981+
func testTextRecovery1() {
937982
assertParse(
938983
"""
939984
Lorem1️⃣ ipsum2️⃣ dolor3️⃣ sit4️⃣ amet5️⃣, consectetur adipiscing elit
940985
""",
941986
diagnostics: [
942-
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
943-
DiagnosticSpec(locationMarker: "2️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
944-
DiagnosticSpec(locationMarker: "3️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
945-
DiagnosticSpec(locationMarker: "4️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
987+
DiagnosticSpec(
988+
locationMarker: "1️⃣",
989+
message: "consecutive statements on a line must be separated by newline or ';'",
990+
fixIts: ["insert newline", "insert ';'"]
991+
),
992+
DiagnosticSpec(
993+
locationMarker: "2️⃣",
994+
message: "consecutive statements on a line must be separated by newline or ';'",
995+
fixIts: ["insert newline", "insert ';'"]
996+
),
997+
DiagnosticSpec(
998+
locationMarker: "3️⃣",
999+
message: "consecutive statements on a line must be separated by newline or ';'",
1000+
fixIts: ["insert newline", "insert ';'"]
1001+
),
1002+
DiagnosticSpec(
1003+
locationMarker: "4️⃣",
1004+
message: "consecutive statements on a line must be separated by newline or ';'",
1005+
fixIts: ["insert newline", "insert ';'"]
1006+
),
1007+
DiagnosticSpec(locationMarker: "5️⃣", message: "extraneous code ', consectetur adipiscing elit' at top level"),
1008+
],
1009+
applyFixIts: ["insert newline"],
1010+
fixedSource: """
1011+
Lorem
1012+
ipsum
1013+
dolor
1014+
sit
1015+
amet, consectetur adipiscing elit
1016+
"""
1017+
)
1018+
}
1019+
1020+
func testTextRecovery2() {
1021+
assertParse(
1022+
"""
1023+
Lorem1️⃣ ipsum2️⃣ dolor3️⃣ sit4️⃣ amet5️⃣, consectetur adipiscing elit
1024+
""",
1025+
diagnostics: [
1026+
DiagnosticSpec(
1027+
locationMarker: "1️⃣",
1028+
message: "consecutive statements on a line must be separated by newline or ';'",
1029+
fixIts: ["insert newline", "insert ';'"]
1030+
),
1031+
DiagnosticSpec(
1032+
locationMarker: "2️⃣",
1033+
message: "consecutive statements on a line must be separated by newline or ';'",
1034+
fixIts: ["insert newline", "insert ';'"]
1035+
),
1036+
DiagnosticSpec(
1037+
locationMarker: "3️⃣",
1038+
message: "consecutive statements on a line must be separated by newline or ';'",
1039+
fixIts: ["insert newline", "insert ';'"]
1040+
),
1041+
DiagnosticSpec(
1042+
locationMarker: "4️⃣",
1043+
message: "consecutive statements on a line must be separated by newline or ';'",
1044+
fixIts: ["insert newline", "insert ';'"]
1045+
),
9461046
DiagnosticSpec(locationMarker: "5️⃣", message: "extraneous code ', consectetur adipiscing elit' at top level"),
9471047
],
1048+
applyFixIts: ["insert ';'"],
9481049
fixedSource: """
9491050
Lorem; ipsum; dolor; sit; amet, consectetur adipiscing elit
9501051
"""

0 commit comments

Comments
 (0)