Allow .self in keypaths

This commit is contained in:
Nick Lockwood
2024-11-24 08:21:10 +00:00
parent 409cebcb00
commit 4fd7bb0cbb
2 changed files with 17 additions and 3 deletions
+3 -3
View File
@@ -57,9 +57,9 @@ public extension FormatRule {
}
var replacementTokens: [Token]
if nextIndex == lastIndex {
// TODO: add this when https://bugs.swift.org/browse/SR-12897 is fixed
// replacementTokens = tokenize("\\.self")
return
// https://bugs.swift.org/browse/SR-12897
guard formatter.options.swiftVersion >= "5.10" else { return }
replacementTokens = tokenize("\\.self")
} else {
let tokens = formatter.tokens[nextIndex + 1 ... lastIndex]
guard tokens.allSatisfy({ $0.isSpace || $0.isIdentifier || $0.isOperator(".") }) else {
+14
View File
@@ -110,4 +110,18 @@ class PreferKeyPathTests: XCTestCase {
let options = FormatOptions(swiftVersion: "5.2")
testFormatting(for: input, rule: .preferKeyPath, options: options)
}
func testSelfNotConvertedToKeyPathBefore5_10() {
// https://bugs.swift.org/browse/SR-12897
let input = "let foo = bar.compactMap { $0 }"
let options = FormatOptions(swiftVersion: "5.2")
testFormatting(for: input, rule: .preferKeyPath, options: options)
}
func testSelfConvertedToKeyPath() {
let input = "let foo = bar.compactMap { $0 }"
let output = "let foo = bar.compactMap(\\.self)"
let options = FormatOptions(swiftVersion: "5.10")
testFormatting(for: input, output, rule: .preferKeyPath, options: options)
}
}