Fix Self being incorrectly removed in declarations or expressions

This commit is contained in:
Nick Lockwood
2023-08-19 05:57:44 +01:00
parent f8d6f0457a
commit d9c558da9f
2 changed files with 35 additions and 6 deletions
+7 -5
View File
@@ -2441,12 +2441,12 @@ extension Formatter {
processDeclaredVariables(at: &i, names: &members)
}
} else {
let removeSelf = explicitSelf != .insert && !usingDynamicLookup && !inClosureDisallowingImplicitSelf
let onlyLocal = options.swiftVersion < "5"
let removeSelf = explicitSelf != .insert && !usingDynamicLookup && (
(staticSelf && classOrStatic) || (!staticSelf && !inClosureDisallowingImplicitSelf)
)
processDeclaredVariables(at: &i, names: &localNames,
removeSelfKeyword: removeSelf ? selfKeyword : nil,
onlyLocal: onlyLocal,
onlyLocal: options.swiftVersion < "5",
scopeAllowsImplicitSelfRebinding: scopeAllowsImplicitSelfRebinding)
}
case .keyword("func"):
@@ -2586,7 +2586,9 @@ extension Formatter {
assert(!isTypeRoot)
// Guard is included because it's an error to reference guard vars in body
var scopedNames = localNames
let removeSelf = explicitSelf != .insert && !inClosureDisallowingImplicitSelf
let removeSelf = explicitSelf != .insert && !usingDynamicLookup && (
(staticSelf && classOrStatic) || (!staticSelf && !inClosureDisallowingImplicitSelf)
)
processDeclaredVariables(
at: &index, names: &scopedNames,
removeSelfKeyword: removeSelf ? selfKeyword : nil,
+28 -1
View File
@@ -6433,6 +6433,19 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, rule: FormatRules.redundantStaticSelf)
}
func testPreserveStaticSelfInInstanceFunction() {
let input = """
enum Foo {
static var value = 0
func f() {
Self.value = value
}
}
"""
testFormatting(for: input, rule: FormatRules.redundantStaticSelf)
}
func testPreserveStaticSelfForShadowedProperty() {
let input = """
enum Foo {
@@ -6448,7 +6461,7 @@ class RedundancyTests: RulesTests {
func testPreserveStaticSelfInGetter() {
let input = """
public enum Foo {
enum Foo {
static let foo: String = "foo"
var sharedFoo: String {
@@ -6481,6 +6494,20 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, output, rule: FormatRules.redundantStaticSelf)
}
func testPreserveStaticSelfInGuardLet() {
let input = """
class LocationDeeplink: Deeplink {
convenience init?(warnRegion: String) {
guard let value = Self.location(for: warnRegion) else {
return nil
}
self.init(location: value)
}
}
"""
testFormatting(for: input, rule: FormatRules.redundantStaticSelf)
}
// MARK: - semicolons
func testSemicolonRemovedAtEndOfLine() {