Skip to content

Commit 9f411b6

Browse files
committed
Test guard let
1 parent 679c340 commit 9f411b6

3 files changed

Lines changed: 23 additions & 60 deletions

File tree

Sources/Rules.swift

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ public struct _FormatRules {
10451045
case .startOfScope("{"):
10461046
guard canInsertSpaceAtStart else { return }
10471047
startToken = token
1048+
canInsertSpaceAtStart = false
10481049

10491050
guard let linebreakToken = formatter.token(at: i + 2),
10501051
!linebreakToken.isLinebreak
@@ -1075,66 +1076,6 @@ public struct _FormatRules {
10751076
break
10761077
}
10771078
}
1078-
1079-
formatter.forEachToken { i, token in
1080-
switch token {
1081-
case .keyword("class"),
1082-
.keyword("struct"),
1083-
.keyword("extension"):
1084-
guard let startOfScopeIndex = formatter.index(of: .startOfScope("{"), after: i),
1085-
!(formatter.token(at: startOfScopeIndex + 2)?.isLinebreak ?? true)
1086-
else { return }
1087-
1088-
if let wrongToken = formatter.lastToken(before: startOfScopeIndex, where: {
1089-
$0 == .keyword("func") || $0 == .keyword("protocol")
1090-
}),
1091-
let wrongIndex = formatter.index(of: token, before: startOfScopeIndex)
1092-
{
1093-
let startOfScopeLine = formatter.originalLine(at: startOfScopeIndex)
1094-
let funcLine = formatter.originalLine(at: startOfScopeIndex)
1095-
if startOfScopeLine == funcLine { return }
1096-
}
1097-
1098-
formatter.insertLinebreak(at: startOfScopeIndex + 1)
1099-
1100-
case .endOfScope("}"):
1101-
guard let currentScopeToken = formatter.currentScope(at: i),
1102-
let startOfScopeIndex = formatter.index(of: currentScopeToken, before: i),
1103-
let token = formatter.lastToken(before: startOfScopeIndex, where: {
1104-
$0 == .keyword("class") ||
1105-
$0 == .keyword("struct") ||
1106-
$0 == .keyword("extension")
1107-
}),
1108-
let keywordIndex = formatter.index(of: token, before: startOfScopeIndex)
1109-
else { return }
1110-
1111-
let startOfScopeLine = formatter.originalLine(at: startOfScopeIndex)
1112-
let keywordLine = formatter.originalLine(at: keywordIndex)
1113-
1114-
if let wrongToken = formatter.lastToken(before: startOfScopeIndex, where: {
1115-
$0 == .keyword("func") || $0 == .keyword("protocol")
1116-
}),
1117-
let wrongIndex = formatter.index(of: token, before: startOfScopeIndex)
1118-
{
1119-
let funcLine = formatter.originalLine(at: wrongIndex)
1120-
if startOfScopeLine == funcLine { return }
1121-
}
1122-
1123-
guard startOfScopeLine == keywordLine else { return }
1124-
guard !(formatter.token(at: i)?.isLinebreak ?? true) else { return }
1125-
guard let prevEndScopeIndex = formatter.index(of: .endOfScope, before: i) else { return }
1126-
1127-
let isCloseBrace = formatter.token(at: prevEndScopeIndex) == .endOfScope("}")
1128-
let isCorrectEndOfScope = isCloseBrace || !(formatter.token(at: i - 3)?.isLinebreak ?? true)
1129-
1130-
guard isCorrectEndOfScope, prevEndScopeIndex == i - 2 else { return }
1131-
1132-
formatter.insertLinebreak(at: prevEndScopeIndex + 1)
1133-
1134-
default:
1135-
break
1136-
}
1137-
}
11381079
}
11391080

11401081
/// Adds a blank line around MARK: comments

Tests/RulesTests+BlanklineInScope.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,27 @@ import XCTest
1212
class BlanklineInScopeTests: XCTestCase {
1313
// MARK: - Space in start/end of scope
1414

15+
func testGuardLet() {
16+
let input = """
17+
func setNavigationBarHidden(_ isHidden: Bool, animated: Bool) {
18+
guard let navigationController = navigationController else { return }
19+
20+
navigationController.setNavigationBarHidden(isHidden, animated: animated)
21+
}
22+
"""
23+
24+
let output = """
25+
func setNavigationBarHidden(_ isHidden: Bool, animated: Bool) {
26+
guard let navigationController = navigationController else { return }
27+
28+
navigationController.setNavigationBarHidden(isHidden, animated: animated)
29+
}
30+
"""
31+
32+
let formattedInput = (try? format(input, rules: [FormatRules.insertBlankLinesAtScope])) ?? ""
33+
XCTAssertEqual(output, formattedInput)
34+
}
35+
1536
func testBlankLineAtStartOfScope() {
1637
let input = """
1738
struct Foo {

Tests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ extension BlanklineInScopeTests {
115115
("testBlanklinesInsideProtocolExtension", testBlanklinesInsideProtocolExtension),
116116
("testBlankLinesInStruct", testBlankLinesInStruct),
117117
("testBlankLinesInStruct_noSpacing", testBlankLinesInStruct_noSpacing),
118+
("testGuardLet", testGuardLet),
118119
]
119120
}
120121

0 commit comments

Comments
 (0)