mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Allow .self in keypaths
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user