Skip to content

Commit 301cf8d

Browse files
committed
Add newline fix-it after consecutive statements
1 parent 325c99e commit 301cf8d

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
@@ -1024,18 +1069,74 @@ final class DeclarationTests: XCTestCase {
10241069
)
10251070
}
10261071

1027-
func testTextRecovery() {
1072+
func testTextRecovery1() {
10281073
assertParse(
10291074
"""
10301075
Lorem1️⃣ ipsum2️⃣ dolor3️⃣ sit4️⃣ amet5️⃣, consectetur adipiscing elit
10311076
""",
10321077
diagnostics: [
1033-
DiagnosticSpec(locationMarker: "1️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
1034-
DiagnosticSpec(locationMarker: "2️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
1035-
DiagnosticSpec(locationMarker: "3️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
1036-
DiagnosticSpec(locationMarker: "4️⃣", message: "consecutive statements on a line must be separated by ';'", fixIts: ["insert ';'"]),
1078+
DiagnosticSpec(
1079+
locationMarker: "1️⃣",
1080+
message: "consecutive statements on a line must be separated by newline or ';'",
1081+
fixIts: ["insert newline", "insert ';'"]
1082+
),
1083+
DiagnosticSpec(
1084+
locationMarker: "2️⃣",
1085+
message: "consecutive statements on a line must be separated by newline or ';'",
1086+
fixIts: ["insert newline", "insert ';'"]
1087+
),
1088+
DiagnosticSpec(
1089+
locationMarker: "3️⃣",
1090+
message: "consecutive statements on a line must be separated by newline or ';'",
1091+
fixIts: ["insert newline", "insert ';'"]
1092+
),
1093+
DiagnosticSpec(
1094+
locationMarker: "4️⃣",
1095+
message: "consecutive statements on a line must be separated by newline or ';'",
1096+
fixIts: ["insert newline", "insert ';'"]
1097+
),
1098+
DiagnosticSpec(locationMarker: "5️⃣", message: "extraneous code ', consectetur adipiscing elit' at top level"),
1099+
],
1100+
applyFixIts: ["insert newline"],
1101+
fixedSource: """
1102+
Lorem
1103+
ipsum
1104+
dolor
1105+
sit
1106+
amet, consectetur adipiscing elit
1107+
"""
1108+
)
1109+
}
1110+
1111+
func testTextRecovery2() {
1112+
assertParse(
1113+
"""
1114+
Lorem1️⃣ ipsum2️⃣ dolor3️⃣ sit4️⃣ amet5️⃣, consectetur adipiscing elit
1115+
""",
1116+
diagnostics: [
1117+
DiagnosticSpec(
1118+
locationMarker: "1️⃣",
1119+
message: "consecutive statements on a line must be separated by newline or ';'",
1120+
fixIts: ["insert newline", "insert ';'"]
1121+
),
1122+
DiagnosticSpec(
1123+
locationMarker: "2️⃣",
1124+
message: "consecutive statements on a line must be separated by newline or ';'",
1125+
fixIts: ["insert newline", "insert ';'"]
1126+
),
1127+
DiagnosticSpec(
1128+
locationMarker: "3️⃣",
1129+
message: "consecutive statements on a line must be separated by newline or ';'",
1130+
fixIts: ["insert newline", "insert ';'"]
1131+
),
1132+
DiagnosticSpec(
1133+
locationMarker: "4️⃣",
1134+
message: "consecutive statements on a line must be separated by newline or ';'",
1135+
fixIts: ["insert newline", "insert ';'"]
1136+
),
10371137
DiagnosticSpec(locationMarker: "5️⃣", message: "extraneous code ', consectetur adipiscing elit' at top level"),
10381138
],
1139+
applyFixIts: ["insert ';'"],
10391140
fixedSource: """
10401141
Lorem; ipsum; dolor; sit; amet, consectetur adipiscing elit
10411142
"""

0 commit comments

Comments
 (0)