Skip to content

Commit 8785395

Browse files
committed
Add prefersKeyPath support for contains(where:)
1 parent 26fd167 commit 8785395

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

Sources/Rules.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5118,17 +5118,27 @@ public struct _FormatRules {
51185118
) { formatter in
51195119
formatter.forEach(.startOfScope("{")) { i, _ in
51205120
guard formatter.options.swiftVersion >= "5.2",
5121-
let prevIndex = formatter.index(of: .nonSpaceOrLinebreak, before: i)
5121+
var prevIndex = formatter.index(of: .nonSpaceOrLinebreak, before: i)
51225122
else {
51235123
return
51245124
}
51255125
var prevToken = formatter.tokens[prevIndex]
5126+
var label: String?
5127+
if prevToken == .delimiter(":"),
5128+
let labelIndex = formatter.index(of: .nonSpace, before: prevIndex),
5129+
case let .identifier(name) = formatter.tokens[labelIndex],
5130+
let prevIndex2 = formatter.index(of: .nonSpaceOrLinebreak, before: labelIndex)
5131+
{
5132+
label = name
5133+
prevToken = formatter.tokens[prevIndex2]
5134+
prevIndex = prevIndex2
5135+
}
51265136
let parenthesized = prevToken == .startOfScope("(")
51275137
if parenthesized {
51285138
prevToken = formatter.last(.nonSpaceOrLinebreak, before: prevIndex) ?? prevToken
51295139
}
51305140
guard case let .identifier(name) = prevToken,
5131-
["map", "flatMap", "compactMap", "allSatisfy", "filter"].contains(name),
5141+
["map", "flatMap", "compactMap", "allSatisfy", "filter", "contains"].contains(name),
51325142
let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: i, if: {
51335143
$0 == .identifier("$0")
51345144
}),
@@ -5137,6 +5147,13 @@ public struct _FormatRules {
51375147
else {
51385148
return
51395149
}
5150+
if name == "contains" {
5151+
if label != "where" {
5152+
return
5153+
}
5154+
} else if label != nil {
5155+
return
5156+
}
51405157
var replacementTokens: [Token]
51415158
if nextIndex == lastIndex {
51425159
// TODO: add this when https://bugs.swift.org/browse/SR-12897 is fixed
@@ -5149,6 +5166,9 @@ public struct _FormatRules {
51495166
}
51505167
replacementTokens = [.operator("\\", .prefix)] + tokens
51515168
}
5169+
if let label = label {
5170+
replacementTokens = [.identifier(label), .delimiter(":"), .space(" ")] + replacementTokens
5171+
}
51525172
if !parenthesized {
51535173
replacementTokens = [.startOfScope("(")] + replacementTokens + [.endOfScope(")")]
51545174
}

Tests/RulesTests+Syntax.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,4 +1893,17 @@ extension RulesTests {
18931893
let options = FormatOptions(swiftVersion: "5.2")
18941894
testFormatting(for: input, rule: FormatRules.preferKeyPath, options: options)
18951895
}
1896+
1897+
func testNoMapPropertyToKeyPathForTrailingContains() {
1898+
let input = "let foo = bar.contains { $0.foo }"
1899+
let options = FormatOptions(swiftVersion: "5.2")
1900+
testFormatting(for: input, rule: FormatRules.preferKeyPath, options: options)
1901+
}
1902+
1903+
func testMapPropertyToKeyPathForContainsWhere() {
1904+
let input = "let foo = bar.contains(where: { $0.foo })"
1905+
let output = "let foo = bar.contains(where: \\.foo)"
1906+
let options = FormatOptions(swiftVersion: "5.2")
1907+
testFormatting(for: input, output, rule: FormatRules.preferKeyPath, options: options)
1908+
}
18961909
}

Tests/XCTestManifests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ extension RulesTests {
10561056
("testMalformedFunctionNotMisidentifiedAsClosure", testMalformedFunctionNotMisidentifiedAsClosure),
10571057
("testMapNestedPropertyWithSpacesToKeyPath", testMapNestedPropertyWithSpacesToKeyPath),
10581058
("testMapPropertyToKeyPath", testMapPropertyToKeyPath),
1059+
("testMapPropertyToKeyPathForContainsWhere", testMapPropertyToKeyPathForContainsWhere),
10591060
("testMarkExtensionsDisabled", testMarkExtensionsDisabled),
10601061
("testMarkExtensionsIfNotEmpty", testMarkExtensionsIfNotEmpty),
10611062
("testMarkInsideMultilineComment", testMarkInsideMultilineComment),
@@ -1251,6 +1252,7 @@ extension RulesTests {
12511252
("testNoMapPropertyToKeyPathForFunctionCalls", testNoMapPropertyToKeyPathForFunctionCalls),
12521253
("testNoMapPropertyToKeyPathForOptionalChaining", testNoMapPropertyToKeyPathForOptionalChaining),
12531254
("testNoMapPropertyToKeyPathForSwiftLessThan5_2", testNoMapPropertyToKeyPathForSwiftLessThan5_2),
1255+
("testNoMapPropertyToKeyPathForTrailingContains", testNoMapPropertyToKeyPathForTrailingContains),
12541256
("testNoMapSelfToKeyPath", testNoMapSelfToKeyPath),
12551257
("testNoMarkFunctionArgument", testNoMarkFunctionArgument),
12561258
("testNoMarkNamedFunctionArgument", testNoMarkNamedFunctionArgument),

0 commit comments

Comments
 (0)