mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Fix Self being incorrectly removed in declarations or expressions
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user