1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
import _SwiftFormatTestSupport
@_spi(Rules) import SwiftFormat
final class DoNotUseSemicolonsTests: LintOrFormatRuleTestCase {
func testSemicolonUse() {
assertFormatting(
DoNotUseSemicolons.self,
input: """
print("hello")1️⃣; print("goodbye")2️⃣;
print("3")
""",
expected: """
print("hello")
print("goodbye")
print("3")
""",
findings: [
FindingSpec("1️⃣", message: "remove ';' and move the next statement to a new line"),
FindingSpec("2️⃣", message: "remove ';'"),
]
)
}
func testSemicolonsInNestedStatements() {
assertFormatting(
DoNotUseSemicolons.self,
input: """
guard let someVar = Optional(items.filter ({ a in foo(a)1️⃣; return true2️⃣; })) else {
items.forEach { a in foo(a)3️⃣; }4️⃣; return5️⃣;
}
""",
// The formatting in the expected output is unappealing, but that is fixed by the pretty
// printer and isn't a concern for the format rule.
expected: """
guard let someVar = Optional(items.filter ({ a in foo(a)
return true })) else {
items.forEach { a in foo(a) }
return
}
""",
findings: [
FindingSpec("1️⃣", message: "remove ';' and move the next statement to a new line"),
FindingSpec("2️⃣", message: "remove ';'"),
FindingSpec("3️⃣", message: "remove ';'"),
FindingSpec("4️⃣", message: "remove ';' and move the next statement to a new line"),
FindingSpec("5️⃣", message: "remove ';'"),
]
)
}
func testSemicolonsInMemberLists() {
assertFormatting(
DoNotUseSemicolons.self,
input: """
struct Foo {
func foo() {
code()
}1️⃣;
let someVar = 52️⃣;let someOtherVar = 63️⃣;
}
""",
expected: """
struct Foo {
func foo() {
code()
}
let someVar = 5
let someOtherVar = 6
}
""",
findings: [
FindingSpec("1️⃣", message: "remove ';'"),
FindingSpec("2️⃣", message: "remove ';' and move the next statement to a new line"),
FindingSpec("3️⃣", message: "remove ';'"),
]
)
}
func testNewlinesAfterSemicolons() {
assertFormatting(
DoNotUseSemicolons.self,
input: """
print("hello")1️⃣;
/// This is a doc comment for printing "goodbye".
print("goodbye")2️⃣;
/// This is a doc comment for printing "3".
print("3")3️⃣;
print("4")4️⃣; /** Inline comment. */ print("5")5️⃣;
print("6")6️⃣; // This is an important statement.
print("7")7️⃣;
""",
expected: """
print("hello")
/// This is a doc comment for printing "goodbye".
print("goodbye")
/// This is a doc comment for printing "3".
print("3")
print("4")
/** Inline comment. */ print("5")
print("6") // This is an important statement.
print("7")
""",
findings: [
FindingSpec("1️⃣", message: "remove ';'"),
FindingSpec("2️⃣", message: "remove ';'"),
FindingSpec("3️⃣", message: "remove ';'"),
FindingSpec("4️⃣", message: "remove ';' and move the next statement to a new line"),
FindingSpec("5️⃣", message: "remove ';'"),
FindingSpec("6️⃣", message: "remove ';'"),
FindingSpec("7️⃣", message: "remove ';'"),
]
)
}
func testBlockCommentAtEndOfBlock() {
assertFormatting(
DoNotUseSemicolons.self,
input: """
print("hello")1️⃣; /* block comment */
""",
expected: """
print("hello") /* block comment */
""",
findings: [
FindingSpec("1️⃣", message: "remove ';'"),
]
)
assertFormatting(
DoNotUseSemicolons.self,
input: """
if x {
print("hello")1️⃣; /* block comment */
}
""",
expected: """
if x {
print("hello") /* block comment */
}
""",
findings: [
FindingSpec("1️⃣", message: "remove ';'"),
]
)
}
func testBlockCommentAfterSemicolonPrecedingOtherStatement() {
assertFormatting(
DoNotUseSemicolons.self,
input: """
print("hello")1️⃣; /* block comment */ print("world")
""",
expected: """
print("hello")
/* block comment */ print("world")
""",
findings: [
FindingSpec("1️⃣", message: "remove ';' and move the next statement to a new line"),
]
)
assertFormatting(
DoNotUseSemicolons.self,
input: """
if x {
print("hello")1️⃣; /* block comment */
}
""",
expected: """
if x {
print("hello") /* block comment */
}
""",
findings: [
FindingSpec("1️⃣", message: "remove ';'"),
]
)
}
func testSemicolonsSeparatingDoWhile() {
assertFormatting(
DoNotUseSemicolons.self,
input: """
do { f() };
while someCondition { g() }
do {
f()
};
// Comment and whitespace separating blocks.
while someCondition {
g()
}
do { f() }1️⃣;
for _ in 0..<10 { g() }
""",
expected: """
do { f() };
while someCondition { g() }
do {
f()
};
// Comment and whitespace separating blocks.
while someCondition {
g()
}
do { f() }
for _ in 0..<10 { g() }
""",
findings: [
FindingSpec("1️⃣", message: "remove ';'"),
]
)
}
}
|